[PyQt] Using a protected slot (columnResized)

Phil Thompson phil at riverbankcomputing.co.uk
Fri Dec 28 12:42:49 GMT 2007


On Thursday 27 December 2007, Andreas Pakulat wrote:
> On 27.12.07 15:27:20, Noam Raphael wrote:
> > 2007/12/26, Andreas Pakulat <apaku at gmx.de>:
> > > Then you need to provide a minimal self-contained example that
> > > demonstrates the problem.
> >
> > No problem at all:
> >
> > http://python.pastebin.com/f1ac4e258
> >
> > It creates a table which does nothing when you resize a column...
>
> Hmm, it works as soon as you put the method into a subclass of
> QTableView. So either this is not supported by PyQt4, or there's a bug.

It's a SIP bug. In effect the "connection" is associated with the Python 
wrapper of the emitter, rather than the emitter itself. When the wrapper is 
garbage collected the connection is lost.

The workaround is to keep a reference to object returned by 
horizontalHeader(). The fix will be in tonight's SIP snapshot.

> If Phil doesn't come back to this thread until sometime in early january
> I suggest to send a new mail with the above example and this code which
> works fine:
>
> ,----[ testtab.py ]-
>
> | #!/usr/bin/env python
> |
> | import sys
> | from PyQt4 import QtCore, QtGui
> | from PyQt4.QtCore import Qt
> |
> |
> |
> | class MyWidget(QtGui.QTableView):
> | 	def __init__(self):
> | 		QtGui.QTableView.__init__(self, None)
> | 		self.connect(self.horizontalHeader(),
> | QtCore.SIGNAL("sectionResized(int,int,int)"), self.columnResized) def
> | columnResized(*args):
> | 		print args
> |
> |
> | def main():
> |     app = QtGui.QApplication(sys.argv)
> |
> |     model = QtGui.QStandardItemModel(3,3)
> |     tableView = MyWidget()
> |     tableView.setModel(model)
> |     tableView.show()
> |     sys.exit(app.exec_())
> |
> | if __name__ == '__main__':
> |     main()
>
> `----

Given this explanation I think it's just luck (ie. when the cyclic garbage 
collector happens to be invoked) that the above seems to work.

Phil


More information about the PyQt mailing list