QMainWindow: size management of central widget and dock widgets



  • I want mainwindow to keep approximate ratio of dock widget sizes and central widget size, when the window's size is changed. I've written simplest mainwindow with central widget and 6 dock widgets:

    #include <QPlainTextEdit>
    #include <QDockWidget>
    
    #include "mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        this->setGeometry(20, 20, 600, 600);
    
        this->setCentralWidget(
              new QPlainTextEdit("central", this)
              );
    
        QDockWidget *p_dw;
    
        for (int i = 0; i < 6; ++i){
           p_dw = new QDockWidget("dock " + QString::number(i));
    
           p_dw->setWidget(
                 new QPlainTextEdit("dock " + QString::number(i), this)
                 );
    
           Qt::DockWidgetArea area;
           switch (i){
              case 0:
              case 1:
                 area = Qt::TopDockWidgetArea;
                 break;
              case 2:
                 area = Qt::RightDockWidgetArea;
                 break;
              case 3:
              case 4:
                 area = Qt::BottomDockWidgetArea;
                 break;
              default:
                 area = Qt::LeftDockWidgetArea;
                 break;
           }
    
           addDockWidget(area, p_dw);
        }
    }
    

    When started, it looks as follows:

    That's fine. I can shrink it:

    And expand back, and after expanded, it looks as at the first screenshot. It is fine.

    The problem is that if user "touches" central widget bounds (I mean, changes the central widget size manually, ever for a little bit), then main window changes its policy dramatically: after user touches central widget, then shrinks window, and then expands it back, all dockwidgets have their minimum sizes:

    The question is, how to prevent this behavior? Or, more generally, how can I influence on the size policy of QMainWindow? I can't find any documentation about the behaviour I've explained above.

    Ideally, I want all the widgets (both dockwidgets and central widget) to keep their relative ratio when overall window size is changed. For example, when window is large, central widget occupies, say, 20% of horizontal space. So, when window is shrinked by user, it should occupy that same 20% of horizontal space. The same is for vertical space, of course. So when we set up some widget arrangement, then shrink window, then expand back, we should get the same picture.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.