Important: Please read the Qt Code of Conduct -

Qt5.0 size hint is incorrect (way too small) for QSpinBox compared to Qt4.8

  • When I ported my Qt4.8 application to Qt5.0, all the spin boxes are drawn incorrectly. Specifically, a QSpinBox is placed in a QFormLayout using setFieldGrowthPolicy(QFormLayout::FieldsStayAtSizeHint); The QSpinBox is drawn so small that most of the digits are not visible. The problem is that the sizeHint is way too small for the QSpinBox as computed by Qt5.0 vs correct in Qt4.8.

    Look at this sample code which creates an array of 5 QSpinBoxes, each with a maximum value one digit longer than the previous, and prints the size hint. Under Qt4.8, the size hints start out at a width of 27, and each digit adds a width of 6. Under Qt5.0, the size hint starts out at only 9, and each additional digit again adds a width of 6. But the original width of 9 is certainly not big enough to draw the box, 1 digit, and the up/down arrows. So you don't get much on your display.
    @ QSpinBox* test1[5];
    int max = 1;
    for (unsigned i = 0; i < SIZE(test1); i++)
    test1[i] = new QSpinBox;
    test1[i]->setRange(0, max);
    max *= 10;
    qDebug() << "test1 " << i << " sizeHint = " << test1[i]->sizeHint();

    Output from the above test code compiled under Qt4.8:
    @test1 0 sizeHint = QSize(27, 20)
    test1 1 sizeHint = QSize(33, 20)
    test1 2 sizeHint = QSize(39, 20)
    test1 3 sizeHint = QSize(45, 20)
    test1 4 sizeHint = QSize(51, 20)
    test2 0 sizeHint = QSize(36, 20)
    test2 1 sizeHint = QSize(42, 20)
    test2 2 sizeHint = QSize(48, 20)
    test2 3 sizeHint = QSize(54, 20)
    test2 4 sizeHint = QSize(60, 20) @

    Output from the above code compiled with Qt5.0
    @test1 0 sizeHint = QSize(9, 22)
    test1 1 sizeHint = QSize(15, 22)
    test1 2 sizeHint = QSize(21, 22)
    test1 3 sizeHint = QSize(27, 22)
    test1 4 sizeHint = QSize(33, 22)
    test2 0 sizeHint = QSize(18, 22)
    test2 1 sizeHint = QSize(24, 22)
    test2 2 sizeHint = QSize(30, 22)
    test2 3 sizeHint = QSize(36, 22)
    test2 4 sizeHint = QSize(42, 22) @

  • Note - I found this where they "fixed a bug" by changing they way they calculate the size hint for QSpinBox. They now compute the size hint by computing it for the text only, and let the style add what is necessary for the frame, rectangle, etc. This fix doesn't work, because obviouslyl the style doesn't add anything for the frame, rectangle, etc. So now you get a size hint that is good for text only, and no room for the frame, rectangle, arrows, etc. Please put the old code back in!

    "Tree SHA1: e0b52b5
    Parent SHA1: 528de79 (Missing break in case QEvent::WindowStateChange.)
    raw diff | raw patch
    Fix incorrect size hint for abstract spinbox

    This size hint was based on strange logic from an alternate
    universe. The size hint should only contain the text size
    and let the style add what is neccessary for the frame rect etc.
    Most styles have worked around this by simply ignoring it,
    however some styles could still break a bit.

    Note that we add 4 since that is the constant that the old
    code 'usually' ended up at and should be compatible with
    our existing styles.

    Change-Id: Iebdbcb8949dd8b7daa7d8cb96ae5ab7351e4f79d
    Reviewed-by: J-P Nurmi"

Log in to reply