显示QComboBox的QtWidget出现空白

时间:2017-02-09 08:19:40

标签: python pyqt pyqt5

我正在编写一个使用PyQt5显示QComboBox(下拉菜单)的QtWidget。然而,它变得空洞。有谁知道为什么?

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class Test(QtWidgets.QWidget):
    def __init__(self, formname = "Test"):
        super().__init__()
        self.formname = formname
        self.impute_methods = ["Method 1", "Method 2"]

    def setupUi(self, Form):
        Form.setObjectName(self.formname)
        Form.resize(1154, 902)
        self._create_buttons(Form)
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def _create_buttons(self, Form):
        self.test_box = QtWidgets.QComboBox()
        self.test_box.addItems(self.impute_methods)
        self.test_box.setGeometry(QtCore.QRect(110, 190, 150, 27))
        self.test_box.setObjectName("test_box")

    def selectionchange(self, i):
        print(i)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate(self.formname, "Test"))

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    tester = QtWidgets.QDialog()
    ui = Test()
    ui.setupUi(tester)
    tester.show()
    sys.exit(app.exec_())

以下是我看到的截图:

enter image description here

1 个答案:

答案 0 :(得分:0)

将comboBox的父级设置为Test()

def _create_buttons(self, Form):
    self.test_box = QtWidgets.QComboBox(self)
    ...

并将ui放在对话框上:

...
ui = Test()
ui.setParent(tester)
...