How to scale a progress bar
-
Hi, I'm learning how to handle the QT Designer. I'm starting to figure out the layout, but I've stumbled upon a blocker - my layout should get rescaled at runtime, and it works for normal widgets, but doesn't work for the progress bar. It's as if the progress bar had a fixed size.
This is how it looks initially - 3 elements in a vertical layout, progress bar in the middle.
And after rescaling, only the widgets (top and bottom rectangles) get rescaled. The progress bar stays exactly the same.
Which property is responsible for this behavior? I'm comparing the progress bar with the other widgets, but don't see that difference.
-
The behaviour will depend a little on the widgets in the layout (i.e. a maximum or minimum size), and the settings on the layout itself.
Assuming you are using a QGridLayout (because you don't say), and the contained widgets are mostly default, then you should look at QGridLayout::rowStretch() and QGridLayout::setRowMinimumHeight(). You should also look for minimum size limits on the progress bar or the labels either side.
Otherwise, post more information.
-
@ChrisW67
I'm not using QGridLayout in this case (alhough perhaps I should). To describe the picture in more detail:
this is a horizontal layout, containing labels on both sides, and a QWidget in the middle. The QWidget in the middle is a vertical layout, consisting of two QWidgets, represented as the green and blue rectangles; and a progress bar.
It looks like the progress bar has some "minimum size" parameter, but I can't find it - I just hope it's not hardcoded somewhere in the QT engine. It looks like 95px is that minimum - it doesn't scale below that. If I set the layout, so that the progress bar is above 95px, it does scale (but doesn't go below 95px, of course).
The minimum/maximum parameters of the progress bar are not related with the size.
widget part of the progress bar states, that its minimum size is 0, just like other widgets that scale properly. Instead, its height doesn't go below this 95 pixels. Width seems to scale properly.
These are the parameters of the vertical layout, containing the progress bar, and 2 other widgets.
-
It has something to do with the content of the progress bar - right now I'm sure there is some hidden, implicit minimum size that QT Designer doesn't inform me about.
I observe the same behavior with labels, except in case of labels, I've realized that the implicit minimum size is determined by the label's text (also, wow, the "scaledcontent" field for labels is useless, the content doesn't get scaled at all, lol)
Haven't figured this one out, left it for later. Maybe once I figure it out, I'll post the answer here.
-
@DatGK
https://codebrowser.dev/qt5/qtbase/src/widgets/widgets/qprogressbar.cpp.html#424QFontMetrics fm = fontMetrics(); QStyleOptionProgressBar opt; initStyleOption(&opt); int cw = style()->pixelMetric(QStyle::PM_ProgressBarChunkWidth, &opt, this); QSize size = QSize(qMax(9, cw) * 7 + fm.horizontalAdvance(QLatin1Char('0')) * 4, fm.height() + 8); if (opt.orientation == Qt::Vertical) size = size.transposed();
So
qMax(9, cw) * 7 + fm.horizontalAdvance(QLatin1Char('0')) * 4
.
I think the gist is 63+-odd for the chunks (7 chunks) + 4 character0
s. Then in your case this is applied for the height instead of the width. May be quite close to 95-odd pixels.