[Interest] Size of spin box image

John Weeks john at wavemetrics.com
Sat Aug 25 00:05:15 CEST 2012

I need to draw an image of a spin box. Really, draw the image, I can't use a QSpinBox widget.

So I'm using QStyle:: drawComplexControl() with  QStyle::CC_SpinBox. This works well, up to a point.

Now I am ready to size it to fit what I want to put into the control's edit field, which may use various fonts, etc. For this it seems logical to use QStyle:: sizeFromContents(). For the QSize field I used the obvious thing: the size of the bounding box for the text. That didn't work at all- the returned size is actually smaller in height than the input contentsSize parameter.

So I looked at the sizeHint() function for the QAbstractSpinBox widget. I was astounded to find quite a lot of code with hard-coded constants, the use of QLineEdit::sizeHint(), computations on the font metrics, two calls to  subControlRect(QStyle::CC_SpinBox, &opt,  QStyle::SC_SpinBoxEditField ...), the second one with the comment:

         // get closer to final result by repeating the calculation

Why is this so complicated? I could never come up with the complex heuristic used for QAbstractSpinBox. 

Is that really what needs to be done? Is there something more obvious that I can do? I suppose I could just copy the code, or even create a dummy QSpinBox just for the purpose of calling sizeHint(), but that is very unsatisfying.

Is there a better approach?

Thanks in advance!

-John Weeks

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120824/9f77d5f9/attachment.html>

More information about the Interest mailing list