Qt的布局类继承关系
在此仅简单介绍3种不常用的layout
QStackedLayout, QFormLayout , QGridLayout
实现代码
myLayouts.h
#pragma once
#include <QtWidgets/QWidget>
#include "ui_myLayouts.h"
class myLayouts : public QWidget
{
Q_OBJECT
public:
myLayouts(QWidget *parent = Q_NULLPTR);
private:
Ui::myLayoutsClass ui;
void initMyStackedLayout();
void initMyFormLayout();
void initMyGridLayout();
};
myLayouts.cpp
#include "myLayouts.h"
#include <QStackedLayout>
#include <QFormLayout>
#include <QBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <qlineedit.h>
#include <qspinbox.h>
myLayouts::myLayouts(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
initMyStackedLayout();
initMyFormLayout();
initMyGridLayout();
}
void myLayouts::initMyStackedLayout()
{
QWidget* wgt1, * wgt2, * wgt3;
wgt1 = new QWidget();
wgt2 = new QWidget();
wgt3 = new QWidget();
wgt1->setStyleSheet("background:rgb(128,0,0)");
wgt2->setStyleSheet("background:rgb(0,128,0)");
wgt3->setStyleSheet("background:rgb(0,0,128)");
QLabel* lb1, * lb2, * lb3;
lb1 = new QLabel("Label1", wgt1);
lb2 = new QLabel("Label2", wgt2);
lb3 = new QLabel("Label3", wgt3);
lb1->setStyleSheet("background:rgb(220,220,220)");
lb2->setStyleSheet("background:rgb(170,170,170)");
lb3->setStyleSheet("background:rgb(100,100,100)");
QStackedLayout* slay = new QStackedLayout(ui.widget);
slay->addWidget(wgt1);
slay->addWidget(wgt2);
slay->addWidget(wgt3);
slay->setStackingMode(QStackedLayout::StackAll);
QHBoxLayout* hlay = new QHBoxLayout;
hlay->addLayout(slay);
ui.widget->setLayout(hlay);
ui.comboBox->addItem(tr("page1"));
ui.comboBox->addItem(tr("page2"));
ui.comboBox->addItem(tr("page3"));
connect(ui.comboBox, SIGNAL(activated(int)), slay, SLOT(setCurrentIndex(int)));
}
void myLayouts::initMyFormLayout()
{
QLineEdit* nameLineEdit1, * emailLineEdit1,*addrLineEdit1;
nameLineEdit1 = new QLineEdit;
emailLineEdit1 = new QLineEdit;
addrLineEdit1 = new QLineEdit;
QSpinBox* ageSpinBox1 = new QSpinBox;
QFormLayout* formLayout = new QFormLayout;
formLayout->addRow(tr("&Name1:"), nameLineEdit1);
formLayout->addRow(tr("&Email1:"), emailLineEdit1);
formLayout->addRow(tr("&Age1:"), ageSpinBox1);
formLayout->addRow(tr("&Addr1:"), addrLineEdit1);
ui.widget2->setLayout(formLayout);
}
void myLayouts::initMyGridLayout()
{
QLineEdit* nameLineEdit, * emailLineEdit, * addrLineEdit;
nameLineEdit = new QLineEdit;
emailLineEdit = new QLineEdit;
addrLineEdit = new QLineEdit;
QSpinBox* ageSpinBox = new QSpinBox;
QLabel* nameLabel, * emailLabel, * ageLabel, * addrLabel;
nameLabel = new QLabel(tr("&Name:"));
nameLabel->setBuddy(nameLineEdit);
emailLabel = new QLabel(tr("&Email:"));
emailLabel->setBuddy(emailLineEdit);
ageLabel = new QLabel(tr("&Age:"));
ageLabel->setBuddy(ageSpinBox);
addrLabel = new QLabel(tr("&Addr:"));
addrLabel->setBuddy(addrLineEdit);
QGridLayout* gridLayout = new QGridLayout;
gridLayout->addWidget(nameLabel, 0, 0);
gridLayout->addWidget(nameLineEdit, 0, 1);
gridLayout->addWidget(emailLabel, 1, 0);
gridLayout->addWidget(emailLineEdit, 1, 1);
gridLayout->addWidget(ageLabel, 2, 0);
gridLayout->addWidget(ageSpinBox, 2, 1);
gridLayout->addWidget(addrLabel, 3, 0);
gridLayout->addWidget(addrLineEdit, 3, 1);
ui.widget3->setLayout(gridLayout);
}