qt 代码布局练习

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QIntValidator>
#include <QDoubleValidator>
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    test();
}

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

void MainWindow::test()
{
    //设置标题 --- 开始
     QWidget * widget2 = new QWidget(this);
     QPushButton* max_but =new QPushButton(); //最大化按钮
     QPushButton* minx_but =new QPushButton(); //最小化按钮
     QPushButton* close_but =new QPushButton(); //关闭按钮

     QLabel * ico = new QLabel("ico");
     QLabel* title = new QLabel("title");

     QVBoxLayout * mainLayout_ = new QVBoxLayout(); //主窗口垂直布局
     QHBoxLayout * horLayout_ = new QHBoxLayout(); //水平布局
     QVBoxLayout * vctLayout_ = new QVBoxLayout(); //垂直布局

     //水平布局
     QHBoxLayout * xLayout_ = new QHBoxLayout();//水平布局
     xLayout_->addSpacing(0);//设置水平布局之间的间距
     xLayout_->addWidget(ico);//添加到水平布局
     xLayout_->addWidget(title); //添加到水平布局xLayout->addWidget(ico);//添加到水平布局
     xLayout_->addWidget(max_but); //添加到水平布局
     xLayout_->addWidget(minx_but);//添加到水平布局
     xLayout_->addWidget(close_but);//添加到水平布局
     widget2->setLayout(xLayout_); // 布局到主窗口



     //设置标题 --- 结束


    QDoubleValidator* fvalidator = new QDoubleValidator(); //设置输入范围
    QWidget * widget = new QWidget(this);
    QVBoxLayout * mainLayout = new QVBoxLayout(); //主窗口垂直布局
    QHBoxLayout * horLayout = new QHBoxLayout(); //水平布局
    QVBoxLayout * vctLayout = new QVBoxLayout(); //垂直布局
    //
    QLabel* _lable = new QLabel("请输入");
    horLayout->addSpacing(0); //是在 setSpacing(int) 的基础之上再插入间距。这个距离可以是负值,表示后一个部件会覆盖在前一个部件上面。
    horLayout->addWidget(_lable); //addwidget()方法用于向布局中添加控件;
    //
    QLabel * x_lable = new QLabel("x:");
    QLabel * y_lable = new QLabel("y:");
    QLabel * z_lable = new QLabel("z:");

    QLineEdit* x_edit = new QLineEdit();
    x_edit->setValidator(fvalidator); //限制输入方式
    QLineEdit* y_edit = new QLineEdit();
    y_edit->setValidator(fvalidator); //限制输入方式
    QLineEdit* z_edit = new QLineEdit();
    z_edit->setValidator(fvalidator); //限制输入方式

    //水平布局
    QHBoxLayout * xLayout = new QHBoxLayout();//水平布局
    xLayout->addSpacing(20);//设置水平布局之间的间距
    xLayout->addWidget(x_lable);//添加到水平布局
    xLayout->addWidget(x_edit); //添加到水平布局

    QHBoxLayout * yLayout = new QHBoxLayout();//水平布局
    yLayout->addSpacing(20);//设置水平布局之间的间距
    yLayout->addWidget(y_lable);//添加到水平布局
    yLayout->addWidget(y_edit); //添加到水平布局

    QHBoxLayout * zLayout = new QHBoxLayout();//水平布局
    zLayout->addSpacing(20);//设置水平布局之间的间距
    zLayout->addWidget(z_lable);//添加到水平布局
    zLayout->addWidget(z_edit); //添加到水平布局

    QHBoxLayout * zLayout2 = new QHBoxLayout();//水平布局
    zLayout2->addSpacing(20);//设置水平布局之间的间距
    zLayout2->addWidget(widget2);//添加标题栏到水平布局

    //垂直布局
    vctLayout->addLayout(zLayout2); // 将水平布局x 添加到垂直布局
    vctLayout->addLayout(xLayout); // 将水平布局x 添加到垂直布局
    vctLayout->addLayout(yLayout); // 将水平布局y 添加到垂直布局
    vctLayout->addLayout(zLayout); // 将水平布局z 添加到垂直布局

    //mainLayout->addLayout(horLayout);//将所有水平布局  再添加到水平布局
    mainLayout->addLayout(vctLayout);//将所有垂直布局 再添加到水平布局
    mainLayout->addStretch(); //在主布局下添加一个弹簧

    //布局到主窗口
    widget->setLayout(mainLayout); // 布局到主窗口


    //
    this->setCentralWidget(widget);






}


posted @ 2022-04-29 18:20  雾枫  阅读(135)  评论(0编辑  收藏  举报