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. How to split QDockWidgets with equal size?

How to split QDockWidgets with equal size?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qdockwidgetmainwindow
8 Posts 3 Posters 4.3k 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.
  • Z Offline
    Z Offline
    Zylann
    wrote on 19 Nov 2015, 14:49 last edited by
    #1

    In my application I allow the user to split docks vertically or horizontally, using splitDockWidget(), so if the user splits the first dock and then splits again the two it can get 2x2 docks of equal size without having to adjust them.

    Horizontally, splits get equal width. However, when I split vertically, the second dock gets no height Oo

    I searched on the web and essentially found doc quotes recommending to set size policies in the widget inside the QDockWidget.
    But I have no idea how to tell "take half the space when splitting a dock" with these policies :(
    I tried to resize the content before docking, no effect at all.
    I also don't get why it works only horizontally...

    How can I do this?

    1 Reply Last reply
    1
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 19 Nov 2015, 22:06 last edited by
      #2

      Hi,

      No height ?

      Can you provide a minimal code sample that shows that behavior ?

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

      Z 1 Reply Last reply 20 Nov 2015, 10:24
      0
      • S SGaist
        19 Nov 2015, 22:06

        Hi,

        No height ?

        Can you provide a minimal code sample that shows that behavior ?

        Z Offline
        Z Offline
        Zylann
        wrote on 20 Nov 2015, 10:24 last edited by Zylann
        #3

        @SGaist By "no height", I mean it's tiny, or has a zero-height.
        I reproduced the behaviour with this code:

        Header

        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
        
        private slots:
            void splitDockHorizontal();
            void splitDockVertical();
        
        private:
            QDockWidget * m_dock1;
        };
        

        Implementation

        MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
        {
            setDockNestingEnabled(true);
        
            m_dock1 = new QDockWidget("Dock 1", this);
        
            addDockWidget(Qt::LeftDockWidgetArea, m_dock1);
        
            QWidget * dock1Content = new QWidget(this);
            QVBoxLayout * boxLayout = new QVBoxLayout();
            dock1Content->setLayout(boxLayout);
            QPushButton * btnH = new QPushButton("SplitH", dock1Content);
            QPushButton * btnV = new QPushButton("SplitV", dock1Content);
            boxLayout->addWidget(btnH);
            boxLayout->addWidget(btnV);
            connect(btnH, SIGNAL(clicked()), this, SLOT(splitDockHorizontal()));
            connect(btnV, SIGNAL(clicked()), this, SLOT(splitDockVertical()));
            m_dock1->setWidget(dock1Content);
        }
        
        MainWindow::~MainWindow()
        {
        }
        
        void MainWindow::splitDockHorizontal()
        {
            QWidget * content = new QWidget(this);
        
            QDockWidget * dock = new QDockWidget("Dock +", this);
            dock->setWidget(content);
            splitDockWidget(m_dock1, dock, Qt::Horizontal);
        }
        
        void MainWindow::splitDockVertical()
        {
            QWidget * content = new QWidget(this);
        
            QDockWidget * dock = new QDockWidget("Dock +", this);
            dock->setWidget(content);
            splitDockWidget(m_dock1, dock, Qt::Vertical);
        }
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 21 Nov 2015, 23:32 last edited by
          #4

          That's because when you create your new widget they are empty so splitting horizontally you have the height of m_dock1 that also applied to your newly created dock however when splitting vertically only the width will be used.

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

          Z 1 Reply Last reply 23 Nov 2015, 09:33
          0
          • S SGaist
            21 Nov 2015, 23:32

            That's because when you create your new widget they are empty so splitting horizontally you have the height of m_dock1 that also applied to your newly created dock however when splitting vertically only the width will be used.

            Z Offline
            Z Offline
            Zylann
            wrote on 23 Nov 2015, 09:33 last edited by Zylann
            #5

            @SGaist I know my widget is empty, however it should not prevent it from getting half the size. In my use case, it actually contains a native window (with no Qt widget inside), which has no preferred size. If I were to put it floating, maybe I could set it a size, but here I'm clueless. That's why I also tried to resize it before docking, but it doesnt works...

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 23 Nov 2015, 21:11 last edited by
              #6

              In that case you should settle for a size you know is good for that window and set it on the widget

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

              R 1 Reply Last reply 8 Feb 2025, 19:04
              0
              • S SGaist
                23 Nov 2015, 21:11

                In that case you should settle for a size you know is good for that window and set it on the widget

                R Online
                R Online
                RokeJulianLockhart
                wrote on 8 Feb 2025, 19:04 last edited by
                #7

                @SGaist, surely there is a way to get it to dynamically adjust, though? With the disparate form factors available today (think Linux phones) setting hardcoded sizes seems like a poor idea.

                When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

                S 1 Reply Last reply 8 Feb 2025, 20:39
                0
                • R RokeJulianLockhart
                  8 Feb 2025, 19:04

                  @SGaist, surely there is a way to get it to dynamically adjust, though? With the disparate form factors available today (think Linux phones) setting hardcoded sizes seems like a poor idea.

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 8 Feb 2025, 20:39 last edited by
                  #8

                  @RokeJulianLockhart if you take the original situation from almost ten years ago, it was for desktop and the dock widget contained an alien widget which did not provide any useful size information.
                  Hard coded size are rarely a good option but you have to make a decision at some point.

                  In between, there has been alternative implementations to the QMainWindow layout system that have been created that might handle that case better.

                  You can also check the implementation of splitDockWidgets to see how the size are computed and how you might (or not) influence it to do a 50/50 split.

                  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

                  • Login

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