将组合框添加到主窗口

add combobox to mainwindow

我用 pyqt5 创建了一个带有 2 个按钮的 Main window,现在我想向它添加一个组合框。但如果我保留 App(QMainWindow),组合框将不会显示。只有当我写 App(QWidgets) 它才会显示。有没有办法向 main window 添加组合框?这是我的代码:

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'Phan mem quan ly tai lieu- Tuan tien mom'
        self.left = 200
        self.top = 200
        self.width = 320
        self.height = 200
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        button = QPushButton('chaychuongtrinh', self)
        button.setToolTip('bam vao nut nay de chay chuong trinh')
        button.move(100, 70)
        button.clicked.connect(self.on_click)

        button1 = QPushButton('kiemtra', self)
        button1.setToolTip('kiem tra thong tin')
        button1.move(200, 70)
        button1.clicked.connect(self.on_click)

        layout = QHBoxLayout()
        self.cb = QComboBox()
        self.cb.addItem("C")
        self.cb.addItem("C++")
        self.cb.addItems(["Java", "C#", "Python"])
        self.cb.currentIndexChanged.connect(self.selectionchange)
        layout.addWidget(self.cb)
        self.setLayout(layout)
        self.setWindowTitle("combo box demo")
        self.show()
    def selectionchange(self,i):
        print ("Items in the list are :")
        print(self.cb.currentText())

每个小部件都需要 parent

对于 QPushButton,您将 self 用作 parent。对于 QComboBox 也使用 self 作为 parent

self.cb = QComboBox(self)

QMainWindow是一个特殊的widget,它已经有一个布局,所以我们不应该再添加另一个布局,如图:

在这种情况下,您必须创建一个新的小部件并将其放置在中央小部件中,在这个新的小部件中,我们必须放置像 QPushButton 或 QComboBox 这样的小部件。

在你的例子中,QPushButton 是可见的,因为如果一个小部件传递了父级,它将位于父级的位置 (0, 0),然后你将它移动到特定位置move()。相反,组合框没有父项,因此不可见。

此外,如果您要使用布局,则不应使用 move () 函数,因为布局会扩展到整个小部件。在下面的例子中没有使用布局:

def initUI(self):
    self.setWindowTitle(self.title)
    self.setGeometry(self.left, self.top, self.width, self.height)
    self.widget = QWidget(self)
    self.setCentralWidget(self.widget)
    button = QPushButton('chaychuongtrinh', self.widget)
    button.move(100, 70)
    button.setToolTip('bam vao nut nay de chay chuong trinh')
    button.clicked.connect(self.on_click)
    button1 = QPushButton('kiemtra', self.widget)
    button1.setToolTip('kiem tra thong tin')
    button1.clicked.connect(self.on_click)
    button1.move(200, 70)
    self.cb = QComboBox(self.widget)
    self.cb.addItem("C")
    self.cb.addItem("C++")
    self.cb.addItems(["Java", "C#", "Python"])
    self.cb.currentIndexChanged.connect(self.selectionchange)
    self.cb.move(300, 70)
    self.setWindowTitle("combo box demo")

布局:

def initUI(self):
    self.setWindowTitle(self.title)
    self.setGeometry(self.left, self.top, self.width, self.height)
    self.widget = QWidget(self)
    self.setCentralWidget(self.widget)
    layout = QHBoxLayout(self.widget)
    layout.addItem(QSpacerItem(139, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
    vlay = QVBoxLayout()
    vlay.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
    layout.addLayout(vlay)
    layout.addItem(QSpacerItem(139, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
    button = QPushButton('chaychuongtrinh', self)
    button.setToolTip('bam vao nut nay de chay chuong trinh')
    button.clicked.connect(self.on_click)
    button1 = QPushButton('kiemtra', self)
    button1.setToolTip('kiem tra thong tin')
    button1.clicked.connect(self.on_click)
    self.cb = QComboBox(self)
    self.cb.addItem("C")
    self.cb.addItem("C++")
    self.cb.addItems(["Java", "C#", "Python"])
    self.cb.currentIndexChanged.connect(self.selectionchange)
    vlay.addWidget(button)
    vlay.addWidget(button1)
    vlay.addWidget(self.cb)
    vlay.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
    self.setWindowTitle("combo box demo")
    self.show()