How to prevent QSplitter child from collapsing before completely minimizing its size to 0?
-
Hi,
I'm trying to get a horizontal
QSplitter
to allow me to resize one of its children's width to 0 without it collapsing before reaching the edge.Here's an example of
QSplitter
with 2 children, aQWidget
, and aQPushButton
, where the button collapses before I drag it to the edge:Minimal Reproducible example:
QSplitter *splitter = new QSplitter(); QWidget *widget = new QWidget; QPushButton *button = new QPushButton("button"); widget->setStyleSheet("background: darkblue;"); splitter->addWidget(button); splitter->addWidget(widget); splitter->show(); splitter->setMinimumSize(100,100);
I tried:
-
setChildrenCollapsible(false), but it results in the button not not budging below a certain width, here's how it looks:
-
Setting minimum width of
QSplitter
and its children to 0, setting their sizes manually below the size it won't go below, changing size policy toignored
, but it made no difference.
Using an event filter, I noticed a QInputMethodQueryEvent triggered when the button collapses, but I do not know how to use that.
I also noticed that the behavior I'm looking to achieve is possible with
QWidget
,QFrame
, here's how it looks with 2QWidgets
in aQSplitter
:Based on this observation, I managed to find a workaround, by placing the widget I need (button for example) inside a
QWidget
, and making the container widget a child ofQSplitter
, and it works, but I'd rather avoid using this method.What is causing this behavior? And how do I change it so that the button resizes until its width reaches 0 without collapsing?
Thank you for your time, effort and interest!
-
-
@Abderrahmene_Rayene set minimum width of button to 0? Its minimum width may not be 0 because it has text.
-
@JoeCFD Thanks for the link
I was already setting its minimum width explicitly as follows:
button->setMinimumWidth(0);
and just to check if it is actually set, I used this:
button->connect(button,&QPushButton::clicked,[button]() { button->setMinimumWidth(0); qDebug()<<button->minimumWidth(); qDebug()<<button->size(); });
I resized the button and clicked it to check its size multiple times, it is not arbitrary, and the minimum width is set to 0. I'm not sure this is the correct way to check it, but that's what could think of
I tried the other answer, but same result, the only difference is that it made its size smaller when I used no text, but the "snappy" collapsing still occurs without text.
-
@Abderrahmene_Rayene Try also to set minimumSizeHint = 0?
or use QToolButton? Or create your own push button. -
@JoeCFD Same thing, I'm thinking every compound widget behaves like this in a
QSplitter
(QTextEdit
,QComboBox
...), but as I've mentioned, if I place them in a containerQWidget
, I get the desired result (no snappy collapsing), I don't understand why, but it seems this is the only possible solution.I thought about sub-classing the widgets I want to use, and somehow resize them before they collapse, I'm not sure this even works, and it seems like too much trouble and chaos compared to the container method.
P.S: Sorry if me using a button is misleading, I'm just using it as an example.
-
@Abderrahmene_Rayene This is interesting. Maybe you can print out the stylesheets of qpushbutton and qwidget as its container. You may be able to see some differences.
-