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. Resize window and grow subwidget
Forum Updated to NodeBB v4.3 + New Features

Resize window and grow subwidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 3 Posters 7.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.
  • S samdol

    Hi

    Picture
    I have a window which has three sub sections. ListView is at center widget and there are two dockWidget on left and right of ListView as you can see in A . When I resize window by dragging right side, ListView grows as B. My goal is to let Dock2 grow while Dock1 and ListView do not change as C. Is there simple way to do that?

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @samdol Did you try to change the sizePolicy property?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    4
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #3

      use QWidget::setSizePolicy set it to QSizePolicy::Expanding for the widgets you want to grow and QSizePolicy::Preferred for the rest. in your case listView->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Expanding); dock2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      S 1 Reply Last reply
      4
      • VRoninV VRonin

        use QWidget::setSizePolicy set it to QSizePolicy::Expanding for the widgets you want to grow and QSizePolicy::Preferred for the rest. in your case listView->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Expanding); dock2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);

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

        @VRonin
        That works well if those three widgets are like groupBoxes. But if the widget on left or right is the dockWidget, it did not work as expected. But interestingly, dockWidget also has setSizePolicy() functions. Would it possibly be a bug in Qt?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #5

          if listView is the central widget, try setting listView->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Expanding); after you made it a central widget

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          S 1 Reply Last reply
          2
          • VRoninV VRonin

            if listView is the central widget, try setting listView->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Expanding); after you made it a central widget

            S Offline
            S Offline
            samdol
            wrote on last edited by
            #6

            @VRonin
            It works partially. When I drag righthand side, initially listView grows while docks does not grow. After listView reaches the certain size, it does not grow while docks grow. Here is the code I tried,

            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent)
            {
                this->resize(300, 300);
                groupBox_main = new QGroupBox(this);
                QVBoxLayout *vlayout = new QVBoxLayout;
                groupBox_main->setLayout(vlayout);
                list_view = new QListView;
                vlayout->addWidget(list_view);
            
                groupBox_main->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Expanding);
                dockWidget_1 = new QDockWidget(this);
                addDockWidget(Qt::LeftDockWidgetArea, dockWidget_1);
                dockWidget_2 = new QDockWidget(this);
                dockWidget_2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
                this->addDockWidget(Qt::RightDockWidgetArea, dockWidget_2);
                setCentralWidget(groupBox_main);
            }
            

            Interesting thing is that if I don't include vlayout->addWidget(list_view); listview never grows while I drag the righthand side which is what I wanted.

            VRoninV 1 Reply Last reply
            0
            • S samdol

              @VRonin
              It works partially. When I drag righthand side, initially listView grows while docks does not grow. After listView reaches the certain size, it does not grow while docks grow. Here is the code I tried,

              MainWindow::MainWindow(QWidget *parent) :
                  QMainWindow(parent)
              {
                  this->resize(300, 300);
                  groupBox_main = new QGroupBox(this);
                  QVBoxLayout *vlayout = new QVBoxLayout;
                  groupBox_main->setLayout(vlayout);
                  list_view = new QListView;
                  vlayout->addWidget(list_view);
              
                  groupBox_main->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Expanding);
                  dockWidget_1 = new QDockWidget(this);
                  addDockWidget(Qt::LeftDockWidgetArea, dockWidget_1);
                  dockWidget_2 = new QDockWidget(this);
                  dockWidget_2->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
                  this->addDockWidget(Qt::RightDockWidgetArea, dockWidget_2);
                  setCentralWidget(groupBox_main);
              }
              

              Interesting thing is that if I don't include vlayout->addWidget(list_view); listview never grows while I drag the righthand side which is what I wanted.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #7

              @samdol said in Resize window and grow subwidget:

              After listView reaches the certain size

              That certain size is listView->sizeHint() you can call listView->setSizeHint(/*QSize*/) to decide how much it should be

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              S 1 Reply Last reply
              1
              • VRoninV VRonin

                @samdol said in Resize window and grow subwidget:

                After listView reaches the certain size

                That certain size is listView->sizeHint() you can call listView->setSizeHint(/*QSize*/) to decide how much it should be

                S Offline
                S Offline
                samdol
                wrote on last edited by
                #8

                @VRonin
                Sorry, I can see QListView::sizeHint(), but I don't see the method QListView::setSizeHint(). It is quite strange.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #9

                  Sorry, my bad, use setMaximumSize instead

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  S 1 Reply Last reply
                  0
                  • VRoninV VRonin

                    Sorry, my bad, use setMaximumSize instead

                    S Offline
                    S Offline
                    samdol
                    wrote on last edited by
                    #10

                    @VRonin
                    I also tried that. But setMaximumSize limits the size of listView. I don't want to limit the size of listView. For example, I intend that if I drag handler between listView and dock2, listView can grow wihout limit while dragging right hand side only makes dock2 grow.

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #11

                      ok then just use setSizePolicy.

                      use QSizePolicy::Preferred for the listView and QSizePolicy::Maximum for the left dock

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      S 1 Reply Last reply
                      0
                      • VRoninV VRonin

                        ok then just use setSizePolicy.

                        use QSizePolicy::Preferred for the listView and QSizePolicy::Maximum for the left dock

                        S Offline
                        S Offline
                        samdol
                        wrote on last edited by
                        #12

                        @VRonin
                        That also makes no difference. listView grows upto sizeHint and then docks start grow. It is quite weird that listView and groupBox have sizeHint() but they don't have setSizeHint(). Then where those initial sizeHint values come from? Why they don't allow users to change it?

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #13

                          you can still subclass QListView and reimplement sizeHint() to return what you prefer

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          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