Between two dockwidgets, how to change the size of one dock when the other is changed?



  • I have two dockwidgets (one Top and one bottom). In both of them, there are some other widgets. I hope that when the size of one dock is changed, the other one (as well as the Inside widgets) will be changed accordingly.
    My question is that, once I incrise the size of the buttom dock, and decrise it again, the top dock will not be changed accordingly. Please see the picture as follows:
    0_1506843285823_snapshot1.JPG

    Does anyone can help to make it work?
    Please find the .ui file as well here: [0_1506843876276_QtGuiApplication2.ui](Uploading 100%)
    Thanks in advance!


  • Qt Champions 2016

    Hi
    Did you use layout for the inside widgets?
    That will normally take care of scaling to the size of the dock so they take up all space.



  • Sorry, as a new user, I am not allowed to upload .ui file. but i hope that it will be not difficult to see the problem. Looking forwards to any suggestion and remark. Thanks a lot!


  • Qt Champions 2016

    @kaien
    Im not sure any of us can upload Ui files. :)

    They are text, so you can just paste it as text here.

    And welcome to the forums.


  • Qt Champions 2016

    Anyway
    if your issue is that widgets you place inside the dock do not follow
    the dock size, you should have a look at layouts
    http://doc.qt.io/qt-5/layout.html
    They are for that.



  • @mrjj Thanks for your reply and suggestions. Here is the ui file:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>QtGuiApplication2Class</class>
     <widget class="QMainWindow" name="QtGuiApplication2Class">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>664</width>
        <height>485</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>QtGuiApplication2</string>
      </property>
      <widget class="QWidget" name="centralWidget"/>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>664</width>
         <height>23</height>
        </rect>
       </property>
      </widget>
      <widget class="QDockWidget" name="dockWidget_3">
       <attribute name="dockWidgetArea">
        <number>4</number>
       </attribute>
       <widget class="QWidget" name="dockWidgetContents_3">
        <layout class="QVBoxLayout" name="verticalLayout_2">
         <item>
          <widget class="QMdiArea" name="mdiArea_2"/>
         </item>
        </layout>
       </widget>
      </widget>
      <widget class="QDockWidget" name="dockWidget_4">
       <attribute name="dockWidgetArea">
        <number>8</number>
       </attribute>
       <widget class="QWidget" name="dockWidgetContents_4">
        <layout class="QVBoxLayout" name="verticalLayout">
         <item>
          <widget class="QListWidget" name="listWidget"/>
         </item>
        </layout>
       </widget>
      </widget>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources>
      <include location="QtGuiApplication2.qrc"/>
     </resources>
     <connections/>
    </ui>
    
    

    Thanks a lot for your help.


  • Qt Champions 2016

    Hi
    It seems you did use layout ?
    If you float the dock and drag the border , does the inside widgets rescale ?



  • @mrjj said in Between two dockwidgets, how to change the size of one dock when the other is changed?:

    Hi
    It seems you did use layout ?
    If you float the dock and drag the border , does the inside widgets rescale ?

    The Inside widgets of top dock rescale when the bottom one up, but not change when the bottom one down.


  • Qt Champions 2016

    @kaien
    that sounds odd , they should always rescale no matter if up or down.

    If you play around with this
    http://doc.qt.io/qt-5/qtwidgets-mainwindows-mainwindow-example.html
    you will see that there is never space between the docks
    so i think its inside widgets that something up.

    So my best guess is that your layout inside dock widget is not working since it dont
    take all space.

    Show the code that creates the docks.



  • @mrjj I've tried some ways to use layout.

    1. The first think is to set a QVBoxLayout as a centralWidget, then I can put two docks Inside.
      Here is the code
    #include "stdafx.h"
    #include "QtGuiApplication2.h"
    
    QtGuiApplication2::QtGuiApplication2(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    	QVBoxLayout *mainlayout = new QVBoxLayout(this);
    	mainlayout->addWidget(ui.dockWidget);
    	mainlayout->addWidget(ui.dockWidget_4);
    	ui.centralWidget->setLayout(mainlayout);
    }
    

    However, this is not what I want since I found that I can not drag the border of docks anymore, and once it float, it can not be docked again.

    1. The second try is to add top dock Inside of centralWidget as follows:
    #include "stdafx.h"
    #include "QtGuiApplication2.h"
    
    QtGuiApplication2::QtGuiApplication2(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    	QVBoxLayout *mainlayout = new QVBoxLayout(this);
    	mainlayout->addWidget(ui.dockWidget);
    	ui.centralWidget->setLayout(mainlayout);
    }
    

    This time, I really get what I want without that gap, but I can not dock anymore the top dockWidget!

    1. If I don't define centralWidget and just use defaut setting as:
      0_1506849424868_snapshot2.JPG

    Then I can dock the two dockwidgets, but I will get the problem asked in this post.

    So could you please give me an example to show how to do it in a correct way.
    Thanks a lot!



  • @mrjj Here is all codes for this sample:

    • main.cpp
    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    • mainwindow.cpp
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    • mainwindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    
    • mainwindow.ui
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>746</width>
        <height>500</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralWidget"/>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>746</width>
         <height>23</height>
        </rect>
       </property>
      </widget>
      <widget class="QToolBar" name="mainToolBar">
       <attribute name="toolBarArea">
        <enum>TopToolBarArea</enum>
       </attribute>
       <attribute name="toolBarBreak">
        <bool>false</bool>
       </attribute>
      </widget>
      <widget class="QStatusBar" name="statusBar"/>
      <widget class="QDockWidget" name="dockWidget">
       <attribute name="dockWidgetArea">
        <number>4</number>
       </attribute>
       <widget class="QWidget" name="dockWidgetContents">
        <layout class="QVBoxLayout" name="verticalLayout_2">
         <item>
          <widget class="QMdiArea" name="mdiArea"/>
         </item>
        </layout>
       </widget>
      </widget>
      <widget class="QDockWidget" name="dockWidget_2">
       <attribute name="dockWidgetArea">
        <number>8</number>
       </attribute>
       <widget class="QWidget" name="dockWidgetContents_2">
        <layout class="QVBoxLayout" name="verticalLayout">
         <item>
          <widget class="QListWidget" name="listWidget"/>
         </item>
        </layout>
       </widget>
      </widget>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    
    


  • Ok, very a lot of tests, finally, I found a way to solve the problem.
    We just need to add central Widget as of one of the dock widget, for example, the top widget as

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        setCentralWidget(ui.dockWidget);
    }
    
    

    Then everything will be all right! I got excatly what I want :)
    Thanks @mrjj for your discussion.


  • Qt Champions 2016

    @kaien
    Super
    Normally the docks only likes do dock to the sides of central but it seems you
    found a way :)


Log in to reply
 

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