Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add two label-field pair as one row to QFormLayout?

Tags:

c++

qt5

qtwidgets

So I have QFormLayout that manages my QLabel-QLineEdit pairs just fine.
Problem is, that I need to achive something like this: enter image description here

Horizontal border/tittle isn't a problem, but "Street"-"Apartment"/"Post code"-"City" pairs are.

So my question is: how to add two pairs of QLabel-QLineEdit as one row to QFromLayout?

If it's not possible with QFormLayout, do you have any suggestion about achivining the same with other layout (QGridLayout, I guess)?

Keep in mind, that labels can have different size proportions after translated to other languages.

Thanks in advance!

like image 500
rsht Avatar asked Jul 03 '15 11:07

rsht


2 Answers

Thanks for all the response!

I've ended up adding QLabel as label and QHBoxLayout with QLineEdit, QLabel and QLineEdit as field to QFormLayout. Something like:

QLabel firstLabel, secondLabel;
QLineEdit fisrtEdit, secondEdit;

QHBoxLayout hBoxLayout;
hBoxLayout.addWidget(firstEdit);
hBoxLayout.addWidget(secondLabel);
hBoxLayout.addWidget(secondEdit);

QWidget container;
container.setLayout(hBoxLayout);

myFormLayout.addRow(firstLabel, container);

do the trick!

Also, if you're planing to add more than one row like this, I'll need to set all secondLabels to one fixed width. I did this by iterating over all secondLabels twice: first time for finding maximux width and second for setting this width to all of them.

A bit hacky, but I couldn't find a better way so far. Solution with QGridLayout would be even more complicated, on my opinion.

like image 108
rsht Avatar answered Nov 03 '22 08:11

rsht


I think you should create a QWidget with get a Vertical layout with the QLabel and the QLineEdit then add the label in the QFormLayout. I don't get the time to show you an example but think about to create an ui with a QLabel-QLineEdit in a QVBoxLayout. By creating an ui you can add any widget in with the same form easily.

So you mainWindow. you main layout -> create your widget -> add vertical layout -> add your QLabel and QLineEdit to yout widget layout -> add your widget to your main layout.

I think you should show a QtCreator-QtDesigner tutorial. It will take you some time but you will get really faster after.

like image 29
Gabriel de Grimouard Avatar answered Nov 03 '22 09:11

Gabriel de Grimouard