Any better approach for using ensureWidgetVisible() without QTimer::singleShot?
-
I have a QScrollArea where I add child widgets dynamically. I want the scrollbar to jump to the newly added widget. Son, I'm using
ensureWidgetVisible()
method to move the scrollbar automatically. QWidgets sizes are not calculated properly until they are displayed. Therefore, I'm usingQTimer::singleShot
to wait100ms
before calling theensureWidgetVisible()
. Otherwise, the viewport of QScrollArea may not update its size immediately and therefore moves the scroll to an intermediate position.Is there any better approach without using QTimer?
-
If it's your custom widget you can override its showEvent and emit a signal from it to which you can connect and call the scrolling there.
If you don't want to modify the widget you can also install an event filter when you add it. In the handler check event type and if it's
QEvent::Show
do the scrolling. After that you can remove the filter. -
@Chris-Kawa Thanks. That worked!