QT QGridLayout QHBoxLayout QVBoxLayout 布局管理器运用

 

最近在学习QT编程,在百度贴吧中看到有人问关于窗口布局的问题 结合最近学到的东西 用代码实现了下该功能

http://tieba.baidu.com/p/2473655082 

 
    QWidget* widget = new QWidget;
    widget->setFixedSize(350,300);
    widget->setWindowTitle("UnicomMini");
 
    //设置支持中文
    QTextCodec *codec = QTextCodec::codecForName("System");
    QTextCodec::setCodecForLocale(codec);
    QTextCodec::setCodecForTr(codec);
    QTextCodec::setCodecForCStrings(codec);
 
    QLabel* srceenAreaLabel = new QLabel(QObject::tr("屏幕区"), widget);
 
    QGridLayout* centerBtnLayout = new QGridLayout;
 
   // centerBtnLayout->addStretch();
    QPushButton* chongzhiBtn = new QPushButton(QObject::tr("充值缴费"), widget);
    QPushButton* yuechaxunBtn = new QPushButton(QObject::tr("余额查询"), widget);
    QPushButton* zhangdanchaxunBtn = new QPushButton(QObject::tr("账单查询"), widget);
    QPushButton* detailchaxunBtn = new QPushButton(QObject::tr("详细查询"),  widget);
    QPushButton* buycardBtn = new QPushButton(QObject::tr("买电子卡"), widget);
    QPushButton* syssettingBtn = new QPushButton(QObject::tr("系统设置"), widget);
 
    centerBtnLayout->addWidget(chongzhiBtn,0,0,1,1);
    centerBtnLayout->addWidget(yuechaxunBtn,0,1,1,1);
    centerBtnLayout->addWidget(zhangdanchaxunBtn,1,0,1,1);
    centerBtnLayout->addWidget(detailchaxunBtn,1,1,1,1);
    centerBtnLayout->addWidget(buycardBtn,2,0,1,1);
    centerBtnLayout->addWidget(syssettingBtn,2,1,1,1);
    centerBtnLayout->setContentsMargins(40,1, 40, 0);   // 设置下距离边框的距离
 
    QLabel* keyAreaLabel = new QLabel(QObject::tr("键盘区"), widget);
 
    QHBoxLayout* hKeyAreaLayout = new QHBoxLayout;
    QStringList btnsTextList;
    btnsTextList<<"0"<<"1"<<"2"<<"3"<<"4"<<"5"<<"6"<<"7"<<"8"<<"9"<<"<-";
    for( int i= 0; i< 11; i++)
    {
        QPushButton* btn = new QPushButton(btnsTextList.at(i), widget );
        btn->setFixedSize(25,25);
        hKeyAreaLayout->addWidget(btn);
    }
 
    QGridLayout* direction = new QGridLayout;
    QPushButton* topBtn = new QPushButton(QObject::tr("上"),widget);
    QPushButton* leftBtn = new QPushButton(QObject::tr("左"),widget);
    QPushButton* downBtn = new QPushButton(QObject::tr("下"),widget);
    QPushButton* rightBtn = new QPushButton(QObject::tr("右"),widget);
 
    direction->addWidget(topBtn, 0,1, 1,1);
    direction->addWidget(leftBtn, 1,0, 1,1);
    direction->addWidget(downBtn, 1,1, 1,1);
    direction->addWidget(rightBtn, 1,2, 1,1);
 
    QVBoxLayout* mainLayout = new QVBoxLayout;
    mainLayout->addWidget(srceenAreaLabel);
    mainLayout->addLayout(centerBtnLayout);
    mainLayout->addWidget(keyAreaLabel);
    mainLayout->addLayout(hKeyAreaLayout);
    mainLayout->setSpacing(10);
    mainLayout->addLayout(direction);
 
    widget->setLayout(mainLayout);
    widget->show();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值