Issue when restoring docked widgets with restoreState
-
Hello,
when trying to restore my docked widget position with restoreState, the dimension are not correct.The mainwindow is already present and visible. On a user action, I setup various UI elements and try to restore previous state. See pseudo code :
auto lCentralWidget = new ImageDisplayForm( *this, aMainWindow ); lCentralWidget->setTitleBarWidget( new QWidget( lCentralWidget ) ); // hide title bar lCentralWidget->setObjectName( CENTRAL_WIDGET_NAME ); mMainWindow.setCentralWidget( lCentralWidget );
and right after that, I restore the previous state:
QSettings lSettings; lSettings.beginGroup( std::string( SETTINGS_NAME ) + "_" + mCameraName ); const int lSize = lSettings.beginReadArray( WIDGET_INFO ); for( auto i = 0; i < lSize; ++i ) { lSettings.setArrayIndex( i ); CameraUIWidgetBase *lWidget = RequestWidget( lSettings.value( WIDGET_ID ).toString().toStdString() ); if( lWidget != nullptr ) { if( lSettings.value( WIDGET_FLOATING ).toBool() ) { lWidget->restoreGeometry( lSettings.value( WIDGET_GEOMETRY ).toByteArray() ); } } } lSettings.endArray(); mMainWindow.restoreGeometry( lSettings.value( MAIN_GEOMETRY ).toByteArray() ); mMainWindow.restoreState( lSettings.value( MAIN_STATE ).toByteArray() ); lSettings.endGroup();
The save/restore functions are probably working, because if I call the restore function manually later, it works properly. But if I call it right after the instanciation of UI elements, the dimensions are incorrect.
All my widgets are subclassed from a custom class (and QDockWidget), so I can easily put a breakpoint in the resizeEvent() function to try and understand better what happens.
- central widget is set
- RestoreUI function is called
2.1 the required dock widget is instanciated (not docked yet)
2.1.1 resizeEvent of this widget is called
2.2 QMainWindow::restoreGeometry
2.3 QMainWindow::restoreState
2.3.1 the dock widget is resized to the correct size
2.3.2 the dock widget is resized again to an incorrect size
2.4 we are out of restoreState
2.5 the dock widget is resized again to an incorrect size
2.6 centralWidget is resized
I guess my issue is because the centralWidget is not drawn before I call restoreState, but I dont understand why.
EDIT: if I add
lCentralWidget->show();
aftermMainWindow.setCentralWidget( lCentralWidget );
it works. But It would be nice to understand why I need to force show it. -
Hi,
Until shown, widgets don't have any "physical" presence.
What I usually do when restoring such settings is to use a 0 based timeout QTimer to trigger the settings loading function. This will happen after the event loop starts which usually mean that your widgets should be ready to be properly modified.
-