1

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.

JLYK
  • 391
  • 9
  • 22

1 Answers1

4

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
) 
jdi
  • 90,542
  • 19
  • 167
  • 203
  • So tried the line you suggested and I'm getting an AttributeError: itemChanged. – JLYK Jun 26 '12 at 17:32
  • @JLYK: What version of PyQt are you running? – jdi Jun 26 '12 at 17:34
  • pyqt4. I just changed the line you gave me to QtCore.QObject.connect(self.table, QtCore.SIGNAL('itemChanged(QTableWidgetItem*)'), someFunc). It seems to work on the most part. Still calls my function multiple times but I guess that's a different set of problems. – JLYK Jun 26 '12 at 18:21
  • "pyqt4" is not a version. run this: `python -c 'from PyQt4 import QtCore; print QtCore.PYQT_VERSION_STR'`. And yea it would be firing more than once if your code is calling it multiple time. Where do you make this connection in the first place? – jdi Jun 26 '12 at 18:36
  • Ah...sorry. I'm using version 4.4.3 – JLYK Jun 26 '12 at 18:44
  • That is a really old version. Any reason you are bound to that one? At least use something 4.5+. They have 4.9+ now – jdi Jun 26 '12 at 18:47
  • Sorry I can't change the version of it as I'm not working on my personal machine. – JLYK Jun 26 '12 at 18:48
  • Well then I guess you have to stick to older syntaxis. You can always build in your homedir – jdi Jun 26 '12 at 18:49
  • Here's an extension of my above problem I described if you care to have a look: http://stackoverflow.com/questions/11213914/pyqt4-runtimeerror-underlying-c-c-object-has-been-deleted – JLYK Jun 26 '12 at 18:55
  • @JLYK: These two questions definitely overlap. I have answered why you are getting multiple signal firing in your other one, and I believe this question is all solved as well. – jdi Jun 26 '12 at 19:33