简体   繁体   English

PyQt5 QMainWindow 不显示中央小部件

[英]PyQt5 QMainWindow not displaying central widget

I want to have a central Widget with a grid layout containing multiple other widgets.我想要一个中央小部件,其网格布局包含多个其他小部件。

the problem is that the central widget is not showing on QMainWindow even after using setCentralWidget function.问题是即使在使用 setCentralWidget function 之后,中央小部件也没有显示在 QMainWindow 上。

here is the code that is not working, i can't find the error (edit: there was no exceptions raised, just the fact i couldn't see the widgets)这是不起作用的代码,我找不到错误(编辑:没有引发异常,只是我看不到小部件的事实)

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel, QGridLayout

class Board(QWidget):
    def __init__(self):
        super().__init__()


Clock(QWidget):
    def __init__(self):
        super().__init__()

class MainGrid(QWidget):

    def __init__(self):
        super().__init__()
        self.initGrid()

    def initGrid(self):
        grid= QGridLayout()

        test = QLabel('test')
        board = Board()
        clock = Clock()
        board.setStyleSheet('background-color: pink')
        clock.setStyleSheet('background-color: blue')

        grid.addWidget(board, 2, 1, 10, 10)
        grid.addWidget(clock, 13, 4, 3, 3)

        self.setLayout(grid)


class MainWin(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        centralGrid = MainGrid()
        centralGrid.setStyleSheet('background-color: red')
        centralGrid.sizeHint()
        self.setCentralWidget(centralGrid)

        self.setGeometry(200, 100, 1000, 600)
        self.setWindowTitle('Simple Checkers')
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = MainWin()
    sys.exit(app.exec_())

edit: thanks to scheff answer i think i found where i went wrong.编辑:感谢 scheff 的回答,我想我找到了哪里出错了。 to visualize the widgets i changed their backgrounds using setStyleSheet function, on Qt Documentation:为了可视化小部件,我在 Qt 文档中使用 setStyleSheet function 更改了它们的背景:

Note: If you subclass a custom widget from QWidget, then in order to use the StyleSheets you need to provide a paintEvent to the custom widget:注意:如果您从 QWidget 子类化自定义小部件,那么为了使用 StyleSheets,您需要为自定义小部件提供一个paintEvent:

as for the test label i used it for further testing but forgot to add it to the grid layout which added even more confusion.至于测试 label,我将其用于进一步测试,但忘记将其添加到网格布局中,这增加了更多的混乱。

Unfortunately, the OP claimed that不幸的是,OP声称

the problem is that the central widget is not showing on QMainWindow even after using setCentralWidget function.问题是即使在使用 setCentralWidget function 之后,中央小部件也没有显示在 QMainWindow 上。

without elaborating in detail.没有详细阐述。

I had a rough look onto the source and came to the conclusion that我粗略地查看了源代码并得出的结论是

  • widgets have been added to layout小部件已添加到布局中
  • layout is set to widget布局设置为小部件
  • the widget has been set to QMainWindow .该小部件已设置为QMainWindow

So far so fine.到目前为止一切顺利。

Then I copied the complete source of OP to my local box.然后我将 OP 的完整源代码复制到我的本地盒子中。

To make it running I had to add/modify a variety of things:为了让它运行,我必须添加/修改各种东西:

  1. All Qt imports were missing.缺少所有 Qt 导入。 I added我添加了

    from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import *
  2. For sys.argv (in app = QApplication(sys.argv) ), import sys is needed as well.对于sys.argv (在app = QApplication(sys.argv)中),也需要import sys

  3. The widgets Board and Clock were missing.小部件BoardClock丢失了。

     #board = Board() #clock = Clock() clock = QLabel('Clock') #board.setStyleSheet('background-color: pink')
  4. The test = QLabel('test') wasn't added to the grid layout. test = QLabel('test')未添加到网格布局中。

     grid.addWidget(test, 2, 1, 10, 10)

After having fixed all of this, the (modified) source was this:在修复了所有这些之后,(修改后的)来源是这样的:

#!/usr/bin/python3

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class MainGrid(QWidget):

    def __init__(self):
        super().__init__()
        self.initGrid()

    def initGrid(self):
        grid= QGridLayout()

        test = QLabel('test')
        #board = Board()
        #clock = Clock()
        clock = QLabel('Clock')
        #board.setStyleSheet('background-color: pink')
        test.setStyleSheet('background-color: pink')
        clock.setStyleSheet('background-color: blue')

        grid.addWidget(test, 2, 1, 10, 10)
        grid.addWidget(clock, 13, 4, 3, 3)

        self.setLayout(grid)


class MainWin(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        centralGrid = MainGrid()
        centralGrid.setStyleSheet('background-color: red')
        centralGrid.sizeHint()
        self.setCentralWidget(centralGrid)

        self.setGeometry(200, 100, 1000, 600)
        self.setWindowTitle('Simple Checkers')
        self.show()

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    gui = MainWin()
    sys.exit(app.exec_())

Note:笔记:

I added the "hut" in the first line我在第一行添加了“小屋”

#!/usr/bin/python3

for my own convenience.为了我自己的方便。

Then I ran it in cygwin64 (because I only had Windows 10 with cygwin at hand):然后我在cygwin64中运行它(因为我手头只有 Windows 10 和 cygwin):

$ chmod a+x testQMainWindowCentralWidget.py

$ ./testQMainWindowCentralWidget.py 

and got:并得到:

./testQMainWindowCentralWidget.py 的快照

Now, the QMainWindow.setCentralWidget() works as expected.现在, QMainWindow.setCentralWidget()按预期工作。

I don't know which issues the OP actually ran in.我不知道 OP 实际遇到了哪些问题。

I'm not sure whether the exposed code of OP was the exact copy/paste and the missing details were the actual source of OP's problems.我不确定 OP 的公开代码是否是确切的复制/粘贴,而丢失的细节是否是 OP 问题的实际来源。

When I tried to make it running I carefully considered the trace-backs I got in the first attempts and fixed the bugs step-by-step respectively until I got the above result.当我试图让它运行时,我仔细考虑了我在第一次尝试中得到的回溯,并分别逐步修复了错误,直到得到上述结果。

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

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