4

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

1 回答 1

10

你应该说

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

请注意,它在第一个参数中表示int而不是columnSIGNAL。您还只需connect为树小部件调用一次,而不是为树中的每个节点调用一次。

于 2009-07-13T13:20:59.173 回答