简体   繁体   中英

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.

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 , eg print(row*self.max_column_count + column)

or

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

You can use lambda or functools.partial to pass extra data:

def print_cell(table, row, column):
    columnCount = table.columnCount()
    print(row * columnCount + column)

lambda:

table.cellClicked.connect(lambda row, column: print_cell(table, row, column))

partial:

table.cellClicked.connect(functools.partial(print_cell, table))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM