简介
QGridLayout 占用来自其父布局或 parentWidget()获得的空间,将其划分为行列表格形式,在每个布局的表格里放置控件或者布局器就行。
新建QGridLayout之后,在桌面上就会出现一个网格状的布局,这个布局是看不见的,如下表格一样,那么如何确定这样的表格有几行几列呢?可以对其进行初始化。
常用方法
addWidget有两种不同的重载形式。
addWidget(控件名,行数,列数);
addWidget(控件名,行数,列数,控件长度占几行,控件长度占几列);
setRowStretch(行数,行宽度); //第一个参数是行数,其最大值表示这个布局有几行;第二个参数是该行的长度比例。
setColumnStretch(列数,列宽度); 第一个参数是行数,其最大值表示这个布局有几列;第二个参数是该列的长度比例
实例代码
实例1
void Widget::initGridLayout()
{
setMinimumSize(550, 300);
setMaximumSize(550, 300);
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);
//头像
QLabel* pImageLabel = new QLabel(this);
QPixmap pixmap(":/resources/user_image.png");
pImageLabel->setFixedSize(150, 150);
pImageLabel->setPixmap(pixmap);
pImageLabel->setScaledContents(true);
//用户名
QLineEdit* pUserNameLineEdit = new QLineEdit(this);
pUserNameLineEdit->setFixedSize(300, 50);
pUserNameLineEdit->setPlaceholderText("QQ号码/手机/邮箱");
QLineEdit* pPasswordLineEdit = new QLineEdit(this);
pPasswordLineEdit->setFixedSize(300, 50);
pPasswordLineEdit->setPlaceholderText("密码");
pPasswordLineEdit->setEchoMode(QLineEdit::Password);
QPushButton* pForgotButton = new QPushButton(this);
pForgotButton->setText("找回密码");
pForgotButton->setFixedWidth(80);
QCheckBox* pRememberCheckBox = new QCheckBox(this);
pRememberCheckBox->setText("记住密码");
QCheckBox* pAutoLoginCheckBox = new QCheckBox(this);
pAutoLoginCheckBox->setText("自动登录");
QPushButton* pLoginButton = new QPushButton(this);
pLoginButton->setFixedHeight(48);
pLoginButton->setText("登录");
QPushButton* pRegisterButton = new QPushButton(this);
pRegisterButton->setFixedHeight(48);
pRegisterButton->setText("注册账号");
QGridLayout* pGridLay = new QGridLayout(this);
pGridLay->addWidget(pImageLabel, 0,0,2,1);
pGridLay->addWidget(pUserNameLineEdit, 0,1,1,2);
pGridLay->addWidget(pPasswordLineEdit, 1,1,1,2);
pGridLay->addWidget(pForgotButton, 2,1,1,1);
pGridLay->addWidget(pRememberCheckBox, 2,2,1,1, Qt::AlignLeft | Qt::AlignVCenter);
pGridLay->addWidget(pAutoLoginCheckBox, 2,2,1,1, Qt::AlignRight | Qt::AlignVCenter);
pGridLay->addWidget(pLoginButton, 3,1,1,2);
pGridLay->addWidget(pRegisterButton, 4,1,1,2);
pGridLay->setHorizontalSpacing(20);
pGridLay->setVerticalSpacing(20);
pGridLay->setContentsMargins(30,30,30,30);
}
图片
实例2
void Widget::initGridLayout1()
{
QGridLayout* gLayout = new QGridLayout(this);
QPushButton* pButton1 = new QPushButton(FetchString("One"));
QPushButton* pButton2 = new QPushButton(FetchString("Two"));
QPushButton* pButton3 = new QPushButton(FetchString("Three"));
QPushButton* pButton4 = new QPushButton(FetchString("Four"));
QPushButton* pButton5 = new QPushButton(FetchString("Five"));
QPushButton* pButton6 = new QPushButton(FetchString("Six1"));
QPushButton* pButton7 = new QPushButton(FetchString("Seven"));
pButton1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pButton2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pButton3->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pButton4->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pButton5->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pButton6->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pButton7->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
gLayout->addWidget(pButton1, 0, 1);
gLayout->addWidget(pButton2, 1, 0, 2, 1);
gLayout->addWidget(pButton3, 1, 1);
gLayout->addWidget(pButton4, 1, 2);
gLayout->addWidget(pButton5, 2, 1);
gLayout->addWidget(pButton6, 3, 0, 1, 3);
gLayout->addWidget(pButton7, 4, 1);
gLayout->setColumnStretch(0, 1);
gLayout->setColumnStretch(1, 1);
gLayout->setColumnStretch(2, 2);
gLayout->setRowStretch(0, 1);
gLayout->setRowStretch(1, 2);
gLayout->setRowStretch(2, 2);
gLayout->setRowStretch(3, 2);
gLayout->setRowStretch(4, 2);
}
源代码