Finding the "snap point" for a splitter ?
-
I have a layout where I have 3 widgets left, center and right laid out in a horizontal splitter (so they're from left to right side-by-side).
The problem is with the initial size of the splitter. The left side is initially way too large.
The splitter has a "snap" point that seems to be some kind of minimum acceptable size if you drag it smaller it'll completely close.
How to find this snap point and adjust the splitter so that both the
left and right sides are at the snap point and the center widget has maximum space?Thanks!
-
According to my experience, the "snap point" is indeed the minimum size, but not the value
minimumSize()
returned (unless you have set it), butminimumSizeHint()
if you have layout set on the widget.
Are you suresetStretchFactor()
is not working? I think it should if you set the center widget's strech to1
and leave the other two untouched (0). (Or you can just set the center widget'sHorizontal Stretch
to1
if you are using designer.) -
Hi,
Isn't it the minimal size of the widget ?
That said, you can also use setStretchFactor so you can make have them distributed in a more suitable fashion.
-
Hi,
Isn't it the minimal size of the widget ?
That said, you can also use setStretchFactor so you can make have them distributed in a more suitable fashion.
@SGaist said in Finding the "snap point" for a splitter ?:
Hi,
Isn't it the minimal size of the widget ?
That said, you can also use setStretchFactor so you can make have them distributed in a more suitable fashion.
The minimum size is (width) is 0. So this can't be right.
strech factor seems super obscure, doubt that will do anything other than make the situation worse.
-
@SGaist said in Finding the "snap point" for a splitter ?:
Hi,
Isn't it the minimal size of the widget ?
That said, you can also use setStretchFactor so you can make have them distributed in a more suitable fashion.
The minimum size is (width) is 0. So this can't be right.
strech factor seems super obscure, doubt that will do anything other than make the situation worse.
-
@SGaist said in Finding the "snap point" for a splitter ?:
@SamiV123 yes it can be. You can also set the minimum size to something suitable for you.
Well yes the minimum size of a widget can be 0 but in this case it doesn't make sense since the when the splitter is at the snap point the size (width) of the widget is non-zero. So the snap point can't be at a point where the widget's minimum size is zero.. Makes sense?
Secondarily, using fixed minimum size rarely (if ever) works correctly across platforms and style engines. Using X units of width and it looks ok with Fusion or whatever? Yeah, great then go and change the style engine to kvAntum or Adwaita and see how things are all kaput because the layout and widget sizes are completely different.
-
Use the following function:
pcoSplitter->setSizes({ iLeftViewWidth , iMiddleViewWidth, iRightViewWidth });and to your question:
pcoSplitter->setSizes({ 0 , iMiddleViewWidth, 0 }); -
Use the following function:
pcoSplitter->setSizes({ iLeftViewWidth , iMiddleViewWidth, iRightViewWidth });and to your question:
pcoSplitter->setSizes({ 0 , iMiddleViewWidth, 0 });And what are the sizes? Yeah.. that's basically the meat of the question.
-
Each splitter part is connected to a widget (QLabel, QListWidget, QScrollArea ....)
pcoSplitter->addWidget(pcoLeftWidget);
int iLeftViewWidth = pcoLeftWidget->width();pcoSplitter->addWidget(pcoMiddleWidget);
int iMiddleViewWidth = pcoMiddleWidget->width();pcoSplitter->addWidget(pcoRightWidget);
int iRightViewWidth = pcoRightWidget->widht(); -
Each splitter part is connected to a widget (QLabel, QListWidget, QScrollArea ....)
pcoSplitter->addWidget(pcoLeftWidget);
int iLeftViewWidth = pcoLeftWidget->width();pcoSplitter->addWidget(pcoMiddleWidget);
int iMiddleViewWidth = pcoMiddleWidget->width();pcoSplitter->addWidget(pcoRightWidget);
int iRightViewWidth = pcoRightWidget->widht();@Zbigniew-Sch said in Finding the "snap point" for a splitter ?:
Each splitter part is connected to a widget (QLabel, QListWidget, QScrollArea ....)
pcoSplitter->addWidget(pcoLeftWidget);
int iLeftViewWidth = pcoLeftWidget->width();pcoSplitter->addWidget(pcoMiddleWidget);
int iMiddleViewWidth = pcoMiddleWidget->width();pcoSplitter->addWidget(pcoRightWidget);
int iRightViewWidth = pcoRightWidget->widht();If you think about this for a second you should realize this is completely illogical.
Let's say the parent widget that contains the splitter is 1000 units wide and has a splitter that has left pane 250, center 500 and right pane 250 units in width.
What would be the result of setting the splitter "sizes" (QSplitter::setSizes) to 0, 500, 0?
The space to be allotted between the three widgets is 1000. So the sizes would logically need to sum up to 1000. 0 + 500 + 0 sums to 500. Makes no sense.
-
I think you didn't understand the logic. You first have to determine the three widths of the widgets (when closing splitter).
If you reduce the left and right widgets to 0 (via GUI splitter widgets changes), the middle widget will give the total width of splitter (1000), and the next time you start the program you can use splitter values.
If you want to set the wide splitter widgets arbitrarily when initializing, then the following logic applies, example:splitter->setSizes({20, 200, 300}) - total width of the splitter corresponds to 520 units - the left widget: (20/520) * spliter->width() - wide - the middle widget : (200/520) * spliter->width() - wide - the right widget: (300/520) * spliter->width() - wide
and if I set splitter->setSizes({0, 500, 0}) it doesn't matter what I pass 200, 500 or 1000, the left and right
widgets have a width of 0 and the middle widget has the width of the entire splitter.I hope the logic is understandable now
-
According to my experience, the "snap point" is indeed the minimum size, but not the value
minimumSize()
returned (unless you have set it), butminimumSizeHint()
if you have layout set on the widget.
Are you suresetStretchFactor()
is not working? I think it should if you set the center widget's strech to1
and leave the other two untouched (0). (Or you can just set the center widget'sHorizontal Stretch
to1
if you are using designer.) -
Use the QSplitterHandle class.
Integrate the object into the splitter and use "QSplitterHandle::mousePressEvent(QMouseEvent*
eventPress)" to realize the “snap point”.- Create your own class (QYourSplitterHandle) derived from "QSplitterHandle".
- Implement:
void QYourSplitterHandle::mousePressEvent(QMouseEvent* eventPress) - Create spliiter handle in your splitter class (virtual):
virtual QSplitterHandle *createHandle();
Example:
QYourSplitterHandle* QYourSplitter::createHandle()
{
return new QYourSplitterHandle(Qt::Orientation::Horizontal, this);
}Calculate the widths of splitter widgets attached to the current splitter and for the "snap-point"
do not call function from base class:
QSplitterHandle::mousePressEvent(eventPress);Determine splitter handle (using splitter object):
QYourSplitterHandle* pcoSplitterHandle =
qobject_cast<QYourSplitterHandle*>(pcoSplitter->handle(0)); -
I forgot, sorry, you have to do the same with the following function:
void QYourSplitterHandle::mouseMoveEvent(QMouseEvent* event) -
According to my experience, the "snap point" is indeed the minimum size, but not the value
minimumSize()
returned (unless you have set it), butminimumSizeHint()
if you have layout set on the widget.
Are you suresetStretchFactor()
is not working? I think it should if you set the center widget's strech to1
and leave the other two untouched (0). (Or you can just set the center widget'sHorizontal Stretch
to1
if you are using designer.) -