【发布时间】:2021-04-30 02:44:30
【问题描述】:
希望你没事。
我用图像填充了一个表格,它的行为大部分都像我想要的那样,除非我将单元格连接到 cellClicked 信号。
假设我有这个功能
def print_cell (column,row):
print(column,row)
...
table.cellClicked.connect(print_cell)
这按预期工作,在单击单元格时打印一个(列,行)对,但我需要的远不止这些。我的预期功能需要的不仅仅是列和行对。
我需要这样的东西:
def print_cell(column, row, max_column_count)
print(row*max_column_count + column) #I need to convert to array to load files.
我不明白如何做到这一点,因为 cellClicked 只给了我列和行。我已经尝试了很多东西,但似乎没有任何效果,我想要你的全新建议,因为我可能理解错了。
编辑:提供更多的最少代码。
# 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')
另外几个cmets:
是的,正如 cmets 中所指出的,目前外部函数不是类的一部分。
我从有关表格外观的许多功能中删除了代码,但这可能足以诊断问题。
非常感谢。
【问题讨论】:
-
请提供一个可重现的最小示例,以便我们查看可用的数据。我的直觉是
print_cell()应该是一个类的一部分,因此能够访问该类的属性,而self.max_column_count就是这些属性之一。 -
很抱歉没有先这样做,我无法共享代码,我试图对其进行编辑,但已发布以查看我展示的内容是否足够。我将编辑问题。谢谢。
-
问题已更新@couka 谢谢
-
“目前这是硬编码的,因为这个数字将取决于其他因素。” 什么因素?这些因素是否取决于类实例中定义的其他方面?这些函数在类之外是否存在特定且重要的原因?
-
我不能透露你问的大部分信息,我想尊重提出问题所需的代码的完整性,但我不能分享它,但考虑到其他人已经准确回答似乎表示没关系。我重视您的热情,希望您的回答对其他人有所帮助,但是由于您没有考虑到我没有经验,所以我迷失了它们,因为这是我的第一个 UI,我正在学习很多东西。我建议下次考虑到这一点。谢谢。
标签: python qt pyqt pyside pyside2