简体   繁体   中英

How to react on a click in PyQt4 QTreeWidget

I am new to Pyhthon and Qt, hence this basic question. What I want is that when I click an item in a QTreeWidget an event handler is called which tells me which item has been clicked. The code I tried is:

    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) 

This does not run, the error code is:

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'

What am I doing wrong? And a question related to this: how can I figure out which item was clicked?

I could not find a tutorial for this, any suggestion is welcome.

Thanks for any help.

Question asked too early, I found the answer after a lot of experimenting. The code was wrong in not mentioning self.dirtree and connecting from self.dir_tree instead of self. So the correct code should be:

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

And some experimenting led to the following callback:

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

Item refers to the clicked QTreeWidgetItem itself (in my case it is a derived class with extra information: still works fine) and column to the clicked column.

The last question still stands. I have still not a good grasp of signals/slots. Any pointer to a good tutorial is welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM