Visibility and Position
-
Hello,
I have three QPushButtons next to each other in a QHBoxLayout. The second button in the center is sometimes visible and sometimes now, but no matter which of those is currently true I want to keep the position of the other two buttons the same so they don't "hop around". How can I achieve this? -
A QSpacerItem could help here. You have to set a size for your buttons and the QSpacerItem fills up the remaining space.
http://doc.qt.nokia.com/4.7-snapshot/qboxlayout.html#insertStretch -
Could you maybe provide a small example code? I am having trouble especially with finding the correct size of the space.
-
You could also set a minimum size for the buttons. The middle button will not disappear anymore. You might want to check out how the window handles this, because you get horizontal scroll bars automatically otherwise. You are also able to set maximum sizes and as bomb3rman says use the spacers to keep the buttons in the correct position on the screen.
-
I don't see how setting the minimum size would help me.
And as I already answered to Bomb3rman, I need example code for the QSpacerItem because I have trouble finding the correct size. QPushButton::size() does not return the right value.
-
[quote author="Wurstinator" date="1344250755"]Could you maybe provide a small example code? I am having trouble especially with finding the correct size of the space.[/quote]
First, use "setFixedSize":http://qt-project.org/doc/qt-5.0/qwidget.html#setFixedSize-2 on all your buttons using width and height as required then add them to a "Horizontal Box Layout":http://qt-project.org/doc/qt-4.8/QHBoxLayout.html along with a QSpacer via "addStretch":http://qt-project.org/doc/qt-4.8/qboxlayout.html#addStretch or "insertStretch":http://qt-project.org/doc/qt-4.8/qboxlayout.html#insertStretch
-
[quote author="Wurstinator" date="1344256193"] QPushButton::size() does not return the right value.[/quote]
use "sizeHint":http://qt-project.org/doc/qt-5.0/qwidget.html#sizeHint-prop instead
-
setFixedSize cannot be used because the Window can be resized and therefore the buttons need to adapt their size to it.
sizeHint doesn't work correctly either. I am trying to get the size exactly like it is displayed in the window.
-
You can just omit the setFixedSize then from my previous post and do the rest... But the problem with that is that if the window is too small, the button will dissappear as they get no space to stay on screen... To fix this, you should call "setMinimumSize":http://qt-project.org/doc/qt-4.8/qwidget.html#minimumSize-prop on the MainWindow such that it can accommodate the buttons properly... Then the user cannot resize beyond a certain point and so the buttons stay on screen and the QLayout takes care of their proper positioning and size
Also, you should put the stretches between the buttons, so that when the middle button goes invisible, the stretch covers it instead of the other button jumping back into its place(This is what you originally wanted, right?)
-
Yes, that is what I am trying to accomplish.
But I never worked with stretches, that's why I asked for example code. If I simply use insertStretch the two remaining buttons get reduced to their MinimumSize.edit:
Well, it seems that I found a solution but I feel like it is inconvenient.
First, I have to use setStretch(1) on the three buttons when I create them. This is probably the major thing I have been missing before.
The reason why I don't like my current code is because I have to use setSpacing whenever the visibility changes.
If the button is visible, the Layout creates a space of 5 between each of the three buttons which gives a total space of 10.
If it is invisible, the Layout only creates a space of 5 between the first and the third button. So, to make it look the same, I have to manually duplicate the spacing attribute.