Skip to content

Don't reserve room for spinbox buttons if buttons disabled

Nyan Pasu requested to merge (removed):fix-spinbox-nobuttons into master

Previously, in Qt Widgets with the Breeze theme active, QAbstractSpinBox::setButtonSymbols(QAbstractSpinBox::NoButtons) would hide the buttons but leave a white space where the buttons would normally be. This changes the Breeze theme to skip adding the width of the hidden buttons to the spinbox's width.

This fixes QSpinBox and QDoubleSpinBox with buttons hidden. QDateTimeEdit is fixed as well, but it has empty whitespace to the right of the text both with and without buttons visible. (That's not related to this bug or fix though.)

Before (QSpinBox):

breeze_NoButtons

After (QSpinBox):

breeze_NoButtons_fixed

After (QDateTimeEdit):

Screenshot_20210807_191258_

Technical details

labelRect.adjust(frameWidth... -frameWidth) is necessary to prevent the QLineEdit subcontrol from painting over the right border of the spinbox. But does this result in the QLineEdit being too small to fit the text (since we subtract 2*frameWidth from the width)? No, the spinbox's size (hint?) is obtained from the QLineEdit's size (hint?) by calling spinBoxSizeFromContents, which calls expandSize( size, frameWidth ), which adds 2*frameWidth to the height and width, So the final size hint of the QLineEdit is exactly equal to the contentsSize passed into spinBoxSizeFromContents. In fact, with regular spinboxes with buttons, the edit field subcontrol is slightly wider than necessary!

Also why is it called labelRect despite being an editable QLineEdit text field, and not a static label?

I'm not sure if I got the code style right, as it's rather unusual. And I don't know if the existing code style is right either; I found some lines with trailing whitespace, and ninja clang-format completely reformatted dozens of files, so I can't use that to fix my formatting.

This applies cleanly to Breeze 5.22, and I think it's worth cherry-picking to that branch. It's personally important to me because I'm developing an app using QAbstractSpinBox::NoButtons.

BUG: 440718

Edited by Nyan Pasu

Merge request reports