在QWidget里面布局的例子

 

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include<QScreen>
#include<QGridLayout>
#include<QLabel>
#include<QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);



    int widgetWidth=this->width();
    int widgetHeight=this->height();

    QWidget* myWidget=new QWidget(this);
    myWidget->setStyleSheet(".QWidget{border: 3px solid red;}");
    myWidget->setGeometry(widgetWidth/4,widgetHeight/4,widgetWidth/2,widgetHeight/2);
    myWidget->show();

    //下面开始布局
    QGridLayout* layout = new QGridLayout(myWidget);

    QLabel *label0=new QLabel();
    label0->setText("hahah");
    label0->setAlignment(Qt::AlignCenter);
    label0->setStyleSheet(".QLabel{border: 3px solid green;}");

    QLabel *label1=new QLabel();
    label1->setText("heheh");
    label1->setAlignment(Qt::AlignCenter);
    label1->setStyleSheet(".QLabel{border: 3px solid green;}");


    QPushButton *button0=new QPushButton();
    button0->setText("b0");
    QPushButton *button1=new QPushButton();
    button1->setText("b1");



     // label0->setMinimumSize(160, 30);//设置最小尺寸
      label0->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

      //label1->setMinimumSize(160, 30);//设置最小尺寸
      label1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

     // button0->setMinimumSize(160, 30);//设置最小尺寸
      button0->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

      //button1->setMinimumSize(160, 30);//设置最小尺寸
      button1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);



    //layout->setSpacing(100);//设置间距
    layout->addWidget(label0, 0, 0);//往网格的不同坐标添加不同的组件
    layout->addWidget(label1, 0, 1);
    layout->addWidget(button0, 1, 0);
    layout->addWidget(button1, 1, 1);

    layout->setRowStretch(0, 3);//设置行列比例系数
    layout->setRowStretch(1, 1);
    layout->setColumnStretch(0, 1);
    layout->setColumnStretch(1, 1);

    setLayout(layout);//设置顶级布局管理器




}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::GetScreenSize()//没使用,供参考
{

    QScreen *screen=QGuiApplication::primaryScreen ();
    QRect mm=screen->availableGeometry() ;
    screenWidth = mm.width();
    screenHeight = mm.height();


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值