Search code examples
c++qt5qtwidgets

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


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!


Solution

  • 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.