Qt (QGis) 中动态布局例子
QHBoxLayout* classLayout = new QHBoxLayout;
QLabel *label1 = new QLabel(tr("选择分类栅格图层:"));
mInputClassCombo = new QComboBox(this);
classLayout->addWidget(label1);
classLayout->addWidget(mInputClassCombo);
QHBoxLayout* outputLayout = new QHBoxLayout;
QLabel *label2 = new QLabel(tr("输出文件:"));
mOutputBuildingLineEdit = new QLineEdit(this);
mOutputBrowseButton = new QPushButton(tr("..."), this);
mOutputBrowseButton->setFixedWidth(15);
outputLayout->addWidget(label2);
outputLayout->addWidget(mOutputBuildingLineEdit);
outputLayout->addWidget(mOutputBrowseButton);
QGridLayout *pixelLayout = new QGridLayout;
pixelLayout->setAlignment(Qt::AlignHCenter);
QLabel *labelR = new QLabel(tr("R"));
QLabel *labelG = new QLabel(tr("G"));
QLabel *labelB = new QLabel(tr("B"));
mPixel_R_LineEdit = new QLineEdit(this);
mPixel_G_LineEdit = new QLineEdit(this);
mPixel_B_LineEdit = new QLineEdit(this);
pixelLayout->addWidget(labelR, 0, 0);
pixelLayout->addWidget(mPixel_R_LineEdit, 1, 0);
pixelLayout->addWidget(labelG, 0, 1);
pixelLayout->addWidget(mPixel_G_LineEdit, 1, 1);
pixelLayout->addWidget(labelB, 0, 2);
pixelLayout->addWidget(mPixel_B_LineEdit, 1, 2);
QHBoxLayout* operationLayout = new QHBoxLayout;
mAddButton = new QPushButton(tr("添加"), this);
mRemoveButton = new QPushButton(tr("删除"), this);
operationLayout->addWidget(mAddButton);
operationLayout->addWidget(mRemoveButton);
setupTable();
QHBoxLayout* executeLayout = new QHBoxLayout;
mOkButton = new QPushButton(tr("确定"), this);
mCancelButton = new QPushButton(tr("取消"), this);
executeLayout->addWidget(mOkButton);
executeLayout->addWidget(mCancelButton);
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addLayout(classLayout);
mainLayout->addLayout(outputLayout);
mainLayout->addLayout(pixelLayout);
mainLayout->addLayout(operationLayout);
mainLayout->addWidget(mTableWidget);
mainLayout->addLayout(executeLayout);
this->setLayout(mainLayout);
QObject::connect(mInputClassCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(on_mInputClassCombo_currentIndexChanged()));
connect(mOutputBrowseButton, SIGNAL(clicked()), this, SLOT(on_mOutputBrowseButton_clicked()), Qt::UniqueConnection);
connect(mAddButton, SIGNAL(clicked()), this, SLOT(on_mAddButton_clicked()), Qt::UniqueConnection);
connect(mRemoveButton, SIGNAL(clicked()), this, SLOT(on_mRemoveButton_clicked()), Qt::UniqueConnection);
connect(mCancelButton, SIGNAL(clicked()), this, SLOT(on_mCancelButton_clicked()), Qt::UniqueConnection);
connect(mOkButton, SIGNAL(clicked()), this, SLOT(on_mOkButton_clicked()), Qt::UniqueConnection);
值得注意的是“QVBoxLayout *mainLayout = new QVBoxLayout(this);” 为主布局控件,其它的布局控件和Widget控件放置在该控件中,初始化的时候不能用"this",如
QHBoxLayout* classLayout = new QHBoxLayout;