我在 PyQt5 中使用 QMdiArea 时遇到问题,以下是我所做的:
我发现使用 uic 工具生成的相应 .py 代码无法为 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")
如果我将关键行代码更改为:
self.subwindow = QtWidgets.QWidget(self.mdiArea)
然后我可以在 mdiarea 中看到子窗口,但它看起来很奇怪:
这是简单的演示:
- 使用 uic 工具生成的 .py 代码是 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"))
- 主要代码是:
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_())