vandelay vandelay - 3 months ago 56
Python Question

pyqt QTreeWidgetItem double click connect

Is it possible to connect a doubleclick event to a QTreeWidgetItem?

Something like this:

def test(self):
print("hello")

childItem = QTreeWidgetItem()
childItem.doubleClicked.connect(self.test)

Answer

The signal you want is called itemDoubleClicked and belongs to QTreeWidget itself:

from PyQt4 import QtGui

def handler(item, column_no):
    print(item, column_no)

def main():
    app = QtGui.QApplication(sys.argv)
    win = QtGui.QTreeWidget()

    items = [QtGui.QTreeWidgetItem("item: {}".format(i)) for i in xrange(10)]
    win.insertTopLevelItems(0, items)
    win.itemDoubleClicked.connect(handler)

    win.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()