Question

I have designed this UI with Qt4 Designing tool. Its a tree system. Problem is I cant give functionality to each item individually. When I click Install it should run myfunc and when Uninstall is clicked myfuncUninstall should run.. So how can I give functionality to the items individually to tree based system.

from PyQt4 import QtCore, QtGui
try:
  _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
  _fromUtf8 = lambda s: s
def myfunc():
  print "Hello Install"  
def myfuncUninstall():
  print "Hello Uninstall"    
class Ui_Form(object):
  def setupUi(self, Form):
    Form.setObjectName(_fromUtf8("Form"))
    Form.resize(502, 409)
    self.treeWidget = QtGui.QTreeWidget(Form)
    self.treeWidget.setGeometry(QtCore.QRect(10, 10, 261, 341))
    self.treeWidget.setObjectName(_fromUtf8("treeWidget"))
    item_0 = QtGui.QTreeWidgetItem(self.treeWidget)
    item_1 = QtGui.QTreeWidgetItem(item_0)
    item_1 = QtGui.QTreeWidgetItem(item_0)
    self.lineEdit = QtGui.QLineEdit(Form)
    self.lineEdit.setGeometry(QtCore.QRect(290, 10, 113, 27))
    self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
    self.retranslateUi(Form)
    QtCore.QObject.connect(self.treeWidget, QtCore.SIGNAL(_fromUtf8("itemActivated(QTreeWidgetItem*,int)")), myfunc)
    QtCore.QMetaObject.connectSlotsByName(Form)
  def retranslateUi(self, Form):
    Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
    self.treeWidget.headerItem().setText(0, QtGui.QApplication.translate("Form", "BBB", None, QtGui.QApplication.UnicodeUTF8))
    __sortingEnabled = self.treeWidget.isSortingEnabled()
    self.treeWidget.setSortingEnabled(False)
    self.treeWidget.topLevelItem(0).setText(0, QtGui.QApplication.translate("Form", "Install Manager", None, QtGui.QApplication.UnicodeUTF8))
    self.treeWidget.topLevelItem(0).child(0).setText(0, QtGui.QApplication.translate("Form", "1. Install", None, QtGui.QApplication.UnicodeUTF8))
    self.treeWidget.topLevelItem(0).child(1).setText(0, QtGui.QApplication.translate("Form", "2. UnInstall", None, QtGui.QApplication.UnicodeUTF8))
    self.treeWidget.setSortingEnabled(__sortingEnabled)
if __name__ == "__main__":
  import sys
  app = QtGui.QApplication(sys.argv)
  Form = QtGui.QWidget()
  ui = Ui_Form()
  ui.setupUi(Form)
  Form.show()
  sys.exit(app.exec_())
Was it helpful?

Solution

You connect your treeWidget's itemActivated SIGNAL to myfunc,

QtCore.QObject.connect(self.treeWidget, QtCore.SIGNAL(_fromUtf8("itemActivated(QTreeWidgetItem*,int)")), myfunc)

which means, each time you double click on any node of the tree widget, or press ENTER key when any node is focused, it will print out "Hello Install" in your console.

You'd better connect the itemClicked with your myfunc method, and implement myfunc like this:

def myfunc(item, n):
  # print "Hello Install" , item, n
  t=str(item.text(n)).lower()
  if t == '2. uninstall':
    print '----------uninstalling'
  elif t == '1. install':
    print '+++++++installing'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top