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. QMainWindow: size management of central widget and dock widgets
QtWS25 Last Chance

QMainWindow: size management of central widget and dock widgets

Scheduled Pinned Locked Moved General and Desktop
qmainwindowdockwidgetsizepolicy
1 Posts 1 Posters 3.2k Views
  • 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.
  • D Offline
    D Offline
    dimonomid
    wrote on last edited by dimonomid
    #1

    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.

    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