1

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 .

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. to visualize the widgets i changed their backgrounds using setStyleSheet function, on Qt Documentation :

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 :

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 .

nzz coding
  • 47
  • 7

1 Answers1

0

Unfortunately, the OP claimed that

the problem is that the central widget is not showing on QMainWindow even after using setCentralWidget function .

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.

So far so fine.

Then I copied the complete source of OP to my local box.

To make it running I had to add/modify a variety of things:

  1. All Qt imports were missing. 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.

  3. The widgets Board and Clock were missing.

    #board = Board()
    #clock = Clock()
    clock = QLabel('Clock')
    #board.setStyleSheet('background-color: pink')
    
  4. The test = QLabel('test') wasn't added to the grid layout.

    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):

$ chmod a+x testQMainWindowCentralWidget.py

$ ./testQMainWindowCentralWidget.py 

and got:

snapshot of ./testQMainWindowCentralWidget.py

Now, the QMainWindow.setCentralWidget() works as expected.

I don't know which issues the OP actually ran in.

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.

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.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
  • 1
    thank you for your answer, it was the custom QWidget not having a paintEvent method that caused the problem as setStyleSheet requires that you define it, and the fact that i forgot to add the test label to the layout made me even more confused . – nzz coding May 09 '20 at 15:43