Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Horizontal Spacer expanding incorrectly
I want to achieve specific behavior:
- A button with the text expands depending on its text
- Below this button locates another button with fixed size (max 20 pxls)
- First button does not expands more than it needs, the rest of content goes to QLineEdit on the right
- Second button should be located on the right side, so it has to be somehow shifted (Spacer e.g.)
So I assume:
- First button horizontal policy must be 'Maximum' so its width stays constant depending on its content
- Second button should be 'fixed' horizontally to indicate that it has a constant size at a compile time
- Slider values should be 0x0 so it goes lower as much as it needs
- QLineEdit should be 'Expanding' so it expands
I added a change name to 'a' on click to the first button for demo
The first button and QLineEdit get along just fine (first line on pic)
The problem arises when we need to shift second button. Within designer it behaves as I expect (second line on pic).
But when compiled a empty space appears on the left of fixed-size button
Your spacer must be 'Minimum' - then all works fine for me
On last picture it is 'Minimum'. You might have some default size set. If you set it 0x0 I think you will get the same situation.
Button 'G' - Fixed
Spacer - Minimum, 0x0
'AAAAAAAAAAAAAAA' - Maximum
LineEdit - Expanding
Use a QGridLayout instead a QHBox & QVBoxLayout
No, put the spacer and the two buttons in a grid layout. Otherwise the spacer + button and the button don't have any relation to each other and therefore the spacer expands larger than the button.
Worked perfectly. Many thanks.
JoeCFD last edited by JoeCFD
You can also try to align G label to left when it is added to the layout.
layout->addWidget( g_label, 0 , Qt::AlignLeft );
In this case, spacer may not be needed at all.