6

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!

rsht
  • 1,522
  • 12
  • 27
  • How about simply clicking this together in QtDesigner? Should be rather straightforward. And yes, you could use a QGridLayout in code and simply put Appartment and City into column 1 while the single lines have columnSpan set to -1. – Robert Jul 03 '15 at 22:26

4 Answers4

9

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.

rsht
  • 1,522
  • 12
  • 27
1

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.

  • Thanks for reply! Unfortunately, I really can't understand what do you mean. Could you describe your solution in greater details, please? – rsht Jul 03 '15 at 19:49
  • It's better to use nested layout management (native Qt solution). It means that you create a separate `QFormLayout` for you right column and add it using `addRow(QLayout * layout)` to your parent layout. – dazewell Jul 04 '15 at 08:45
  • dazewell it's way faster to use QDesigner to do the ui and it's what you do with my solution. @Roman use QtCreator. Create a new class -> Qt -> Qt form -> Widget -> in you widget.ui create a QLabel and QLineEdit, right click -> vertical layout. return in your main class where you want to add you QLabel-QLineEdit create an objet of you custom widget class then make an layout->addWidget(yourWidget); – Gabrielle de Grimouard Jul 04 '15 at 15:47
0

If you want to add a label without the QLineEdit to appear, you can define a QlineEdit and hide it: (the code is in python, just to show the principle)

self._dummy = QLineEdit(self)
self._dummy.hide()
layout = QFormLayout(self)
layout.addRow("text without an edit field", self._dummy)
0

Here is a method I just created for my solution, this is a pseudo code version that might help someone.

    def create_gb_f(self):
        gb = QGroupBox("F") 

        ly_horiz = QHBoxLayout()

        layout = QFormLayout()
        layout.addRow( label, edit)
        ly_horiz.addLayout(layout)

        layout = QFormLayout()
        layout.addRow( label, edit)
        ly_horiz.addLayout(layout)

        ly_horiz.addWidget( button)
        gb.setLayout(ly_horiz)
        return gb

QuentinJS
  • 162
  • 1
  • 9