QSplitter with one fixed size widget and one variable size widget?
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);
}
![Pietro](https://i.stack.imgur.com/2RJ8I.png?s=256&g=1)
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, 2022Comments
-
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 about 5 yearsA bit late but take a look at this
-
IceFire about 4 years
setStretchFactor
is not reliable at all