I have a problem when using QMdiArea in PyQt5, the following is what I have done:
However, when I use pyqt5_uic to convert the .ui file to .py file, and run the code, it looks like this:
I found that the corresponding .py code generate with uic tools fails to set parent for subwindows in mdiarea:
self.mdiArea = QtWidgets.QMdiArea(IOWidgets)
self.mdiArea.setGeometry(QtCore.QRect(555, 120, 421, 181))
self.mdiArea.setObjectName("mdiArea")
self.subwindow = QtWidgets.QWidget()
self.subwindow.setObjectName("subwindow")
self.lineEdit_1_IO = QtWidgets.QLineEdit(self.subwindow)
self.lineEdit_1_IO.setGeometry(QtCore.QRect(20, 20, 61, 21))
self.lineEdit_1_IO.setObjectName("lineEdit_1_IO")
self.spinBox_1_IO = QtWidgets.QSpinBox(self.subwindow)
self.spinBox_1_IO.setGeometry(QtCore.QRect(30, 60, 42, 22))
self.spinBox_1_IO.setObjectName("spinBox_1_IO")
if I change the critical line code to:
self.subwindow = QtWidgets.QWidget(self.mdiArea)
then I can see the subwindow in mdiarea, but it looks strange:
here is simple demo:
- the .py code generate with uic tools is Ui_Mdi_simple.py:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MDI_test(object):
def setupUi(self, MDI_test):
MDI_test.setObjectName("MDI_test")
MDI_test.resize(545, 336)
self.mdiArea = QtWidgets.QMdiArea(MDI_test)
self.mdiArea.setGeometry(QtCore.QRect(70, 50, 331, 221))
self.mdiArea.setObjectName("mdiArea")
self.subwindow = QtWidgets.QWidget()
self.subwindow.setObjectName("subwindow")
self.comboBox = QtWidgets.QComboBox(self.subwindow)
self.comboBox.setGeometry(QtCore.QRect(50, 30, 68, 22))
self.comboBox.setObjectName("comboBox")
self.lineEdit = QtWidgets.QLineEdit(self.subwindow)
self.lineEdit.setGeometry(QtCore.QRect(50, 70, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.radioButton = QtWidgets.QRadioButton(self.subwindow)
self.radioButton.setGeometry(QtCore.QRect(50, 110, 95, 20))
self.radioButton.setObjectName("radioButton")
self.subwindow_2 = QtWidgets.QWidget()
self.subwindow_2.setObjectName("subwindow_2")
self.retranslateUi(MDI_test)
QtCore.QMetaObject.connectSlotsByName(MDI_test)
def retranslateUi(self, MDI_test):
_translate = QtCore.QCoreApplication.translate
MDI_test.setWindowTitle(_translate("MDI_test", "Form"))
self.subwindow.setWindowTitle(_translate("MDI_test", "subwindow_1"))
self.radioButton.setText(_translate("MDI_test", "RadioButton"))
self.subwindow_2.setWindowTitle(_translate("MDI_test", "subwindow_2"))
- the main code is:
import sys
from PyQt5.QtWidgets import QWidget, QApplication
from Ui_Mdi_simple import Ui_MDI_test
class MDI_demo(QWidget, Ui_MDI_test):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MDI_demo()
win.show()
sys.exit(app.exec_())
I found adding the following two lines of codes in Ui_MDI_test solves the problem:
@ eyllanesc, thanks.