Wibo Kuipers
Wibo Kuipers

Reputation: 83

Layout in QWidget makes background white when adding a stretch

Im using PyQt5 and it's styling system to create a modern looking GUI for my application and i can't seem to get this right.

So i've got a costum titlebar all working. It has 3 parts; a menubar, a label and another menubar that serves as the titlebar buttons for closing, min- and maximizing. I need this titlebar to be a light grey color, but as you can see in the image below, there is white space between the elements.

What it is now:

enter image description here

What is should be:

enter image description here

When you run the example below, you can see that between the labels there is some empty space. Even though the labels are inside a box without styling, the styling is set on the widget.

#### PyQt imports....
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QMenuBar, QApplication,
                            QLabel, QVBoxLayout)
#### Python imports....
import sys

#### Class for sampleWindow....
class sampleWindow(QWidget):
    def __init__(self):
        super().__init__()

        #### Some window settings....
        self.setWindowTitle('Sample Program')
        self.setGeometry(400, 300, 1000, 500)

        ######## THE SAME PROBLEM BUT THIS TIME NOT IN A QMENUBAR ########
        #### Creating the widget and it's layout....
        parentLayout = QHBoxLayout()
        parentWidget = QWidget()

        #### Creating the elements....
        sampleLabelLeft = QLabel('left')
        sampleLabelCenter = QLabel('center')
        sampleLabelRight = QLabel('right')

        #### Setting alignment for the elements....
        sampleLabelLeft.setAlignment(Qt.AlignLeft)
        sampleLabelCenter.setAlignment(Qt.AlignCenter)
        sampleLabelRight.setAlignment(Qt.AlignRight)

        #### Adding the elements to the parentLayout....
        parentLayout.addWidget(sampleLabelLeft)
        parentLayout.addWidget(sampleLabelCenter)
        parentLayout.addWidget(sampleLabelRight)

        #### Setting parentLayout as layout for parentWidget....
        parentWidget.setLayout(parentLayout)

        #### Set styling for elements....
        self.setStyleSheet('QWidget{background:blue;} QLabel{background:red;}')

        #### Setting some a box to put parentWidget in so it can be set as the main layout....
        mainBox = QVBoxLayout()
        mainBox.addStretch()
        mainBox.addWidget(parentWidget)
        mainBox.addStretch()
        mainBox.setContentsMargins(200,200,200,200)
        self.setLayout(mainBox)

app = QApplication(sys.argv)
sampleWindow = sampleWindow()
sampleWindow.show()
app.exec()

So after this i set the background color of the QWidget to a bit of a light grey and the stretches are ignored.

Does anyone know a workaround for this?

Upvotes: 0

Views: 912

Answers (2)

Jeremy Roy
Jeremy Roy

Reputation: 374

I found that setting a background widget solved the problem:

parentWidget = QWidget()
label_background = QLabel(parentWidget)
label_background.setFixedSize(1920, 1080)

Upvotes: 0

eyllanesc
eyllanesc

Reputation: 244093

By default the layout has a style-dependent spacing, so the solution for your case is to set it to 0:

# ...
parentLayout = QHBoxLayout()
parentLayout.setSpacing(0)
# ...

Upvotes: 2

Related Questions