Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QFormLayout with QVBoxLayout



  • Hi everyone,

    I want to make a FormLayout, where the label is a QLabel and the form right is QVBoxLayout. Unfortunately, the alignment doesn't work the way I want it.

    QFormLayout* mLayout = new QFormLayout;
    
        QLabel* label1 = new QLabel(QStringLiteral("Name"));
        QLabel* label2 = new QLabel(QStringLiteral("Name"));
        QLabel* label3 = new QLabel(QStringLiteral("Name"));
    
        QVBoxLayout* vbox = new QVBoxLayout;
        vbox->addWidget(label2);
        vbox->addWidget(label3);
    
        mLayout->addRow(label1, vbox);
    
        this->setLayout(mLayout);
    

    0_1567504170748_11eddb48-e84d-43bb-86d0-9efb42a914de-image.png

    I would like to have the left label on the same height as the right upper label in the QVBoxLayout.

    Turning on borders:

    0_1567504259014_93b94587-903b-4dfe-a0bf-b9ddfa27876d-image.png

    the label left is stretched for some reason and I can't figure out why.

    Any help would be appreciated!



  • maybe you should set fixed size.

    void QWidget::setFixedSize(int w, int h)
    


  • Indeed it works:

    label1->setFixedHeight(label1->sizeHint().height());
    

    But it seems more like a hacky solution, what is the reason that the sizeHint is not respected in the first place?



  • @leon_2001

    QFormLayout* mLayout = new QFormLayout;

    QLabel* label1 = new QLabel(QStringLiteral("Name"));
    QLabel* label2 = new QLabel(QStringLiteral("Name"));
    QLabel* label3 = new QLabel(QStringLiteral("Name"));
    
    mLayout->addRow(label1, label2);
    mLayout->addRow(new QLabel(""),  label3);
    
    this->setLayout(mLayout);


  • Hi,

    thanks for the suggestion, unfortunately this is not what I want to achieve.
    In your code, there is spacing between label2 and label3. But I want the spacing only between the actual rows. Of course I could work with trickst like an extra Spacing element etc. but that's kinda hacky again.

    Having a QFormLayout combinded with QVBoxLayout is exactly what I need and want (and makes the most sense). I would like to know why it's not working without the code from above, up to now it seems like a bug to me.


  • Qt Champions 2019

    @leon_2001 said in QFormLayout with QVBoxLayout:

    up to now it seems like a bug to me.

    Why? The widget just fills out it's available height and therefore the left widget displays it's text centered. I would take a look at QLabel::alignment



  • @christian-ehrlicher

    Which widget? The label on the left side doesn't seeem to fill out the complete avaiable height? Otherwise it would has the same height as the labels on the right together, wouldn't it?

    Why has the left label exactly this height (which by the way stays the same, if you have more spacing in the VBox)


Log in to reply