Issue
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:
What is should be:
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?
Solution
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)
# ...
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.