tags:

views:

444

answers:

1

I'm a Python newbie and I'm trying to write a trivial app with an event handler that gets activated when an item in a custom QTreeWidget is clicked. For some reason it doesn't work. Since I'm only at the beginning of learning it, I can't figure out what I'm 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 make MyTree.onClick() print something when I click a tree item (and have access to the clicked item in this handler).

+4  A: 

You should have said

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

Notice it says int rather than column in the first argument to SIGNAL. You also only need to do the connect call once for the tree widget, not once for each node in the tree.

Vinay Sajip
Thank you, it worked! For some reason my browser won't allow me to mark your answer as accepted, but it works.