PyQt: the event does not fire, what is wrong with my code?

I'm new to Python, and I'm trying to write a trivial application with an event handler that fires when an element is clicked in a custom QTreeWidget. For some reason this does not work. Since I am only at the beginning of studying this, I cannot understand what I am doing wrong. Here is the code:

#!/usr/bin/env python import sys from PyQt4.QtCore import SIGNAL from PyQt4.QtGui import QApplication from PyQt4.QtGui import QMainWindow from PyQt4.QtGui import QTreeWidget from PyQt4.QtGui import QTreeWidgetItem class MyTreeItem(QTreeWidgetItem): def __init__(self, s, parent = None): super(MyTreeItem, self).__init__(parent, [s]) class MyTree(QTreeWidget): def __init__(self, parent = None): super(MyTree, self).__init__(parent) self.setMinimumWidth(200) self.setMinimumHeight(200) for s in ['foo', 'bar']: MyTreeItem(s, self) self.connect(self, SIGNAL('itemClicked(QTreeWidgetItem*, column)'), self.onClick) def onClick(self, item, column): print item class MainWindow(QMainWindow): def __init__(self, parent = None): super(MainWindow, self).__init__(parent) self.tree = MyTree(self) def main(): app = QApplication(sys.argv) win = MainWindow() win.show() app.exec_() if __name__ == '__main__': main() 

My initial goal is to get MyTree.onClick () to print something when I click on a tree element (and have access to the clicked element of this handler).

+4
source share
1 answer

You should have said

 self.connect(self, SIGNAL('itemClicked(QTreeWidgetItem*, int)'), self.onClick) 

Note that instead of the <column, the first argument <<21> specifies int . You also need to make the connect call once for the tree widget, and not once for each node in the tree.

+11
source

All Articles