Issue
I tried to display a widget inside an mdiArea in pyqt5. It shows a form, but no content inside the form. This is the relevant code. I tried the separate people_display and it worked just fine. Does anyone know if it is even possible to uic.loadUI into a widget inside the mdiArea? people_display.ui just contains a TableView and a button box. main_window.ui is just a menu bar and mdiArea in the middle. I used the qt Designer to create the files. I wanted to keep the files separate to make it easier to edit them later when I continue with my project and want to make changes to them.
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_())
I modified the code for testing purposes.
Solution
I have tested your code and just needed to show the window:
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()
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.