Hello. I have a simple code in Qt, as below:
#include "mainwindow.h"
#include <QWidget>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QGridLayout>
#include <QVBoxLayout>
class classA;
class classB;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
classA * objA = new classA(this);
classB * objB = new classB(this);
QVBoxLayout * mainLayout = new QVBoxLayout(this);
setLayout(mainLayout);
mainLayout->addWidget(objA);
mainLayout->addWidget(objB);
}
MainWindow::~MainWindow(){}
classA::classA(QWidget *parent) : QWidget(parent)
{
QGroupBox *grupa = new QGroupBox(tr("classA"),this);
QLabel *labelA1 = new QLabel(tr("Label A1"));
QLabel *labelA2 = new QLabel(tr("Label A2"));
QLineEdit *LineEditA1 = new QLineEdit("LineEditA1");
QLineEdit *LineEditA2 = new QLineEdit("LineEditA2");
QGridLayout *lay = new QGridLayout(grupa);
lay->addWidget(labelA1, 0, 0, Qt::AlignLeft);
lay->addWidget(LineEditA1, 0, 1, Qt::AlignLeft);
lay->addWidget(labelA2, 1, 0, Qt::AlignLeft);
lay->addWidget(LineEditA2, 1, 1, Qt::AlignLeft);
grupa->setLayout(lay);
}
classA::~classA(){}
classB::classB(QWidget *parent) : QWidget(parent)
{
QGroupBox *grupa = new QGroupBox(tr("classB"),this);
QLabel *labelB1 = new QLabel(tr("Label B1"));
QLabel *labelB2 = new QLabel(tr("Label B2"));
QLineEdit *LineEditB1 = new QLineEdit("LineEditB1");
QLineEdit *LineEditB2 = new QLineEdit("LineEditB2");
QGridLayout *lay = new QGridLayout(grupa);
lay->addWidget(labelB1, 0, 0, Qt::AlignLeft);
lay->addWidget(LineEditB1, 0, 1, Qt::AlignLeft);
lay->addWidget(labelB2, 1, 0, Qt::AlignLeft);
lay->addWidget(LineEditB2, 1, 1, Qt::AlignLeft);
grupa->setLayout(lay);
}
classB::~classB(){}
As a result, I should see a window with a nicely spaced elements. Unfortunately, I have something like this:
What am I doing wrong that these items will not spaced properly?