QT 布局嵌套

QT布局一共有四种,常用的三种,QHBoxLayout,QVBoxLayout,QGridLayout,在开发过程中,常常会碰到需要布局嵌套的情况,如果这样写

QVBoxLayout *m_layout = new QVBoxLayout(this);
QHBoxLayout *m_hlayout1 = new QHBoxLayout(this);
QHBoxLayout *m_hlayout2 = new QHBoxLayout(this);

m_layout->addLayout(m_hlayout1);
m_layout->addLayout(m_hlayout2);

QT会报警告:
image.png

那么这个时候它会采用哪个布局呢?最后添加的那个?不是的,它会都不采用,使用系统默认的布局。

那怎么实现嵌套布局呢?
1.使用QWidget过渡

QVBoxLayout *m_layout = new QVBoxLayout(this);

QWidget *m_widget1 = new QWidget(this);
QHBoxLayout *m_hlayout1 = new QHBoxLayout(m_widget1);

QWidget *m_widget2 = new QWidget(this);
QHBoxLayout *m_hlayout2 = new QHBoxLayout(m_widget2);

m_layout->addWidget(m_widget1);
m_layout->addWidget(m_widget2);

2.直接使用,构造里不传this进去(是否会内存泄漏?不会,addlayout会帮它管理好它的生命周期)

QVBoxLayout *m_layout = new QVBoxLayout(this);
QHBoxLayout *m_hlayout1 = new QHBoxLayout();
QHBoxLayout *m_hlayout2 = new QHBoxLayout();

m_layout->addLayout(m_hlayout1);
m_layout->addLayout(m_hlayout2);

测试代码与执行结果:

//hlayout.h
#ifndef HLAYOUT_H
#define HLAYOUT_H

#include 

class HLayout : public QHBoxLayout {
  public:
    HLayout(QWidget* parent = nullptr);
    ~HLayout();
};

#endif  // HLAYOUT_H

//hlayout.cpp
#include "hlayout.h"

#include 

HLayout::HLayout(QWidget* parent) : QHBoxLayout(parent) { qDebug() << "HLayout"; }

HLayout::~HLayout() { qDebug() << "~HLayout"; }

//vlayout.h
#ifndef VLAYOUT_H
#define VLAYOUT_H

#include 

class VLayout : public QVBoxLayout {
  public:
    VLayout(QWidget* parent = nullptr);
    ~VLayout();
};

#endif  // VLAYOUT_H

//vlayout.cpp
#include "vlayout.h"

#include 
VLayout::VLayout(QWidget* parent) : QVBoxLayout(parent) { qDebug() << "VLayout"; }

VLayout::~VLayout() { qDebug() << "~VLayout"; }

//test.h
#ifndef TEST_H
#define TEST_H

#include 

class Test : public QPushButton {
  public:
    Test(QWidget* parent = nullptr);
    ~Test();
};

#endif  // TEST_H

//test.cpp
#include "test.h"

#include 

Test::Test(QWidget* parent) : QPushButton(parent) { qDebug() << "Test"; }

Test::~Test() { qDebug() << "~Test"; }

//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

#include "hlayout.h"
#include "test.h"
#include "vlayout.h"

QT_BEGIN_NAMESPACE
namespace Ui {
    class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow {
    Q_OBJECT

  public:
    MainWindow(QWidget* parent = nullptr);
    ~MainWindow();

  private:
    Ui::MainWindow* ui;
    Test* m_test;
    Test* m_test1;

    VLayout* m_layout;

    HLayout* m_layout1;
    HLayout* m_layout2;
};
#endif  // MAINWINDOW_H

//mainwindow.cpp
#include "mainwindow.h"

#include "ui_mainwindow.h"

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

    m_test = new Test(this->centralWidget());
    m_test->setText("test");

    m_test1 = new Test(this->centralWidget());
    m_test1->setText("test1");

    m_layout = new VLayout(this->centralWidget());
    m_layout1 = new HLayout;
    m_layout2 = new HLayout;

    m_layout1->addWidget(m_test);

    m_layout2->addWidget(m_test1);

    m_layout->addLayout(m_layout1);
    m_layout->addLayout(m_layout2);
}

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

//main.cpp
#include "mainwindow.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

image.png

image.png

你可能感兴趣的:(qt5)