How to Customise Qgroupbox Title in Pyqt5

How to customise QGroupBox title in PyQt5?

1) Probably that's the default QT placement, in the first image the platform style is used, and its take care of borders and title placement, when you change the stylesheet you override something and you get the ugly placement.

2) You can control the "title" position using the QGroupBox:title controls, for example:

gb.setStyleSheet('QGroupBox:title {'
'subcontrol-origin: margin;'
'subcontrol-position: top center;'
'padding-left: 10px;'
'padding-right: 10px; }')

will result in something like this:

title

3) My suggestion is to create different strings for the stylesheet attributes you want to change, then compose them to create the style you want.

PyQt5 QGroupBox - how to display title in line with the top outside line for the QGroupBox?

Try it:

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QHBoxLayout, QVBoxLayout, QPushButton, QRadioButton, \
QGroupBox, QButtonGroup
from PyQt5.Qt import *

class MainForm(QWidget):

def __init__(self):
super().__init__()

self.rdoRed = QRadioButton('Red')
self.rdoGreen = QRadioButton('Green')
self.rdoBlue = QRadioButton('Blue')

self.vblColor = QVBoxLayout()
self.vblColor.addWidget(self.rdoRed)
self.vblColor.addWidget(self.rdoGreen)
self.vblColor.addWidget(self.rdoBlue)

self.gbColor = QGroupBox('Choose Color')

self.gbColor.setStyleSheet('''
QGroupBox {
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #E0E0E0, stop: 1 #FFFFFF);
border: 2px solid #999999;
border-radius: 5px;
margin-top: 2ex; /*leave space at the top for the title */
font-size: 13px;
color: black;
}
QGroupBox::title {
subcontrol-origin: margin;
subcontrol-position: top center; /* position at the top center */
padding: 0 3px;
font-size: 13px;
color: black;
}
''')

self.gbColor.setLayout(self.vblColor)

self.vboxLayout = QVBoxLayout()
self.vboxLayout.addWidget(self.gbColor)

self.setLayout(self.vboxLayout)

def main():
app = QApplication([])
mainForm = MainForm()
mainForm.show()
app.exec()

if __name__ == '__main__':
main()

Sample Image

Set QGroupBox title font size with style sheets

The answer is "no", the title of a QGroupBox does not support the QWidget::font property. I suspect that the title is not an independant QWidget but a part of the QGroupBox widget (thus drawn by the QGroupBox::paint()).

However, the GroupBox widget supports the font property and since the only text displayed by a group box is its title, you can apply your font style to the QGroupBox widget.

QGroupBox
{
font-size: 18px;
font-weight: bold;
}

How to change the font of a QGroupBox's title only?

Font properties are inherited from parent to child, if not explicitly set. You can change the font of the QGroupBox through its setFont() method, but you then need to break the inheritance by explicitly resetting the font on its children. If you do not want to set this on each individual child (e.g. on each QRadioButton) separately, you can add an intermediate widget, e.g. something like

QGroupBox *groupBox = new QGroupBox("Bold title", parent);

// set new title font
QFont font;
font.setBold(true);
groupBox->setFont(font);

// intermediate widget to break font inheritance
QVBoxLayout* verticalLayout = new QVBoxLayout(groupBox);
QWidget* widget = new QWidget(groupBox);
QFont oldFont;
oldFont.setBold(false);
widget->setFont(oldFont);

// add the child components to the intermediate widget, using the original font
QVBoxLayout* verticalLayout_2 = new QVBoxLayout(widget);

QRadioButton *radioButton = new QRadioButton("Radio 1", widget);
verticalLayout_2->addWidget(radioButton);

QRadioButton *radioButton_2 = new QRadioButton("Radio 2", widget);
verticalLayout_2->addWidget(radioButton_2);

verticalLayout->addWidget(widget);

Note also, when assigning a new font to a widget, "the properties from this font are combined with the widget's default font to form the widget's final font".


An even easier approach is to use style sheets - unlike with CSS, and unlike the normal font and color inheritance, properties from style sheets are not inherited:

groupBox->setStyleSheet("QGroupBox { font-weight: bold; } ");

Adjust title position in a QGroupBox (using style sheets)

Applying the following style:

QGroupBox {
font: bold;
border: 1px solid silver;
border-radius: 6px;
margin-top: 6px;
}

QGroupBox::title {
subcontrol-origin: margin;
left: 7px;
padding: 0px 5px 0px 5px;
}

I get something like this:

Sample Image

How to build a QGroupBox with a button in the title?

The widgets that come stock with PyQt are you basic building blocks and convenience widgets to cover the common usages and needs. But when you start to want custom widgets, you then have the freedom to subclass something that is close, and compose your own custom widgets.

Lets take the QGroupBox. Its basically a QFrame composed with a QLabel at the top. Then the class wraps some methods that allow you to set the text of this label. This basic widget would start out like:

group = QtGui.QGroupBox()
group.setTitle("FOO")

What we can do instead is take a QWidget, and add a QGroupBox with a blank label. And then place a button at an absolute position to the parent widget. We could have used a QFrame but using a QGroupBox gets you the instant styling you wanted.

class ButtonGroupBox(QtGui.QWidget):

def __init__(self, parent=None):
super(ButtonGroupBox, self).__init__(parent=parent)

self.layout = QtGui.QVBoxLayout(self)
self.layout.setContentsMargins(0,24,0,0)
self.groupBox = QtGui.QGroupBox(self)
self.button = QtGui.QPushButton("FOO", parent=self)
self.layout.addWidget(self.groupBox)

self.button.move(0, -4)

You can expand this custom class with methods that let you change the text of the button. The margin of the widget at the top is neccessary to give you the extra space to layout the button above the GroupBox

Position of the title in Group Box

Thanks for the tip ekhumoro. Now it works.

Sample Image

QGroupBox{
border: 1px solid cyan;
margin-top: 0.5em;
background-color: #000044;
font: 12px consolas;
color: cyan;
}

QGroupBox::title {
top: -6px;
left: 10px;
}


Related Topics



Leave a reply



Submit