1

When I Scroll a widget in QScrollArea, its scrollbar's step is not singleStep() but singleStep() * 3.

I set singleStep of QScrollArea's verticalScrollBar() to 30 in order to scroll 30 per scroll, but it scrolls 90 per scroll.

MyScrollArea::MyScrollArea(QWidget* parent) : QScrollArea(parent) {
    this->verticalScrollBar()->setSingleStep(30); // set singleStep to 30

    connect(this->verticalScrollBar(), SIGNAL(valueChanged(int)),
            this, SLOT(on_valuechange(int)));
}

MyScrollArea::on_valuechange(int i) {
    qDebug() << i;  // always 90n, not 30n
}

or is this just the problem with my mouse?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Anns98
  • 736
  • 6
  • 6
  • 4
    This is your OS setting. The default setting (see system settings -> mice -> wheel) is to scroll 3 lines. Therefore the *3 here. – chehrlic Mar 15 '21 at 08:16
  • 1
    On a side note, you should use [the new syntax](https://wiki.qt.io/New_Signal_Slot_Syntax), to let the compiler check your connections – Jack Lilhammers Mar 15 '21 at 08:41
  • Oh, it has less parenthesis than the old one. thanks! – Anns98 Mar 15 '21 at 15:01
  • 1
    _Oh, it has less parenthesis than the old one._ ...and that's only the least reason to use it. The compile time checks are even more worth, not to mention that anything callable can become a slot without the need to remark it as such, and lambdas can be used as adapters... ;-) – Scheff's Cat Mar 15 '21 at 15:45
  • Actually, the new syntax may end with a larger line of code (for example, if class' names are large, or if you need to [connect overloaded methods](https://stackoverflow.com/q/16794695/1485885)). Nevertheless, this kind of _drawbacks_ are trivial compared to the benefits of compile-time checks (connection problems when using the old syntax are sometimes very hard to detect). – cbuchart Mar 17 '21 at 09:00

1 Answers1

1

As mentioned in the comments, your single step is being multiplied by the system's number of lines.

If you want to have the same scrolling step regarding less system's settings you always adjust the step accordingly. For example, for Windows:

#include <winuser.h>
#include <QScrollBar>

void setSingleStep(QScrollBar* bar, int step) {
  UINT sys_steps;
  SystemParametersInfoA(SPI_GETWHEELSCROLLLINES, 0, &sys_steps, 0);

  bar->setSingleStep(step / sys_steps);
}
cbuchart
  • 10,847
  • 9
  • 53
  • 93