Important: Please read the Qt Code of Conduct -

Translated title of QGroupBox larger in width than QGroupbox

  • Hi,
    we have a software for our customers and now we got back the first
    translations from our company representation outside of Germany.
    The german and english title of the group box looks nice but the
    spanish title is very long. Currently the title is not completely visible,
    some characters on the right side are missing.

    The groupbox is in a QTabWidget with a vertical layout.
    The title of each groupbox is set to bold using stylesheets.
    The normal text with the groupbox is not bold.

    I tried to calc the width using QFontMetrics, but however, the calculated size was
    smaler the the current width of the groupbox, what makes no sence.

    How can i achieve that the groupbox resize that the full bold title is visible?

  • I think you might have spotted a bug. the problem looks like is in QGroupBox::minimumSizeHint() try subclassing QGroupBox and reimplementing virtual QSize QGroupBox::minimumSizeHint() const override

    QSize QGroupBox::minimumSizeHint() const
    QStyleOptionGroupBox option;
             //QFontMetrics metrics(fontMetrics());  // Wrong
             QFontMetrics metrics(option.fontMetrics); //Right
            int baseWidth = metrics.width(title()) + metrics.width(QLatin1Char(' '));
            int baseHeight = metrics.height();
            if (isCheckable()) {
                baseWidth += style()->pixelMetric(QStyle::PM_IndicatorWidth);
                baseWidth += style()->pixelMetric(QStyle::PM_CheckBoxLabelSpacing);
                baseHeight = qMax(baseHeight, style()->pixelMetric(QStyle::PM_IndicatorHeight));
            QSize size = style()->sizeFromContents(QStyle::CT_GroupBox, &option, QSize(baseWidth, baseHeight), this);
            return size.expandedTo(QWidget::minimumSizeHint());

    (you probably could even use const QFontMetrics& metrics=option.fontMetrics; here or use option.fontMetrics directly where metrics is used) if this fixes your problem it might be worth opening a bug report so that it will be fixed in futre Qt versions

Log in to reply