Signals Emitted by Embedded Checkbox in QTableWidg

2019-08-26 00:30发布

问题:

I currently have a setup where I am trying to embed checkboxes in a QTableWidget. I am setting the checkbox cells in the following way:

chkbox1 = QTableWidgetItem()
chkbox1.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
chkbox1.setCheckState(Qt.Unchecked)
chkbox2 = QTableWidgetItem()
chkbox2.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
chkbox2.setCheckState(Qt.Unchecked)
self.tblData.setItem(i, 0, chkbox1)
self.tblData.setItem(i, 1, chkbox2)

This seems to work fine, however I cannot seem to catch the signal emitted whenever a box is checked or unchecked. I've tried:

 self.connect(self.tblData, SIGNAL('itemChanged(QTableWidgetItem)'), self.updatePlot)

But this doesn't do anything. As a test, I've connected a button click to the same method, and it works fine, so I know it's just that I'm missing a signal.

From what I understand, itemChanged should be emitted any time any data is changed, and isn't changing the checkbox state changing the data?

Thanks in advance for you help.

回答1:

Signal signature is wrong. It should be itemChanged(QTableWidgetItem *) (Note the *):

self.connect(self.tblData, SIGNAL('itemChanged(QTableWidgetItem *)'), self.updatePlot)

or better, use the new style connections:

self.tblData.itemChanged.connect(self.updatePlot)


回答2:

I guess you have to use ItemClicked signal instead. Since itemChanged signal is emitted when the data is changed and data is the text perhaps.