我如何获得一些插槽/功能时,一定QTableWidgetItem在PyQt的被选中/取消被执行
-
26-09-2019 - |
题
我动态创建的表中,具有N行和M QTableWidgetItems每行(即仅用于为复选框) - 我需要运行,它知道该行和每当复选框被选中还是未选中的列代码。 p>
我的复选框亚类如下:
class CheckBox(QTableWidgetItem):
def __init__(self):
QTableWidgetItem.__init__(self,1000)
self.setTextAlignment(Qt.AlignVCenter | Qt.AlignJustify)
self.setFlags(Qt.ItemFlags(
Qt.ItemIsSelectable | Qt.ItemIsUserCheckable | Qt.ItemIsEnabled ))
def stateChanged(self):
do_something(self.row(),self.column())
...
显然,这并不重新定义函数时SIGNAL('stateChanged(int)')
-thingy是因为,嗯,什么都不会发生被调用。
但是,如果我做的:
item = CheckBox()
self.connect(item, SIGNAL('stateChanged(int)'), item.stateChanged)
在环路创建表,我得到一个错误:
TypeError: arguments did not match any overloaded call:
QObject.connect(QObject, SIGNAL(), QObject, SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'CheckBox'
QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'CheckBox'
QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'CheckBox
修改强>:
我还试图重新定义setCheckState()
但显然,做的不是的当项目被选中或得到所谓选中。
修改2 强>: 此外,改变连接到
self.connect(self.table, SIGNAL('itemClicked(item)'),
self.table.stateChanged)
其中table = QTableWidget()
没有帮助的。
我如何做这个正确的方式?
解决方案
的最简单的解决方案可能是连接到cellChanged(int, int)
的QTableWidget
信号;看看下面的例子:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
#signal handler
def myCellChanged(row, col):
print row, col
#just a helper function to setup the table
def createCheckItem(table, row, col):
check = QTableWidgetItem("Test")
check.setCheckState(Qt.Checked)
table.setItem(row,col,check)
app = QApplication(sys.argv)
#create the 5x5 table...
table = QTableWidget(5,5)
map(lambda (row,col): createCheckItem(table, row, col),
[(row, col) for row in range(0, 5) for col in range(0, 5)])
table.show()
#...and connect our signal handler to the cellChanged(int, int) signal
QObject.connect(table, SIGNAL("cellChanged(int, int)"), myCellChanged)
app.exec_()
它创建复选框的5x5表;每当它们中的一个被选中/未选中,myCellChanged
称为并打印行和改变复选框的柱;你可以那么当然使用QTableWidget.item(someRow, someColumn).checkState()
,看看它是否被选中或取消选中。
不隶属于 StackOverflow