Qt布局管理器

布局管理器

 

设计模式实现布局

详情见工具栏

 

垂直布局,水平布局,打破布局。

代码实现布局

main.cpp

#include "testlayout.h"
#include <QApplication>
#include<QLabel>
#include<QLineEdit>
#include<QFormLayout>
#include<QRadioButton>
#include<QVBoxLayout>
#include<QPushButton>
#include<QSpacerItem>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TestLayout w;
   //添加部件并且布局
    //添加标签
    QLabel* nameLabel = new  QLabel("姓名:(&N)");
    QLabel* ageLabel = new  QLabel("年龄:(&A)");
    QLabel* emailLabel = new  QLabel("邮箱:(&E)");
    QLabel* doorLabel = new QLabel("门牌号码:");
     // 添加文本框
     QLineEdit* nameLineEdit = new QLineEdit;
     QLineEdit* ageLineEdit = new QLineEdit;
     QLineEdit* emailLineEdit = new QLineEdit;
     QLineEdit* doorNumLineEdit = new QLineEdit;


    //设置伙伴关系——绑定快捷键
    nameLabel->setBuddy(nameLineEdit);
    ageLabel->setBuddy(ageLineEdit);
    emailLabel->setBuddy(emailLineEdit);
    //添加布局
    //QFormLayout常用语表单布局
    QFormLayout* headerLayout = new QFormLayout;

    //将部件添加到布局管理器中
     headerLayout->addRow(nameLabel,nameLineEdit);
     headerLayout->addRow(ageLabel,ageLineEdit);
     headerLayout->addRow(emailLabel,emailLineEdit);
     headerLayout->addRow(doorLabel,doorNumLineEdit);
     //性别标签
     QLabel* sexLabel = new QLabel("性别:");
     //添加单选按钮
    QRadioButton* mBtn = new QRadioButton;
    QRadioButton* wBtn = new QRadioButton;
    mBtn->setText("男");
    wBtn->setText("女");
    //添加水平布局管理器
    QHBoxLayout* sexLayout = new QHBoxLayout;
    sexLayout->addWidget(sexLabel);
    sexLayout->addWidget(mBtn);
    sexLayout->addWidget(wBtn);

    //添加垂直布局管理器
    //将两个布局管理器添加到一起
    QVBoxLayout* mainLayout = new QVBoxLayout(&w);//参数-指定父窗体
    mainLayout->addLayout(headerLayout);//添加布局
    mainLayout->addLayout(sexLayout);
    //在性别选项下添加空白
    QSpacerItem* spacer = new QSpacerItem(30,30);
    mainLayout->addItem(spacer);//添加空隙对象
    //添加一个按钮
    QPushButton* okBtn = new QPushButton("确定");
    //将按钮添加到布局管理器中
    mainLayout->addWidget(okBtn);//添加部件
    mainLayout->setMargin(10);//与窗口的间隙
    mainLayout->setSpacing(20);//设置控件间的间隙
    //设置窗口布局管理器
    w.setLayout(mainLayout);
    w.show();
    return a.exec();
}

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓ 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值