Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. restoreDockWidget reports true - but doesn't restore

restoreDockWidget reports true - but doesn't restore

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 494 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Musmuris
    wrote on last edited by Musmuris
    #1

    Hi,
    I have a QMainWindow and saving the state as per the docs (saveSate and restoreState - and geometry). The docks all have unique objectName set. The are added after the constructor is called - but before the app gets going (i.e. before .exec())

    The readSettings is called in the c'tor of my main window (again copied from the docs and then when a dock is added restoreDockWidget is called and returns true for each dock. However it just seems to do nothing - and the state is as they were. i.e. if I move them round, exit and restart, then they are back where were originally.

    The ini file seems to be created (and updated) each time - state is obviously an opaque byte array (though it does have the dock names if one coverts to ascii!)

    // aDock is created and object name set then passed to QMainWindow:
    aDock->setAllowedAreas(Qt::AllDockWidgetAreas);
    aDock->setAttribute(Qt::WA_DeleteOnClose, false);
    
    auto act = aDock->toggleViewAction();
    act->setIcon(icon);
    displaysToolBar->addAction(act);
    
    addDockWidget(Qt::TopDockWidgetArea, aDock);
    restoreDockWidget(aDock)
    

    Is the above expected to work?

    JonBJ 1 Reply Last reply
    0
    • M Offline
      M Offline
      Musmuris
      wrote on last edited by
      #5

      And it turns out I was calling windows Show before adding the docks :facepalm:

      For those following at home - the above code minus the readSettings in the main window c'tor then add

          void showEvent(QShowEvent* event) override
          {
              readSettings();
              QMainWindow::showEvent(event);
          }
      
      1 Reply Last reply
      0
      • M Musmuris

        Hi,
        I have a QMainWindow and saving the state as per the docs (saveSate and restoreState - and geometry). The docks all have unique objectName set. The are added after the constructor is called - but before the app gets going (i.e. before .exec())

        The readSettings is called in the c'tor of my main window (again copied from the docs and then when a dock is added restoreDockWidget is called and returns true for each dock. However it just seems to do nothing - and the state is as they were. i.e. if I move them round, exit and restart, then they are back where were originally.

        The ini file seems to be created (and updated) each time - state is obviously an opaque byte array (though it does have the dock names if one coverts to ascii!)

        // aDock is created and object name set then passed to QMainWindow:
        aDock->setAllowedAreas(Qt::AllDockWidgetAreas);
        aDock->setAttribute(Qt::WA_DeleteOnClose, false);
        
        auto act = aDock->toggleViewAction();
        act->setIcon(icon);
        displaysToolBar->addAction(act);
        
        addDockWidget(Qt::TopDockWidgetArea, aDock);
        restoreDockWidget(aDock)
        

        Is the above expected to work?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Musmuris
        I think so, though I haven't used it. A while ago I think there were posts about dock area restoration issues, and maybe some work done on it. So you should probably state your exact Qt version and platform.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #3

          Hi,

          Can you provide a minimal example ?
          The order of the operation you wrote are not clear.
          To me it seems you are loading your settings too early with regard to how you construct your objects.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Musmuris
            wrote on last edited by Musmuris
            #4

            Sure - this is the essence of it:

            #include <QtWidgets>
            
            
            class MyDock: public QDockWidget
            {
            public:
                MyDock(const QString& title, const Qt::WindowFlags& flags = Qt::WindowFlags())
                    : QDockWidget(title, nullptr, flags)
                {
                    setWidget(new QLabel("test"));
                }
            
                ~MyDock() override = default;
            };
            
            class MyDockWindow : public QMainWindow
            {
                //Q_OBJECT
            
            public:
                MyDockWindow()
                {
                    m_docksToolBar = addToolBar("docks");
                    addToolBar(Qt::LeftToolBarArea, m_docksToolBar);
                    m_docksToolBar->setIconSize(QSize(40, 40));
                    m_docksToolBar->setMovable(false);
                    m_docksToolBar->setFloatable(false);
            
                    readSettings();
                }
            
                ~MyDockWindow() = default;
            
                void addDock(const QString& id, MyDock* myDock, const QIcon& icon)
                {
                    myDock->setAllowedAreas(Qt::AllDockWidgetAreas);
                    myDock->setAttribute(Qt::WA_DeleteOnClose, false);
                    myDock->setObjectName(id);
            
                    auto act = myDock->toggleViewAction();
                    act->setIcon(icon);
                    m_docksToolBar->addAction(act);
            
                    addDockWidget(Qt::TopDockWidgetArea, myDock);
                    //myDock->setVisible(false);
                    qDebug("%s %d\n", id.toStdString().c_str(), restoreDockWidget(myDock));
                }
            
            
            protected:
                void closeEvent(QCloseEvent* event) override
                {
                    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "dummy", "app");
                    settings.setValue("geometry", saveGeometry());
                    settings.setValue("windowState", saveState());
                    QMainWindow::closeEvent(event);
                }
            
            private:
                void readSettings()
                {
                    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "dummy", "app");
                    restoreGeometry(settings.value("geometry").toByteArray());
                    restoreState(settings.value("windowState").toByteArray());
                }
            
                QToolBar* m_docksToolBar;    
            };
            
            int main(int argc, char *argv[])
            {
                QApplication a(argc, argv);
                MyDockWindow w;
            
                auto aDock = new MyDock("hello 1");
                w.addDock("hello1", aDock, QIcon::fromTheme(QIcon::ThemeIcon::Computer));
                aDock = new MyDock("hello 2");
                w.addDock("hello2", aDock, QIcon::fromTheme(QIcon::ThemeIcon::DriveOptical));
            
                w.show();
                return a.exec();
            }
            

            The docks available will be configurable in the main app - hence adding this way. I did also try restoring state when the window is shown (i.e. after all docks have been added).

            EDIT: AH! Hang on - if I try restoring state in the showEvent on the mainwindow in the above test app then it works. Will just try that in the real one.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Musmuris
              wrote on last edited by
              #5

              And it turns out I was calling windows Show before adding the docks :facepalm:

              For those following at home - the above code minus the readSettings in the main window c'tor then add

                  void showEvent(QShowEvent* event) override
                  {
                      readSettings();
                      QMainWindow::showEvent(event);
                  }
              
              1 Reply Last reply
              0
              • M Musmuris has marked this topic as solved on
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                It might not be the implementation you want, showEvent can be called several times through the lifetime of your application so your GUI will give your user surprises when maximizing from a minimized state for example.

                I would recommend using a single shot QTimer with a 0 delay in your main.cpp to call readSettings.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  Musmuris
                  wrote on last edited by
                  #7

                  Ah - good to know. Thanks - I can make sure it's a one shot

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved