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 .