使用 pyqt4 在设计的 qtdesigner ui 中添加树视图

adding treeview in a designed qtdesigner ui with pyqt4

我是 Qt 中的新手,我使用的是 python。我用 qtdesigner 制作了一个 .ui 文件,只有一个简单的树视图对象。我使用 pyui.bat 将 .ui 文件转换为 .py 并提升了树视图。
这是我的 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 class init 时,在 运行 应用程序之后我得到一个空白 window !
一句话,我想做一个文件浏览器,我想在用 qtdesigner 创建的漂亮 ui 中使用 TreeView(第二个代码)。

有两个问题:

  1. 您需要在 Qt Designer 中设置顶层小部件的布局
  2. 您需要向 Main.__init__ 添加一个 parent 参数,并且理想情况下,将此参数传递给 QTreeView.__init__