使用pyqt4在设计的qtdesigner ui中添加treeview

时间:2016-03-09 18:57:03

标签: python qt qt-designer qtreeview

我是Qt的新手,我正在使用python。我用qtdesigner创建了一个.ui文件,只有一个简单的Tree View对象。我使用pyui.bat将.ui文件转换为.py,然后我提升了Tree视图 这是我的ui代码:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.treeView = Main(self.centralwidget)
        self.treeView.setGeometry(QtCore.QRect(300, 180, 256, 192))
        self.treeView.setObjectName(_fromUtf8("treeView"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))

from QTree import Main

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

这是我的树查看代码:

from PyQt4 import QtGui
from PyQt4 import QtCore

class Main(QtGui.QTreeView):

  def __init__(self):

    QtGui.QTreeView.__init__(self)
    model = QtGui.QFileSystemModel()
    model.setRootPath( QtCore.QDir.currentPath() )
    self.setModel(model)
    QtCore.QObject.connect(self.selectionModel(), QtCore.SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.test)

  @QtCore.pyqtSlot("QItemSelection, QItemSelection")
  def test(self, selected, deselected):
      print("hello!")
      print(selected)
      print(deselected)

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    w = Main()
    w.show()
    sys.exit(app.exec_())

但是当我尝试运行转换后的ui文件时,我收到错误:

E:\Python27\python.exe C:/Users/Amirmohammad/Desktop/proje/testagain.py
Traceback (most recent call last):
  File "C:/Users/Amirmohammad/Desktop/proje/testagain.py", line 56, in <module>
    ui.setupUi(MainWindow)
  File "C:/Users/Amirmohammad/Desktop/proje/testagain.py", line 31, in setupUi
    self.treeView = Main(self.centralwidget)
TypeError: __init__() takes exactly 1 argument (2 given)

当我尝试将QWidget_parent = None添加到我的Main类init时,运行应用程序后我得到一个空白窗口!
在一个句子中,我想创建一个文件资源管理器,我想在一个用qtdesigner创建的漂亮的ui中使用TreeView(第二个代码)。

1 个答案:

答案 0 :(得分:1)

有两个问题:

  1. 您需要在Qt Designer
  2. 中的顶级窗口小部件上设置布局
  3. 您需要向parent添加Main.__init__参数,理想情况下,将此参数传递给QTreeView.__init__