2013-07-03 30 views
0

我是Pyhthon和Qt的新手,因此这个基本问题。我想要的是,当我点击QTreeWidget中的一个项目时,会调用一个事件处理程序,它告诉我哪个项目已被点击。我试过的代码是:如何在PyQt4的点击反应QTreeWidget

self.dir_tree = QTreeWidget() 
    self.dir_tree.setColumnCount (3) 
    self.dir_tree.setHeaderLabels (("File", "Type", "Size")) 
    self.dir_tree.connect (dir_tree, SIGNAL ("itemClicked (QTreeWidgetItem*, int)"), self.onClickItem) 

def onClickItem (self, column): 
    print (column) 

这不运行,错误代码是:

TypeError: arguments did not match any overloaded call: 
    QObject.connect(QObject, SIGNAL(), QObject, SLOT(),Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'function' 
    QObject.connect(QObject, SIGNAL(), callable, Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'function' 
    QObject.connect(QObject, SIGNAL(), SLOT(), Qt.ConnectionType=Qt.AutoConnection): argument 1 has unexpected type 'function' 

我在做什么错?还有一个与此有关的问题:我怎么才能找出哪个项目被点击?

我找不到这个教程,任何建议是值得欢迎的。

感谢您的任何帮助。

回答

1

问题问得太早,经过很多实验后我找到了答案。该代码在提到self.dirtree和从self.dir_tree而不是self自动连接时是错误的。所以,正确的代码应该是:

self.connect (self.dir_tree, SIGNAL ("itemClicked(QTreeWidgetItem*, int)"), self.onClickItem) 

和一些实验导致了以下回调:

def onClickItem (self, item, column): 
    print (item, column) 

项是指点击QTreeWidgetItem本身(在我的情况下,额外的信息派生类:仍然正常工作)并列到点击列。

最后一个问题依然存在。我仍然没有很好的掌握信号/插槽。任何指向优秀教程的指针都是受欢迎的。