qt布局管理系统-------QBoxLayout(基本布局管理器)

在这里插入图片描述
1、水平布局:QHBoxLayout
垂直布局:QVBoxLayout
2、实现登录注册页面

//设置窗口大小
    this->setFixedSize(400,300);
    this->titleLabel = new QLabel;
    this->titleLabel->setText(QStringLiteral("登录"));
    //对齐方式setAlignment
    this->titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    //布局策略  setSizePolicy设置大小策略(水平、垂直)
    this->titleLabel->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Maximum);
    //this->titleLabel->setGeometry(180,40,40,40);手动布局,设置控件大小
    this->userLael = new QLabel;
    this->userLael->setText(QStringLiteral("用户"));
    //布局策略
    this->userLael->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
    //this->userLael->setGeometry(60,80,30,20);手动布局,设置控件大小
    this->passwdLabel = new QLabel;
    this->passwdLabel->setText(QStringLiteral("密码"));
    //布局策略
    this->passwdLabel->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
    this->userEdit = new QLineEdit;
    this->passwdEdit = new QLineEdit;
    this->userEdit->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
    this->passwdEdit->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
    //设置模式,一般是用小黑点覆盖你所输入的字符
    this->passwdEdit->setEchoMode(QLineEdit::Password);
    this->autoLog = new QCheckBox;
    this->savePasswd = new QCheckBox;
    
    this->logButton = new QPushButton;
    logButton->setText(QStringLiteral("登录"));
    this->regButton = new QPushButton;
    regButton->setText(QStringLiteral("注册"));
    this->logButton->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);
    this->regButton->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum);


    autoLog->setText(QStringLiteral("自动登录"));
    //布局策略
    this->autoLog->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Maximum);
    savePasswd->setText(QStringLiteral("记住密码"));
    //布局策略
    this->savePasswd->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Maximum);
    //布局管理器:水平布局
    this->layout1 = new QHBoxLayout;
    this->layout2 = new QHBoxLayout;
    this->layout3 = new QHBoxLayout;
    this->layout4 = new QHBoxLayout;
    //addwidget()方法用于向布局中添加控件
    this->layout1->addWidget(this->userLael);
    this->layout1->addWidget(this->userEdit);
    this->layout2->addWidget(this->passwdLabel);
    this->layout2->addWidget(this->passwdEdit);
    this->layout3->addStretch();//增加伸缩量
    this->layout3->addWidget(this->autoLog);
    this->layout3->addStretch();//增加伸缩量
    this->layout3->addWidget(this->savePasswd);
    this->layout3->addStretch();//增加伸缩量
    this->layout4->addWidget(this->logButton);
    this->layout4->addWidget(this->regButton);
    //布局管理器:垂直布局
    this->vlayout  = new QVBoxLayout;
    //addLayout()方法用于向布局中添加子布局
    //addLayout用于添加一个已经包含另外一个QLayout布局的盒子到行(或列)中,同时设置布局的拉伸因子
    this->vlayout->addWidget(this->titleLabel);//1
    this->vlayout->addLayout(this->layout1);//2
    this->vlayout->addLayout(this->layout2);//2
    this->vlayout->addLayout(this->layout3);//1
    this->vlayout->addLayout(this->layout4);//2
    //QHBoxLayout::setStretch(int index, int stretch)
    //水平布局添加的控件按照比例进行伸缩
    //两个控件索引0、1、2、3、4,分别按照1:2:2:1:2比例进行伸缩
    this->vlayout->setStretch(0,1);
    this->vlayout->setStretch(1,2);
    this->vlayout->setStretch(2,2);
    this->vlayout->setStretch(3,1);
    this->vlayout->setStretch(4,2);
    setLayout(vlayout);
   

setFixedSize(400,300)设置窗口大小
setSizePolicy设置大小策略(水平、垂直)
对齐方式setAlignment
setGeometry(60,80,30,20);手动布局,设置控件大小
setEchoMode(QLineEdit::Password)设置模式,一般是用小黑点覆盖你所输入的字符
addStretch();//增加伸缩量
addwidget()方法用于向布局中添加控件
addLayout()方法用于向布局中添加子布局
addLayout用于添加一个已经包含另外一个QLayout布局的盒子到行(或列)中,同时设置布局的拉伸因子
setStretch(int index, int stretch)水平布局添加的控件按照比例进行伸缩

//设置内部控件与边框的距离
void setContentsMargins(int left, int top, int right, int bottom) 
//设置两个子控件之间的间距
void setSpacing(int)
//设置水平布局器元素间距为20像素点
hlay->setSpacing(20);
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值