QScrollArea: Area is not updated while an arrow key is held pressed
-
While the key is pressed only a scroll bar is updated but not the area. The area is update when the arrow key is released.
I'd like to change my code to fix that.I tried reimplementing keyPressEvent and forcing updating or repainting the viewport at the end like:
void keyPressEvent(QKeyEvent *event) override { int step = 20; // Set your custom scroll step here switch (event->key()) { case Qt::Key_Up: verticalScrollBar()->setValue(verticalScrollBar()->value() - step); break; case Qt::Key_Down: verticalScrollBar()->setValue(verticalScrollBar()->value() + step); break; case Qt::Key_Left: horizontalScrollBar()->setValue(horizontalScrollBar()->value() - step); break; case Qt::Key_Right: horizontalScrollBar()->setValue(horizontalScrollBar()->value() + step); break; default: QScrollArea::keyPressEvent(event); // Call base class implementation for other keys } // Solution 1: // Force the viewport to update viewport()->update(); // Solution 2: // Force the viewport to repaint viewport()->repaint(); // Solution 3 // Force the viewport to update viewport()->update(); // Ensure the widget inside the scroll area is updated if (widget()) { widget()->update(); }
While the key is held pressed keyPressEvent is called several time but the area is still not updated
How can I fix that? Is it really necessary to reimplement keyPressEvent?
I'm developping in Qt 5.
TIA
-
-
First and foremost: Please update to Qt6, maybe the problem is solved by that.
Are you sure the code is actually hit? Are theupdate()
calls updating the right widget? -
The code I try to modify is open source software. The gui code is big and can't be ported easily. I don't know there is a plan to switch to Qt6.
I added a trace and keyPressEvent is called regularly while the key is held pressed. I did an update on our LayoutScroll which inherits from QScrollArea and also out LayoutViewer which inherits from QWidget.The classes are there: https://github.com/The-OpenROAD-Project/OpenROAD/blob/master/src/gui/src/layoutViewer.cpp
And the class that instantiate them: https://github.com/The-OpenROAD-Project/OpenROAD/blob/master/src/gui/src/layoutTabs.cpp -
The code I try to modify is open source software. The gui code is big and can't be ported easily. I don't know there is a plan to switch to Qt6.
I added a trace and keyPressEvent is called regularly while the key is held pressed. I did an update on our LayoutScroll which inherits from QScrollArea and also out LayoutViewer which inherits from QWidget.The classes are there: https://github.com/The-OpenROAD-Project/OpenROAD/blob/master/src/gui/src/layoutViewer.cpp
And the class that instantiate them: https://github.com/The-OpenROAD-Project/OpenROAD/blob/master/src/gui/src/layoutTabs.cpp -
-
-