您的位置:首页 > 编程语言 > Qt开发

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

2017-12-23 18:13 519 查看
一般界面设计都会有主布局,主布局一般都采用垂直布局(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:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息