共计 2899 个字符,预计需要花费 8 分钟才能阅读完成。
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 会报正告:
那么这个时候它会采纳哪个布局呢?最初增加的那个?不是的,它会都不采纳,应用零碎默认的布局。
那怎么实现嵌套布局呢?
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 <QHBoxLayout>
class HLayout : public QHBoxLayout {
public:
HLayout(QWidget* parent = nullptr);
~HLayout();};
#endif // HLAYOUT_H
//hlayout.cpp
#include "hlayout.h"
#include <QDebug>
HLayout::HLayout(QWidget* parent) : QHBoxLayout(parent) {qDebug() << "HLayout"; }
HLayout::~HLayout() { qDebug() << "~HLayout"; }
//vlayout.h
#ifndef VLAYOUT_H
#define VLAYOUT_H
#include <QVBoxLayout>
class VLayout : public QVBoxLayout {
public:
VLayout(QWidget* parent = nullptr);
~VLayout();};
#endif // VLAYOUT_H
//vlayout.cpp
#include "vlayout.h"
#include <QDebug>
VLayout::VLayout(QWidget* parent) : QVBoxLayout(parent) {qDebug() << "VLayout"; }
VLayout::~VLayout() { qDebug() << "~VLayout"; }
//test.h
#ifndef TEST_H
#define TEST_H
#include <QPushButton>
class Test : public QPushButton {
public:
Test(QWidget* parent = nullptr);
~Test();};
#endif // TEST_H
//test.cpp
#include "test.h"
#include <QDebug>
Test::Test(QWidget* parent) : QPushButton(parent) {qDebug() << "Test"; }
Test::~Test() { qDebug() << "~Test"; }
//mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#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 <QApplication>
int main(int argc, char *argv[])
{QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();}
正文完