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. QDockWidget::restoreGeometry not working correctly when QMainWindow is maximized
Forum Updated to NodeBB v4.3 + New Features

QDockWidget::restoreGeometry not working correctly when QMainWindow is maximized

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.5qdockwidgetqmainwindow
6 Posts 3 Posters 7.4k Views 2 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.
  • S Offline
    S Offline
    skebanga
    wrote on last edited by skebanga
    #1

    I have a number of QDockWidgets, all docked in a single QMainWindow.

    I have overridden the showEvent, and after passing the event on to the base class I am restoring the dock widget's geometry

    void DockWidget::showEvent(QShowEvent* ev) 
    {
        QDockWidget::showEvent(ev);
    
        QByteArray byte_array = settings_.value(id + ".geometry").toByteArray();
    
        LOG("rest: %s", QString(byte_array.toHex()));
    
        QDockWidget::restoreGeometry(byte_array);
    }
    

    In my QMainWindow::closeEvent I am calling saveSettings for each of my dock widgets

    void MainWindow::closeEvent(QCloseEvent* ev) 
    {
        QList<DockWidget*> dock_widgets = findChildren<DockWidget*>();
        
        for (DockWidget* dock_widget : dock_widgets)
            dock_widget->saveSettings();
    
        QMainWindow::closeEvent(ev);
    }
    

    In my dock widget's saveSettings function I write the result of saveGeometry to disk:

    void DockWidget::saveSettings()
    {
        QByteArray byte_array = QDockWidget::saveGeometry();
    
        LOG("save: %s", QString(byte_array.toHex()));
    
        settings_.setValue(id + ".geometry", byte_array);
        settings_.sync();
    }
    

    Whilst this does work when my QMainWindow is not maximized, when it is maximized the widgets aren't restored correctly.

    In this image I have arranged my widgets just prior to closing. (linked because inline the image will be too large)

    In this image I reload my app and the widgets' geometry is incorrectly loaded.

    You can see in my functions above I log the geometry string being saved and loaded.

    I have proven to myself that the settings are correctly saved and restored again, but somehow it's not working correctly

    close the app; save the state:

    save: 01d9d0cb000200000000053f000000640000077f000001a00000053f000000640000077f000001a000000000000000000780
    

    open the app; restore the state: (the hex data here matches the saved one exactly)

    rest: 01d9d0cb000200000000053f000000640000077f000001a00000053f000000640000077f000001a000000000000000000780
    

    close the app again, having touched nothing: (the hex data is different now, as the geometry is different, see marks below)

    save: 01d9d0cb000200000000053f000000640000077f0000014e0000053f000000640000077f0000014e00000000000000000780
                                                        ^                               ^
    

    This doesn't happen when the window is not maximized.

    Is this a bug in Qt, or am I not using the functionality correctly?

    I am using Qt 5.5 on Ubuntu 16.04.

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

      Hi,

      Usually you don't do it per-widget but through QMainWindow::saveState which include dock widgets handling. The only thing to keep in mind is that you need to give objects name to all your dock widgets.

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

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Usually you don't do it per-widget but through QMainWindow::saveState which include dock widgets handling. The only thing to keep in mind is that you need to give objects name to all your dock widgets.

        S Offline
        S Offline
        skebanga
        wrote on last edited by
        #3

        @SGaist thanks for the input, it is very much appreciated!

        Thanks also for the heads up on QDockWidgets not needing to use saveGeometry / restoreGeometry, I have removed that.

        Using saveGeometry / restoreGeometry on a QDockWidget obviously has no effect, as my app's behaviour is the same as it was before with the code removed.

        I am using unique objectNames - I'm using boost::uuids::uuid to generate my object name (and am also checking its unique as well, although whether that's necessary is questionable)

        std::string new_widget_id;
        do
        {
        	new_widget_id = to_string(boost::uuids::random_generator()());
        }
        while (workspace->widgetIdExists(new_widget_id));
        
        setObjectName(QString::fromStdString(new_widget_id));
        

        All that said, the above change (removing saveGeometry / restoreGeometry from my dock widgets) doesn't have any effect in regards to the behaviour I'm seeing.

        I have, however, been pointed to the following 2 bugs.

        • QTBUG-16252 - QDockWidgets of maximized windows are not restored to the correct size when calling QWidget::restoreGeometry() , QMainWindow::restoreState() in a sequence
        • QTBUG-46620 - QMainWindow:restoreState doesnt restore width of QDockWidgets in maximized QMainWindow

        Rather unfortunately the 1st one, which seems to be the one I'm hitting, was first reported in 2010, so I'm not holding out hope that it's going to be resolved.

        There are, however, several possible workaround mentioned in the comments of the 2 bug reports, so I'll try those out and report back if I find a solution

        S 1 Reply Last reply
        0
        • S skebanga

          @SGaist thanks for the input, it is very much appreciated!

          Thanks also for the heads up on QDockWidgets not needing to use saveGeometry / restoreGeometry, I have removed that.

          Using saveGeometry / restoreGeometry on a QDockWidget obviously has no effect, as my app's behaviour is the same as it was before with the code removed.

          I am using unique objectNames - I'm using boost::uuids::uuid to generate my object name (and am also checking its unique as well, although whether that's necessary is questionable)

          std::string new_widget_id;
          do
          {
          	new_widget_id = to_string(boost::uuids::random_generator()());
          }
          while (workspace->widgetIdExists(new_widget_id));
          
          setObjectName(QString::fromStdString(new_widget_id));
          

          All that said, the above change (removing saveGeometry / restoreGeometry from my dock widgets) doesn't have any effect in regards to the behaviour I'm seeing.

          I have, however, been pointed to the following 2 bugs.

          • QTBUG-16252 - QDockWidgets of maximized windows are not restored to the correct size when calling QWidget::restoreGeometry() , QMainWindow::restoreState() in a sequence
          • QTBUG-46620 - QMainWindow:restoreState doesnt restore width of QDockWidgets in maximized QMainWindow

          Rather unfortunately the 1st one, which seems to be the one I'm hitting, was first reported in 2010, so I'm not holding out hope that it's going to be resolved.

          There are, however, several possible workaround mentioned in the comments of the 2 bug reports, so I'll try those out and report back if I find a solution

          S Offline
          S Offline
          skebanga
          wrote on last edited by
          #4

          I was able to fix the problem by using this suggestion in the comments on bug QTBUG-16252.

          The solution is to use QWidget::geometry and QWidget::setGeometry to store and load a QRect, instead of saveGeometry / restoreGeometry and the associated QByteArray

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

            Great !

            Thanks for coming back with the information !

            Since you could make it work, please mark the thread as solved so other forum users may know a solution has been found :)

            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
            • BagherSadeghzadehB Offline
              BagherSadeghzadehB Offline
              BagherSadeghzadeh
              wrote on last edited by BagherSadeghzadeh
              #6

              You should care about QSizePolicy and sizeHint of your widgets inside QDockWidget. ;)

              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