Question

Is it possible to have a QSplitter which has one widget of a fixed size and the other with a variable size?

This code sets things right, but when the window is resized the splitter changes proportionally to the initial widgets sizes:

vSplitter = new QSplitter(Qt::Vertical, this);
vSplitter->addWidget(widget1);
vSplitter->addWidget(widget2);
QList<int> heights;
heights.push_back(550);
heights.push_back(1000);
vSplitter->setSizes(heights);

setCentralWidget(vSplitter);

Thank you.

Was it helpful?

Solution

Try this one:

QSplitter::setStretchFactor ( int index, int stretch )

Just set the stretch factor of the part you want to remain fixed size to 0 and set it to 1 on the other widget.

When resize the entire window, the widget with the stretch factor 0 won't resize.

OTHER TIPS

I'm trying to do something similar. I've got one fixed widget (top), and one non-fixed widget (bottom). I want the user to be able to use the splitter handle normally, but I don't want window resizes to add space to the fixed widget. Using setSizePolicy on both widgets didn't work for me, and neither did using setStretchFactor on either one or both widgets.

I ended up subclassing QSplitter so that I could implement a resizeEvent that would move the splitter after the splitter resized. This code assumes: There are only two widgets. The top [0] is resizeable, and the bottom [1] should not be.

Here is the resizeEvent that I wrote:

MySplitter::resizeEvent(QResizeEvent *event) {
    /* The first resizeEvent is -1 for height and width, because it was
       invisible before. */
    if (event->oldSize().height() != -1) {
        int diff;
        QList<int> previousSizes = sizes();
        QSplitter::resizeEvent(event);
        QList<int> newSizes = sizes();
        /* The bottom widget is the fixed one, so find out if that is to
           grow or shrink. */
        diff = newSizes[1] - previousSizes[1];
        if (diff > 0) {
            /* Keep the bottom from growing by giving the size to the top. */
            newSizes[0] += diff;
            newSizes[1] -= diff;
        }
        else {
            /* Steal size from the top to keep it from shrinking. */
            newSizes[0] -= diff;
            newSizes[1] += diff;
        }
        setSizes(newSizes);
    }
    else
        QSplitter::resizeEvent(event);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top