向qtDesigner UI的基于树的系统添加功能

时间:2014-02-05 05:49:22

标签: python function qt-designer

我使用Qt4设计工具设计了这个UI。它是一个树系统。问题是我无法单独为每个项目提供功能。当我单击Install时,它应该运行myfunc,当单击Uninstall时,myfuncUninstall应该运行..那么如何才能将这些项的功能单独提供给基于树的系统。

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_())

1 个答案:

答案 0 :(得分:1)

您将treeWidget的{​​{1}}信号连接到itemActivated

myfunc

这意味着,每当您在树窗口小部件的任何节点上双击,或者按任意节点时,任何节点都会聚焦,它将打印出来&#34 ; Hello Install"在你的控制台中。

您最好将QtCore.QObject.connect(self.treeWidget, QtCore.SIGNAL(_fromUtf8("itemActivated(QTreeWidgetItem*,int)")), myfunc) itemClicked方法联系起来,并像这样实施myfunc

myfunc