文章目录
- 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();
QHBoxLayout
横向布局
QWidget w;
QHBoxLayout H_Layout;
QLineEdit Edit1;
QLineEdit Edit2;
H_Layout.addWidget(&Edit1);
H_Layout.addWidget(&Edit2);
w.setLayout(&H_Layout);
w.show();
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();
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);
GridLayout.addWidget(&But2,0,1);
GridLayout.addWidget(&But3,1,0);
GridLayout.addWidget(&But4,1,1);
w.setLayout(&GridLayout);
w.show();
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);
GridLayout.setColumnStretch(3,1);
GridLayout.setRowStretch(0,1);
GridLayout.setRowStretch(3,1);
w.setLayout(&GridLayout);
w.show();
网格合并
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);
w.setLayout(&GridLayout);
w.show();
网格嵌套
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);