Qt的問題我在Qt的一個簡單的碼,如下所示:與GUI應用程序
#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(){}
結果,我應該看到一個窗口,其中包含一個很好的間隔元件。不幸的是,我有這樣的事情:
我在做什麼錯了,這些項目將無法正常間隔多長時間?
我添加了這個代碼,它工作正常,但我有警告:QLayout:試圖添加QLayout「」到MainWindow「」,它已經有一個佈局 – niweja 2010-10-15 22:25:15
對不起,是的,正如Shynthriir提到的,在佈局構造函數將佈局分配給MainWindow。將構造函數留空,以便可以使用q-> setLayout(mainLayout)將佈局設置爲q。 – 2010-10-15 23:50:44