qt界面手动布局测试


*****************************************************mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

******************************************************mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QWidget>
#include <QSpacerItem>
#include <QGroupBox>
#include <QTextEdit>
#include <QSpinBox>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
   this->resize(500,200);
    QPushButton *pb1 = new QPushButton("Commit");
    QPushButton *pb2 = new QPushButton("Commit");
 
    //QGroupBox 给hl1中添加
    QGroupBox *gb1 = new QGroupBox("QGroupBox");
 
 
    QHBoxLayout *thl1 = new QHBoxLayout();
    thl1->addWidget(new QLabel("Title1"));
    thl1->addWidget(new QLineEdit());
    QHBoxLayout *thl2 = new QHBoxLayout();
    thl2->addWidget(new QLabel("Title2"));
    thl2->addWidget(new QLineEdit());
    QHBoxLayout *thl3 = new QHBoxLayout();
    thl3->addWidget(new QLabel("Title3"));
    thl3->addWidget(new QLineEdit());
    QHBoxLayout *thl4 = new QHBoxLayout();
    thl4->addWidget(new QSpinBox());
    thl4->addWidget(new QLineEdit());
 
    QVBoxLayout *tvl = new QVBoxLayout();
    tvl->addLayout(thl1);
    tvl->addLayout(thl2);
    tvl->addLayout(thl3);
    tvl->addLayout(thl4);
    gb1->setLayout(tvl);
 
 
    QVBoxLayout *hl1 = new QVBoxLayout(this);
    hl1->addWidget(gb1);
    hl1->addWidget(pb1);
 
    QVBoxLayout *hl2 = new QVBoxLayout(this);
    hl2->addWidget(new QTextEdit("Test textedit..."));
    hl2->addWidget(pb2);
 
    //将两个垂直的布局键入到一个水平布局中
    QHBoxLayout *hl3 = new QHBoxLayout(this); //为一个水平布局
    hl3->addLayout(hl1);
    QSpacerItem *spaceItem = new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum);
    hl3->addSpacerItem(spaceItem);
    hl3->addLayout(hl2);
 
 
    QVBoxLayout *mainVbox = new QVBoxLayout(this);//最外层的vbox
 
    QHBoxLayout *bottomHbox = new QHBoxLayout();//最外层vbox 最下面的hbox
 
    QLabel *qlabel_1 = new QLabel("BottomLabel");
    QLineEdit *lineEdit_1 = new QLineEdit();
    bottomHbox->addWidget(qlabel_1);
    bottomHbox->addWidget(lineEdit_1);
    //将两个水平布局加入最外层的垂直布局
    mainVbox->addLayout(hl3);
    mainVbox->addLayout(bottomHbox);
 
 
    QWidget *widget = new QWidget();
    widget->setLayout(mainVbox);
 
    this->setCentralWidget(widget);
 
}
 
MainWindow::~MainWindow()
{
    //delete ui;
}
 
 
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值