Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Restore/Preserve QDockWidget's geometry/size



  • My Qt application has a QMainWindow whose central widget can be switched/changed with the click of a button.

    And each central widget has set of its own QDockWidget's.

    On switching/changing central widget, corresponding QDockWidget's are shown and rest are hidden.

    I have a problem that: the sizes of newly shown QDockWidget's are changed (by Qt internally), losing the sizes set by the user.

    FYI: I tried QDockWidget::restoreGeometry(...) after showing new QDockWidget's, but it did not work.

    Any help is much appreciated.


  • Lifetime Qt Champion

    @soma_yarram I would use https://doc.qt.io/qt-5/qstackedwidget.html instead of changing central widget.



  • @jsulm : Yes, I do use QStackedWidget to change my main (central) widget instead of calling QMainWindow::setCentralWidget(...).

    When I call QStackedWidget::setCurrentWidget(...), I show new QDockWidget's (by calling QDockWidget::setVisible(true)) and hide previously visible QDockWidget's (by calling QDockWidget::setVisible(false)).
    But my problem is that: the sizes of newly shown QDockWidget's are changed (by Qt internally), losing the sizes set by the user.

    FYI: I call QStackedWidget::addWidget(...) and QMainWindow::addDockWidget(...) during the application startup.


  • Lifetime Qt Champion

    @soma_yarram said in Restore/Preserve QDockWidget's geometry/size:

    I show new QDockWidget's (by calling QDockWidget::setVisible(true))

    Why? It should be enough to activate the correct widget using https://doc.qt.io/qt-5/qstackedwidget.html#currentIndex-prop or https://doc.qt.io/qt-5/qstackedwidget.html#setCurrentWidget



  • @jsulm : Yes, I change my main (central) widget by calling QStackedWidget::setCurrentWidget(...).

    Actually the application has following structure:
    Step 1: I created QWidget_1 and QWidget_2, and added to QStackWidget (which is the central widget of my QMainWindow).

    Step2: I created QDockWidget_1_1, QDockWidget_1_2, QDockWidget_2_1 and QDockWidget_2_2, and added to QMainWindow (using QMainWindow::addDockWidget(Qt::RightDockWidgetArea, ...)).

    Step 3: I (or user) call QStackedWidget::setCurrentWidget(QWidget_1), which internally calls
    QDockWidget_1_1::setVisible(true),
    QDockWidget_1_2::setVisible(true),
    QDockWidget_2_1::setVisible(false),
    QDockWidget_2_2::setVisible(false).
    This hides QDockWidget_2_1 and QDockWidget_2_2, and shows QDockWidget_1_1 and QDockWidget_1_2.

    Step 4: User adjusts the sizes of QDockWidget_1_1 and QDockWidget_1_2 (using the splitter handle added by QMainWindow).

    Step 5: I (or user) call QStackedWidget::setCurrentWidget(QWidget_2), which internally calls
    QDockWidget_1_1::setVisible(false),
    QDockWidget_1_2::setVisible(false),
    QDockWidget_2_1::setVisible(true),
    QDockWidget_2_2::setVisible(true).
    This hides QDockWidget_1_1 and QDockWidget_1_2, and shows QDockWidget_2_1 and QDockWidget_2_2.

    Step 6: User adjusts the sizes of QDockWidget_2_1 and QDockWidget_2_2 (using the splitter handle added by QMainWindow).

    Step 7: Again, I (or user) call QStackedWidget::setCurrentWidget(QWidget_1), which internally calls
    QDockWidget_1_1::setVisible(true),
    QDockWidget_1_2::setVisible(true),
    QDockWidget_2_1::setVisible(false),
    QDockWidget_2_2::setVisible(false).
    This hides QDockWidget_2_1 and QDockWidget_2_2, and shows QDockWidget_1_1 and QDockWidget_1_2.
    But this time, the sizes of QDockWidget_1_1 and QDockWidget_1_2 are different to the ones that are set by the user in Step 4.

    My question is: how can I restore the sizes of QDockWidget_1_1 and QDockWidget_1_2 that are set by the user in Step 4?

    Hope you understand. Thanks in advance.


  • Lifetime Qt Champion

    Hi,

    @soma_yarram said in Restore/Preserve QDockWidget's geometry/size:

    FYI: I tried QDockWidget::restoreGeometry(...) after showing new QDockWidget's, but it did not work.

    Did you gave an objectName (as explained in the QMainWindow::saveState ) to all your QDockWidget to be restored ?


Log in to reply