KcFnMi
KcFnMi

Reputation: 6161

Custom QWidget in QFormLayout, vertical alignment issue

I would like to use a custom made QWidget in one row of a QFormLayout. The code below presents a form layout in which the first row has a a QLineEdit and the second line has a custom made widget:

The problem is that the custom made widget is not vertically aligned. How to align it vertically?

project.pro:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = CustomLineEdit
TEMPLATE = app

SOURCES += main.cpp

main.cpp

#include <QApplication>

#include <QHBoxLayout>
#include <QFormLayout>
#include <QLineEdit>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;

    QFormLayout *formLayout = new QFormLayout(&w);

    QLineEdit *leUser = new QLineEdit;

    QWidget *widget = new QWidget;
    QHBoxLayout *hLayout = new QHBoxLayout(widget);
    hLayout->addWidget(leUser);
    hLayout->addWidget(new QPushButton);

    formLayout->addRow("Text:", new QLineEdit);
    formLayout->addRow("User:", widget);

    w.show();
    return a.exec();
}

Upvotes: 2

Views: 525

Answers (1)

Evgeny
Evgeny

Reputation: 4010

This is because of margins. Just add followed line:

QHBoxLayout *hLayout = new QHBoxLayout(widget);
hLayout->setMargin(0); //this line

Upvotes: 5

Related Questions