Disable horizontal scrolling in QScrollArea comple

2019-02-19 05:25发布

问题:

In Qt, I have a QScrollArea that has some content in it that can scroll vertically but should never, ever be allowed to scroll horizontally. Even if I disable the H scrollbars from showing, a mouse that has a scroll wheel (or touch pad) that supports horizontal motion will make it move a little bit side to side.

Now, this may partly be a layout issue... but nothing is actually off the screen. It's probably a cop-out, but is there a way to just "lock" the scroll area from behind able to move horizontally at all?

回答1:

For your QScrollArea you need filter QEvent::Wheel in eventFilter method or overload wheelEvent(QWheelEvent* event) method.

Other way is create widget inherited from QWidget with overloaded eventFilter only and apply its filter to your scroll area:

scrollArea->viewport()->installEventFilter(someFilterWidget);


回答2:

scrollArea->verticalScrollBar()->setEnabled(false);
scrollArea->horizontalScrollBar()->setEnabled(false);


回答3:

Did you try to set the scroll bar policy ?

myScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

http://qt-project.org/doc/qt-4.8/qabstractscrollarea.html#horizontalScrollBarPolicy-prop