Question

I'm trying to create a simple GUI consisting of 3 sections split vertically, with the top 2 being QLabels and the bottom being a grid of buttons. My issue is that although the buttons are spreading horizontally within the grid, they are not expanding vertically to fill the entire cell.

Here's my setup:

    app = QtGui.QApplication([])

    window = QtGui.QMainWindow()
    window.setWindowFlags(QtCore.Qt.FramelessWindowHint)
    window.setFixedSize(self.WINDOW_WIDTH,self.WINDOW_HEIGHT)

    centralWidget = QtGui.QWidget(window)
    window.setCentralWidget(centralWidget)


    LCD = QtGui.QLabel()
    LCD.setFixedSize(self.WINDOW_WIDTH, int(.32*self.WINDOW_HEIGHT))
    LCD.setAutoFillBackground(True)
    LCD.setAlignment(QtCore.Qt.AlignCenter)
    LCD.setText("LCD")

    palette = QtGui.QPalette()
    palette.setColor(QtGui.QPalette.Background,QtCore.Qt.red)
    palette.setColor(QtGui.QPalette.Foreground,QtCore.Qt.white)
    LCD.setPalette(palette)

    ticker= QtGui.QLabel()
    ticker.setFixedSize(self.WINDOW_WIDTH, int(.04*self.WINDOW_HEIGHT))
    ticker.setText("Ticker")
    ticker.setAlignment(QtCore.Qt.AlignCenter)

    buttonWidget = QtGui.QWidget()
    buttonWidget.setFixedSize(self.WINDOW_WIDTH, int(.64*self.WINDOW_HEIGHT))


    vbox = QtGui.QVBoxLayout(centralWidget)
    vbox.setContentsMargins(0,0,0,0)
    vbox.setSpacing(0)

    vbox.addWidget(LCD)
    vbox.addWidget(ticker)
    vbox.addWidget(buttonWidget)

    buttonGridbox = QtGui.QGridLayout(buttonWidget)
    buttonWidget.setLayout(buttonGridbox)
    for i in range(0,2):
        for j in range(0,2): 
            buttonGridbox.addWidget(QtGui.QPushButton("button "+ str(i)),i,j)



    centralWidget.setLayout(vbox)

    window.show()
    sys.exit(app.exec_())

Here's my output (I've bounded the QLabels and QGridLayout with rectangles to show their regions):

result

Was it helpful?

Solution

You have to set setSizePolicy on them to expand, like

THE_BUTTON_OBJECT.setSizePolicy(QtGui.QSizePolicy.MinimumExpanding,QtGui.QSizePolicy.Preferred)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top