Qt综合使用布局管理器(QVBoxLayout,QHBoxLayout,QGridLayout)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/RGBMarco/article/details/78881391

一般界面设计都会有主布局,主布局一般都采用垂直布局(QVBoxLayout),因为界面大都自上而下浏览观看,才符合人类习惯,
布局和布局之间我们都可以通过addSpacing(int)去调整,布局内容可通过setContentsMargins(int left,int top,int right,int bottom)调整

Example:


Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    QVBoxLayout* mainlayout = new QVBoxLayout();
    QGridLayout* layout = new QGridLayout();
    QLabel* l1 = new QLabel("Name(N)");
    QLineEdit* name = new QLineEdit();
    QLabel* l2 = new QLabel("Sex(S)");
    QComboBox* sex = new QComboBox();
    sex->addItem("Man");
    sex->addItem("Woman");
    sex->setCurrentIndex(0);
    QLabel* l3 = new QLabel("Age(A)");
    QSpinBox* age = new QSpinBox();
    age->setMinimum(0);
    age->setMaximum(10);
    QLabel* l4 = new QLabel("Email(E)");
    QLineEdit* email = new QLineEdit();
    layout->addWidget(l1,0,0,1,1);
    layout->addWidget(name,0,2,1,5);
    layout->addWidget(l2,1,0,1,1);
    layout->addWidget(sex,1,2,1,5);
    layout->addWidget(l3,2,0,1,1);
    layout->addWidget(age,2,2,1,5);
    layout->addWidget(l4,3,0,1,1);
    layout->addWidget(email,3,2,1,5);
    mainlayout->addLayout(layout);
    mainlayout->addSpacing(50);
    QHBoxLayout* hlayout = new QHBoxLayout();
    QFontComboBox* font = new QFontComboBox();
    QPushButton* submit = new QPushButton("Sumbit");
    hlayout->addWidget(font);
    hlayout->addWidget(submit);
    QVBoxLayout* vlayout = new QVBoxLayout();
    QLineEdit* le = new QLineEdit();
    vlayout->addLayout(hlayout);
    vlayout->addWidget(le);
    vlayout->addSpacing(10);
    vlayout->setContentsMargins(10,10,10,10);
    layout->setContentsMargins(10,10,10,10);
    mainlayout->addLayout(layout);
    mainlayout->addLayout(vlayout);
    setLayout(mainlayout);
    ui->setupUi(this);
}

Run:

这里写图片描述

展开阅读全文

没有更多推荐了,返回首页