Search code examples
qtqsplitter

replace widget in Qsplitter without losing the stretch factor


I have 2 widgets(buttons) in the same splliter and i want to replace the first one with Tabwidget.. i lose the right stretch factor (1:1) it will be like (2:1) or not like the old factor (when it's just 2 buttons)

splitter->addwidget(qbut1);
splitter->addwidget(qbut2);
splitter->insertwidget(0,tab);

and even when i add at the first one tab and one button..tab takes size more than button how can i make it (1:1) i try

splitter->setStretchFactor(0,1);
splitter->setStretchFactor(1,1);

but it doesn't work


Solution

  • You can set the sizes of your splits with QSplitter::setSizes. To achieve a one to one ratio you could use something like this:

    int width = splitter->width();
    QList<int> sizes;
    sizes << width/2 << width/2;
    splitter->setSizes(sizes);
    

    Beware that this only sets initial sizes, the user can still resize them at will. Also, re-read the documentation on stretch factors, it sounds as if you may have misunderstood their meaning.