Initial Position QSplitter



  • 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);
    

  • Lifetime Qt Champion

    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.



  • 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.


  • Lifetime Qt Champion

    What value does height() return ?



  • 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.


  • Lifetime Qt Champion

    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.



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

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

  • Lifetime Qt Champion

    @VRonin's solution is indeed cleaner.



  • 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...


  • Lifetime Qt Champion

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



  • Eh yes, apparently I should not have done so?


  • Lifetime Qt Champion

    No, no, that was right.


Log in to reply
 

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