《Qt Creator笔记》Qt 窗口布局

文章目录

  • QVBoxLayout()
  • QHBoxLayout
  • QHBoxLayout---addStrut()
  • QHBoxLayout---setSpacing()
  • QGridLayout()
  • setColumnStretch()和setRowStretch()
  • 网格合并
  • 网格嵌套

QVBoxLayout()

纵向布局
	QWidget w;
	QVBoxLayout V_Layout;
	QLineEdit Edit1;
	QLineEdit Edit2;
	
	V_Layout.addWidget(&Edit1);
	V_Layout.addWidget(&Edit2);
	w.setLayout(&V_Layout);
	
	w.show();

《Qt Creator笔记》Qt 窗口布局_第1张图片

QHBoxLayout

横向布局

QWidget w;
QHBoxLayout H_Layout;
QLineEdit Edit1;
QLineEdit Edit2;
H_Layout.addWidget(&Edit1);
H_Layout.addWidget(&Edit2);
w.setLayout(&H_Layout);
w.show();

《Qt Creator笔记》Qt 窗口布局_第2张图片

QHBoxLayout—addStrut()


QWidget w;
QHBoxLayout H_Layout;
QLineEdit Edit1;
QLineEdit Edit2;
H_Layout.addWidget(&Edit1);
H_Layout.addWidget(&Edit2);
添加弹簧
H_Layout.addStretch(1);  

w.setLayout(&H_Layout);

w.show();

《Qt Creator笔记》Qt 窗口布局_第3张图片

QHBoxLayout—setSpacing()

QWidget w;
QHBoxLayout H_Layout;
QLineEdit Edit1;
QLineEdit Edit2;

H_Layout.addWidget(&Edit1);
设置间距
H_Layout.setSpacing(40);
H_Layout.addWidget(&Edit2);
w.setLayout(&H_Layout);

w.show();

设置间距

QGridLayout()

网格布局

QWidget w;
QHBoxLayout H_Layout;
QPushButton But1("1");
QPushButton But2("2");
QPushButton But3("3");
QPushButton But4("4");

QGridLayout GridLayout;

GridLayout.addWidget(&But1,0,0);//第0行第0列
GridLayout.addWidget(&But2,0,1);
GridLayout.addWidget(&But3,1,0);
GridLayout.addWidget(&But4,1,1);
w.setLayout(&GridLayout);
w.show();

《Qt Creator笔记》Qt 窗口布局_第4张图片

setColumnStretch()和setRowStretch()

 QWidget w;
QHBoxLayout H_Layout;
QPushButton But1("1");
QPushButton But2("2");
QPushButton But3("3");
QPushButton But4("4");

QGridLayout GridLayout;

GridLayout.addWidget(&But1,1,1);
GridLayout.addWidget(&But2,1,2);
GridLayout.addWidget(&But3,2,1);
GridLayout.addWidget(&But4,2,2);

GridLayout.setColumnStretch(0,1);//“0”代表列数,“1”代表权重
GridLayout.setColumnStretch(3,1);
GridLayout.setRowStretch(0,1);//“0”代表行数,“1”代表权重
GridLayout.setRowStretch(3,1);

w.setLayout(&GridLayout);
w.show();

《Qt Creator笔记》Qt 窗口布局_第5张图片

网格合并


QWidget w;
QHBoxLayout H_Layout;
QPushButton But1("1");
QPushButton But2("2");
QPushButton But3("3");
QPushButton But4("4");

QGridLayout GridLayout;

GridLayout.addWidget(&But1,1,1);
GridLayout.addWidget(&But2,1,2);
GridLayout.addWidget(&But3,2,1);
GridLayout.addWidget(&But4,2,2);

GridLayout.setColumnStretch(0,1);
GridLayout.setColumnStretch(3,1);
GridLayout.setRowStretch(0,1);
GridLayout.setRowStretch(4,1);
GridLayout.addWidget(new QPushButton("Merger"),3,1,1,2);//从第3行开始独占1行2列
w.setLayout(&GridLayout);

w.show();

《Qt Creator笔记》Qt 窗口布局_第6张图片

网格嵌套

QWidget w;
QHBoxLayout H_Layout;

QGridLayout GridLayout;

GridLayout.addWidget(new QLineEdit,1,2);
GridLayout.addWidget(new QLineEdit,2,2);
GridLayout.addWidget(new QLabel("User"),1,1);
GridLayout.addWidget(new QLabel("PassWord"),2,1);

QHBoxLayout* H_Box(new QHBoxLayout);
GridLayout.addLayout(H_Box,3,2);
H_Box->addStretch(1);
H_Box->addWidget(new QPushButton("Login"));

GridLayout.setColumnStretch(0,1);
GridLayout.setColumnStretch(3,1);
GridLayout.setRowStretch(0,1);
GridLayout.setRowStretch(4,1);

《Qt Creator笔记》Qt 窗口布局_第7张图片

你可能感兴趣的:(Qt,Creator笔记)