How to preserve QScrollArea scrollbar position when window is maximized and restored?
-
Hi everyone,
I have a window containing a QScrollArea. When I maximize the window and then restore it back to normal size, the scrollbars reset to the default position (usually top-left). I want to remember the scrollbar positions before maximizing and restore them after the window is restored.
What I’ve tried so far:
Saving the scrollbar values (horizontalScrollBar()->value() and verticalScrollBar()->value()) before maximizing.
Restoring these values after the window is restored, using a delayed call with QTimer::singleShot to ensure layout is updated.
However, I’m not sure if this is the best practice or if Qt provides a better official way to handle this, especially when the content inside the scroll area can dynamically change and layouts get refreshed.
My questions are:
How do you usually implement remembering and restoring scrollbar positions on window maximize/restore?
Are there any Qt signals or events better suited for catching these state changes?
What common pitfalls should I watch out for to make sure scrollbar restoration works reliably?
Any advice, best practices, or code examples would be greatly appreciated!
Thanks a lot!
-
Hi,
What type of "QWidget" are you using?
Is that - "QMdiSubWindow" ? -
@Zbigniew-Sch use QScrollArea
-
Use signal for parent of QScrollArea:
windowStateChanged(Qt::WindowState windowState)
and save/restore parameters for QScrollArea