简体   繁体   English

pyqt5对话框未显示任何小部件

[英]pyqt5 dialog not showing any widgets

I have created a dialog with Qt Creator and then translated the .ui file to .py file with 我已经使用Qt Creator创建了一个对话框,然后将.ui文件转换为

 pyuic5 dialog.ui -o dialog.py

The resulting code is the following 结果代码如下

class Ui_dialog(object):
    def setupUi(self, dialog):
        dialog.setObjectName("dialog")
        dialog.resize(976, 725)
        self.verticalLayoutWidget = QtWidgets.QWidget(dialog)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(360, 210, 160, 80))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.pushButton)

        self.retranslateUi(dialog)
        QtCore.QMetaObject.connectSlotsByName(dialog)

    def retranslateUi(self, dialog):
        _translate = QtCore.QCoreApplication.translate
        dialog.setWindowTitle(_translate("dialog", "Dialog"))
        self.pushButton.setText(_translate("dialog", "PushButton"))

Now I'm trying to display the dialog from my main window with 现在我试图从主窗口显示对话框

dialog = QDialog()
dialog.ui = Ui_dialog()
dialog.ui.setupUi(self)
dialog.show()
dialog.exec_()

The dialog is shown, but it's empty so there's no button or any other widgets I tried!? 对话框显示了出来,但是它是空的,所以没有按钮或我尝试过的任何其他小部件!

Ui_Dialog's setupUi method requires a widget to fill in, and in your case you should change the following: Ui_Dialog的setupUi方法需要一个小部件来填充,在您的情况下,您应该更改以下内容:

dialog.ui.setupUi(self)

to: 至:

dialog.ui.setupUi(dialog)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM