QSplitter with one fixed size widget and one variable size widget?

11,984

Solution 1

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.

Solution 2

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);
}
Share:
11,984
Pietro
Author by

Pietro

C++ software developer. Particularly interested in software for technical/scientific applications, also in a research and development environment, following the full lifecycle of projects where possible. Interested in Standard C++, Boost, GPGPU, OpenCL, (spiking/convolutional/biological) neural networks, genetic algorithms, databases, ... and a few more things.

Updated on June 20, 2022

Comments

  • Pietro
    Pietro about 2 years

    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.

  • Olumide
    Olumide about 5 years
    A bit late but take a look at this
  • IceFire
    IceFire about 4 years
    setStretchFactor is not reliable at all