pyqt5 mdiArea显示小部件不显示内容

时间:2019-01-10 14:15:18

标签: python pyqt pyqt5

我试图在pyqt5的mdiArea中显示一个小部件。它显示一个表单,但表单内没有内容。这是相关的代码。我尝试了单独的people_display,但效果很好。有谁知道uic.loadUI甚至可以进入mdiArea内部的小部件吗? people_display.ui仅包含一个TableView和一个按钮框。 main_window.ui只是一个菜单栏,中间是mdiArea。我使用qt Designer创建文件。我希望将文件分开放置,以使以后在继续我的项目并对其进行更改时更容易编辑它们。

from PyQt5 import QtWidgets, uic, Qt
from PyQt5.QtWidgets import QMainWindow, QWidget, QDialog
from cal.person import Person
import sys


class Main(QMainWindow):

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        uic.loadUi('main_window.ui', self)
        self.menuAddPeople.triggered.connect(self.show_add_people)
        self.menuShowPeople.triggered.connect(self.show_show_people)

    def show_add_people(self):
        dialog = AddPeople(self)
        dialog.show()

    def show_show_people(self):
        widget = uic.loadUi('people_display.ui')
        """
        people_list = Person.person_list()
        model = Qt.QStandardItemModel()
        first_names = []
        for item in people_list:
            first_names.append(Qt.QStandardItem(item['first_name']))
        last_names = []
        for item in people_list:
            last_names.append(Qt.QStandardItem(item['last_name']))
        emails = []
        for item in people_list:
            emails.append(Qt.QStandardItem(item['email']))
        roles = []
        for item in people_list:
            roles.append(Qt.QStandardItem(item['role_id']))
        model.appendColumn(first_names)
        model.appendColumn(last_names)
        model.appendColumn(emails)
        model.appendColumn(roles)
        widget.peopleTableView.setModel(model)
        """
        model = Qt.QStandardItemModel()
        model.appendRow(Qt.QStandardItem('asdf'))
        widget.peopleTableView.setModel(model)
        self.mdiArea.addSubWindow(widget)
        print(self.mdiArea.subWindowList())


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())

我出于测试目的修改了代码。

1 个答案:

答案 0 :(得分:0)

我已经测试了您的代码,只需要显示窗口即可:

def show_show_people(self):
    widget = uic.loadUi('people_display.ui')
    # people_list = Person.person_list()
    # for testing
    people_list = [{"first_name": "f1", "last_name": "l1", "email": "e1", "role_id": "r1"},
                   {"first_name": "f2", "last_name": "l2", "email": "e2", "role_id": "r2"},
                   {"first_name": "f3", "last_name": "l3", "email": "e3", "role_id": "r3"}]


    model = Qt.QStandardItemModel(widget)
    for item in people_list:
        it_first_name = Qt.QStandardItem(item['first_name'])
        it_last_name = Qt.QStandardItem(item['last_name'])
        it_email = Qt.QStandardItem(item['email'])
        it_role_id = Qt.QStandardItem(item['role_id'])
        model.appendRow([it_first_name, it_last_name, it_email, it_role_id])
    widget.peopleTableView.setModel(model)
    self.mdiArea.addSubWindow(widget)
    widget.show()

enter image description here