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. Initial Position QSplitter

Initial Position QSplitter

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 11.0k 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.
  • M Offline
    M Offline
    ModelTech
    wrote on 28 Aug 2016, 13:38 last edited by
    #1

    I would like to set the initial position of a vertical QSplitter such that each of the two containing widgets get 50% of the available height. How can I achieve this with QSplitter::setSizes? I have tried the following, but it does not always give the same result (i.e., when one of the containing widgets is of a different class). Any ideas on how to fix this?

    QSplitter *Splitter = new QSpliter(Qt::Vertical);
    Splitter->addWidget(Widget1);
    Splitter->addWidget(Widget2);    // Widget2 can be an instance of different subclasses of QWidget
    QList<int> Sizes;
    Sizes.append(0.5 * height());
    Sizes.append(0.5 * height());
    Splitter->setSizes(Sizes);
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Aug 2016, 15:59 last edited by
      #2

      Hi,

      Do you take into account the minimal size of the widgets as well as their size policies ? From the doc, they will be respected.

      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
      0
      • M Offline
        M Offline
        ModelTech
        wrote on 28 Aug 2016, 16:30 last edited by
        #3

        I tried to play with the percentages, but that does not have any effects. The containing widgets are however resizable to a larger and smaller size than what they initially are, so I would expect that there is no problem with the MinimalSizeHits.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 29 Aug 2016, 20:32 last edited by
          #4

          What value does height() return ?

          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
          0
          • M Offline
            M Offline
            ModelTech
            wrote on 31 Aug 2016, 20:46 last edited by
            #5

            It returns the same value 30 independent of the type of the containing widget that may be of a different type (which is what I would expect). Not sure however whether 30 is rather small.

            What is actually not clear to me for the QSplitter::setSizes function is whether the given size values are considered as relative to each other or whether they are considered as absolute values.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 1 Sept 2016, 07:55 last edited by
              #6

              You should rather use sizeHint. I'm guessing that you are calling that from the constructor. At that time, the widget has not yet any idea of its final size when shown. All the more since you put your widgets in a "floating" QSplitter. Floating because it doesn't seem to be in any layout or QMainWindow.

              Since 30 is likely less than the minimal size of your other widgets. setSize will use their sizeHint to try to come up with something suitable.

              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
              0
              • V Offline
                V Offline
                VRonin
                wrote on 1 Sept 2016, 08:14 last edited by
                #7

                did you try giving the same value to the stretch factor?

                Splitter->setStretchFactor(0,2);
                Splitter->setStretchFactor(1,2);
                

                "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
                1
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 1 Sept 2016, 10:54 last edited by
                  #8

                  @VRonin's solution is indeed cleaner.

                  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
                  • M Offline
                    M Offline
                    ModelTech
                    wrote on 1 Sept 2016, 18:40 last edited by ModelTech 9 Jan 2016, 18:51
                    #9

                    Thanks to your remark @SGaist on the widget not yet having an idea of its final size at its construction, I have been able to solve it as indicated below. I should call setSize after setting the layout of the widget it is part of. I also applied your hint on using sizeHint.

                        QWidget *TopWidget = new QWidget(this);
                        TopWidget->setLayout(Top);
                        Splitter = new QSplitter(Qt::Vertical);
                        Splitter->addWidget(TopWidget);
                        Splitter->addWidget(Behavior);
                        Splitter->setCollapsible(0, false);
                        Splitter->setCollapsible(1, false);
                    
                        QVBoxLayout *Main = new QVBoxLayout;
                        Main->addWidget(Splitter);
                        setLayout(Main);
                    
                        QList<int> Sizes;
                        Sizes.append(0.5 * sizeHint().height());
                        Sizes.append(0.5 * sizeHint().height());
                        Splitter->setSizes(Sizes);
                    

                    The approach of using setStrechFactor instead of setSize does not work...

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 1 Sept 2016, 19:56 last edited by
                      #10

                      Silly question but did you remove the call to setSize when using setStretchFactor ?

                      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
                      0
                      • M Offline
                        M Offline
                        ModelTech
                        wrote on 1 Sept 2016, 20:00 last edited by
                        #11

                        Eh yes, apparently I should not have done so?

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 1 Sept 2016, 20:21 last edited by
                          #12

                          No, no, that was right.

                          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
                          0

                          4/12

                          29 Aug 2016, 20:32

                          8 unread
                          • Login

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