Layout PyQt5对话框窗口将打开,但不显示布局
当我从主窗口(QMainWindow)中加载对话框(QMainWindow)窗口时,即使调用了setupUi()函数,它也不会加载布局 下面是重要的代码片段,用于粘贴到完整代码的链接Layout PyQt5对话框窗口将打开,但不显示布局,layout,dialog,pyqt5,python-3.6,Layout,Dialog,Pyqt5,Python 3.6,当我从主窗口(QMainWindow)中加载对话框(QMainWindow)窗口时,即使调用了setupUi()函数,它也不会加载布局 下面是重要的代码片段,用于粘贴到完整代码的链接 class Ui_Dialog(QMainWindow): def __init__(self, parent=None): super(Ui_Dialog, self).__init__(parent) self.setupUi(self) def setupUi(
class Ui_Dialog(QMainWindow):
def __init__(self, parent=None):
super(Ui_Dialog, self).__init__(parent)
self.setupUi(self)
def setupUi(self, Dialog):
...
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setupUi(self)
self.show()
....
def setupUi(self, Form):
...
self.auto_sap_btn = QPushButton(Form)
self.auto_sap_btn.setGeometry(QRect(0, 0, 61, 25))
self.auto_sap_btn.setObjectName('auto_sap_btn')
self.auto_sap_btn.clicked.connect(self.openDialog)
def openDialog(self):
self.window = Ui_Dialog(self)
self.window.setupUi(self.window)
self.window.move(600, 500)
self.window.show()
现在,我的对话框如下所示:
对话框布局失败
当我从对话框自己创建的脚本加载对话框时:
pyuic5 -x dialog.ui -o dialog.py
看起来是这样的:
正确的对话框布局
我缺少什么?当您在Qt Designer中基于模板创建设计时,当您必须传递相应的小部件时,当您创建Ui\u对话框时,您肯定使用了按钮右侧的
对话框
,因此在这种情况下,您应该使用QDialog而不是QMainWindow:
class Ui_Dialog(QDialog): # change QMainWindow to QDialog
def __init__(self, parent=None):
super(Ui_Dialog, self).__init__(parent)
self.setupUi(self)
[...]
另一个错误是第二次使用setupUi()
方法,因为该方法负责填充小部件,如果调用它两次,您将不必要地添加更多小部件:
def openDialog(self):
self.window = Ui_Dialog(self)
self.window.move(600, 500)
self.window.show()
你完全正确。我完全错过了第二次运行setupUi。我遵循了本教程(),他使用了QMainWindow,但在将其更改为QDialog之后,它的工作方式就像一个符咒。谢谢你的帮助!