我在Qt Designer MainWindow Form和WidgetForm中创建了。 在MainWindow Form中,我添加了Mdi Area。 如何在单击菜单选项后在MdiArea中打开我的设备窗口小部件窗体?
我的main.py
from windows.devices import Ui_Form_device
class Ui_MainWindow(object):
def Devices(self):
self.Form_device = QtWidgets.QWidget()
self.ui = Ui_Form_device()
self.ui.setupUi(self.Form_device)
self.Form_device.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(823, 681)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.centralwidget.setLayout(QtWidgets.QVBoxLayout(self.centralwidget))
self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
self.centralwidget.layout().addWidget(self.mdiArea)
...
# menu : DEVICES
self.actionDevices = QtWidgets.QAction(MainWindow)
self.actionDevices.setObjectName("actionDevices")
self.actionDevices.triggered.connect(self.Devices)
...
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
devices.py
class Ui_Form_device(object):
def setupUi(self, Form_device):
Form_device.setObjectName("Form_device")
Form_device.resize(400, 300)
self.label = QtWidgets.QLabel(Form_device)
self.label.setGeometry(QtCore.QRect(100, 150, 111, 16))
答案 0 :(得分:1)
请按以下方式致电mdiArea.addSubWindow()
:
def Devices(self):
self.Form_device = QtWidgets.QWidget()
self.ui = Ui_Form_device()
self.ui.setupUi(self.Form_device)
self.mdiArea.addSubWindow(self.Form_Device)
答案 1 :(得分:1)
建议不要修改使用Qt Designer进行设计生成的类,因为如果对设计进行任何更改,您将覆盖丢失逻辑部分更改的代码。您必须创建一个继承自Qt Designer中所选小部件的类,并使用初始类填充它。
查看以下内容以获取更多建议:
要做到这一点,你应该做的是使用addSubWindow()
方法,这会返回QMdiSubWindow
,你应该展示这个新小部件。
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
...
class DevicesForm(QtWidgets.QWidget, Ui_Form_device):
def __init__(self, *args, **kwargs):
QtWidgets.QWidget.__init__(self, *args, **kwargs)
self.setupUi(self)
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.actionaction.triggered.connect(self.add_devices)
def add_devices(self):
w = DevicesForm(self)
sub = self.mdiArea.addSubWindow(w)
w.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())