diff -r 053c0578cf58 -r d11a3e713e3b src/RangedSlider.cpp --- a/src/RangedSlider.cpp Fri Apr 01 16:52:25 2022 +0200 +++ b/src/RangedSlider.cpp Fri Apr 01 17:25:42 2022 +0200 @@ -53,22 +53,21 @@ _markerTextIsValue(false), indicatorTextFont("Arial", 10, QFont::Normal) // Previously we just did the indicator text in 'default' font { - // Ensure this->heightInPixels is properly initialised -// this->recalculateHeightInPixels(); + // Fixed minimum size. + this->setMinimumSize(60, 20); + this->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ); - // In principle we want to set our min/max sizes etc here. However, if, say, a maximumSize property has been set - // for this object in a Designer UI File (eg ui/mainWindow.ui) then that setting will override this one, because it - // will be applied later (in fact pretty much straight after this constructor returns). So we also make this call - // inside setValue(), which will be invoked _after_ the setter calls that were auto-generated from the Designer UI - // File. - this->setSizes(); + // There no particular reason to limit our horizontal size, so, in principle, this call asks that there be no such + // (practical) limit. + this->setMaximumWidth(QWIDGETSIZE_MAX); - // Generate mouse move events whenever mouse movers over widget. - this->setMouseTracking(true); + // Generate mouse move events whenever mouse movers over widget. + this->setMouseTracking(true); - this->repaint(); + this->repaint(); } + /** * @brief Set the normal limits in the _prexXxx values and calculate * the real drawing limits so that we can see if a value is slightly @@ -110,9 +109,6 @@ } update(); - - // See comment in constructor for why we call this here - //this->setSizes(); return; } @@ -150,73 +146,6 @@ } -void RangedSlider::recalculateHeightInPixels() const { - // - // We need to be able to tell Qt about our minimum and preferred size in pixels. This is something that's going - // to depend on the dots-per-inch (DPI) resolution of the monitor we're being displayed on. Advice at - // https://doc.qt.io/qt-5/highdpi.html is that, for best High DPI display support, we should replace hard-coded - // sizes in layouts and drawing code with values calculated from font metrics or screen size. For this widget, we - // use font sizes (as described in more detail later in this comment) as it seems simpler and the height of the - // widget really is determined by the size of the text it contains. - // - // In theory, someone might be running the app on a system with multiple screens with different DPI resolutions, - // so, in principle we ought to do redo size calculations every time the widget is moved, in case it moves from one - // screen to another and the resolution changes. In practice, I'm not sure how big a requirement this is. So, for - // now, have just tried to organise things so that it would, in principle, be possible to implement such behaviour - // in future. - // - // We want height to be fixed, as the slider does not get more readable if you make it taller. So minimum and - // preferred height are the same. - // - // For width, We are OK for them to expand and contract horizontally, within reason, as the size of the main window - // changes. Minimum and preferred widths are a bit of a rule-of-thumb, but 2× and 4× height are a sensible stab. - // - // Firstly we have to determine what the fixed height is. We could query the DPI resolution and size of the current - // screen, but the simplest thing is to use font sizes. The slider is basically two lines of characters high. Top - // line is the "indicator" text that sits above the slider visual in the middle of the "preferred range". Bottom - // line is the slider visual and the "value" text that sits to the right of the slider visual. The fonts for both - // bits of text are set in device-independent points in the constructor, and we can just query their height etc in - // pixels. - // - // Secondly, the way we tell Qt about minimum and preferred sizes is slightly different: - // • setMinimumSize() tells Qt not to make us smaller than the specified size when resizing windows etc, HOWEVER - // it does not determine what size we are initially drawn - // • instead, Qt calls sizeHint() when doing initial layout, and we must override this to supply our desired - // initial dimensions. NB: Although it makes no sense, there is nothing to stop this method returning dimensions - // below the minimums already set via setMinimumSize(). (AIUI, sizeHint() is also called on resize events to - // find our preferred dimensions.) - // - // The final wrinkle is that the height of the font sort of depends what you mean. Strictly, using the inter-line - // spacing (= height plus leading, though the latter is often 0) gives you enough space to show any character of the - // font. It is helpful for the indicator text to have a bit of space below it before we draw the graphical bit, and - // it's not a large font in any case. However, for large value text font we don't necessarily need all this space - // because we currently only show digits and decimal points, which don't require space below the baseline. However, - // assumptions about space below the baseline are locale-specific, so, say, using ascent() instead of lineSpacing() - // could end up painting us into a corner. - // - //QFontMetrics indicatorTextFontMetrics(this->indicatorTextFont); -// this->heightInPixels = this->height(); - return; -} - -void RangedSlider::setSizes() { - // Fixed minimum size. - this->setMinimumSize(60, 20); - this->setSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ); - - // There no particular reason to limit our horizontal size, so, in principle, this call asks that there be no such - // (practical) limit. - this->setMaximumWidth(QWIDGETSIZE_MAX); - - return; -} - -//QSize RangedSlider::sizeHint() const -//{ -// this->recalculateHeightInPixels(); -// return QSize(4 * this->heightInPixels, this->heightInPixels); -//} - void RangedSlider::mouseMoveEvent(QMouseEvent* event) { event->accept(); @@ -331,12 +260,8 @@ } -void RangedSlider::moveEvent(QMoveEvent *event) { - // If we've moved, we might be on a new screen with a different DPI resolution... - // .:TBD:. This almost certainly needs further work and further testing. It's far from clear whether our font size - // querying will give different answers just because the app has been moved from one screen to another. - this->recalculateHeightInPixels(); - - QWidget::moveEvent(event); - return; +void RangedSlider::moveEvent(QMoveEvent *event) +{ + QWidget::moveEvent(event); + return; }