JLYK
JLYK

Reputation: 391

pyqt QTableWidgetItem connect signal

I'm trying to make my QTableWidget call some function when I change the values in a cell.

self.table = QtGui.QTableWidget()  
tableItem = QtGui.QTableWidgetItem( str(attr.GetValue()) )
self.table.setItem(row, 1, tableItem )
QtCore.QObject.connect(self.table, QtCore.SIGNAL('currentItemChanged(QTableWidgetItem*, QTableWidgetItem*)'), someFunc)  

My problem is that it calls the function as soon as I enter the cell before I've even made value changes to it. Also it calls the function twice for some weird reason.

Upvotes: 1

Views: 9272

Answers (1)

jdi
jdi

Reputation: 92559

I think you are using the wrong signal. currentItemChanged refers to selection. Not for when the data changes. Try using itemChanged:

self.table.itemChanged.connect(someFunc) 

Notice also that I am using the new-style signal slots that were introduced back in Qt 4.5. You don't have to go to all that trouble anymore of specifying the C++ signature.

As for your signal firing multiple times, it either was because it was firing every time the selection changed and you didn't realize it, or you managed to connect it more than once.

For reference, the old style syntax for connecting this signal is:

QtCore.QObject.connect(
    self.table, 
    QtCore.SIGNAL('itemChanged(QTableWidgetItem*)'), 
    someFunc
) 

Upvotes: 4

Related Questions