1
votes

In QtDesigner I've created QFrame with horizontal layout (layoutSpacing is 5). There are 3 QLineEdit widgets in it, with fixed size and horizontal spacer with expanding size(so when frame size is changed only spacer is changed)

Lets say initial size of lineEdits was 100x30. I need to change size of lineEdit widgets and I do that from code like this:

self.__ui.lineEdit1.resize(40, 30)
self.__ui.lineEdit2.resize(140, 30)
self.__ui.lineEdit3.resize(80, 30)

Their size is changed but their X position is not so I get big space between first two (65), and lineEdit2 is below lineEdit3.

Should I update layout or Frame somehow? I've tried layout.setSpacing(5) before and after mentioned code but in that case size of lineEdit is not changed (it's initial 100 again)

EDIT here is screenshot (I've edited in paint :D) as Qurban requested: enter image description here

1
Can you post the screenshots before and after resize is called? - qurban
@qurban please take a look at EDIT in my question - Aleksandar
Use setFixedSize() instead of resize() everytime you want to resize the widgets, as ekhumoro suggested in his answer. This solution meets your requirements. - qurban

1 Answers

1
votes

Use the setFixedSize method:

    self.__ui.lineEdit1.setFixedSize(40, 30)
    self.__ui.lineEdit2.setFixedSize(140, 30)
    self.__ui.lineEdit3.setFixedSize(80, 30)

You might also want to consider using the layoutStretch property of the horizontal layout. This allows you to set the proportion of space allowed for each line-edit. So if you set it to "1, 4, 2", the second line-edit would get four-times as much space as the first, and the third one would get twice as much. The nice thing about this is that it scales automatically when the main window is resized.