Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

python - How to pass more arguments to a signal in Pyside2?

Hope you are fine.

I populated a table with images and it behaves like I want for the most part, except when I connect the cell to a cellClicked signal.

Lets say I have this function

def print_cell (column,row):
    print(column,row)
...
table.cellClicked.connect(print_cell)

This works as intended, prints a (column, row) pair when the cell is clicked, but i need a bit more than this. My intended function needs more than the column and row pair.

I need something like this:

def print_cell(column, row, max_column_count)
    print(row*max_column_count + column) #I need to convert to array to load files.

I dont understand how this can be done as cellClicked only gives me column and row. I have tried more than a few things but nothing seems to work, I would like your clean slate suggestions as I might have understood wrong.

Edit: providing a bit more of minimal code.

# import 
# The imports we are using are custom made except for os, sys and math, but they include everything we need. I will use the name custom_module when instancing this classes, but they are basic a shortcut to PySide2 stuff.

def count_files(*args):
    pass
#This counts the number of files given name prefix or extension inside a defined folder. It returns a unsigned integer with the total.

def print_cell(row,column):
    print(row,column) #this is the function I want to improve.

class PyDialog(custom_module.QDialog, custom_module.QMainWindow):


    def __init__(self, path, parent=None):
        super(PyDialog, self).__init__(parent)
    
    ext = '.png'
    path = 'files'
    prefix = 'icon'

    file_count = count_files(path,ext)
    max_column_count = 4 #This is hardcoded at the moment as this number will depend of other factors.
    row_count = math.ceil(float(file_count)/float(max_column_count))
    
    self.window=custom_module.dotui.load_ui(path, self)
    
    table = self.window.img_table
    
    table.setColumnCount(max_column_count)
    table.setRowCount(row_count)

    for index in range(file_count):
    
        column, row = divmod(index,max_column_count)
        
        icon_label = custom_module.QLabel()
        icon_pixmap =custom_module.QPixmap(path+prefix+str(index)+ext)
        icon_label.setPixmap(icon_pixmap)
        table.setCellWidget(column,row,icon_label)
    
    table.cellClicked.connect(print_cell)


    self.window.show()
    


if __name__ == '__main__':

   dialog = PyDialog('path')

A couple of further comments:

Yes, for now the external functions are not part of a class, as pointed out in the comments.

I stripped the code from a lot of functionalities regarding the look of the table, but this might be enough to diagnostic the problem.

Thank you very much.

question from:https://stackoverflow.com/questions/65899267/how-to-pass-more-arguments-to-a-signal-in-pyside2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You do not need to pass more arguments with the signal. In general, the additional information should be accessible through the class members. In your particular case, you have two options:

  1. To use the hard coded max_column_count, e.g. print(row*self.max_column_count + column)

or

  1. (preferred) To get the column count of the table, e.g. print(row*self.table.rowCount() + column)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...