qt 开发笔记堆栈布局的应用

1.概要

画面中有一处位置,有个按钮点击后,这片位置完全换成另一个画面,这中情况特别适合用堆栈布局。

//堆栈布局的应用

#include <QStackedLayout>

QStackedLayout *layout = new QStackedLayout(this);

layout->setCurrentIndex(0);

//信号和槽的链接

QPushButton *foundButton = findChild<QPushButton *>("pushButton_2");

if (foundButton) {

// 连接点击事件

connect(foundButton, &QPushButton::clicked, this, &FormMy1::onButtonClicked);

}

private slots:

void onButtonClicked();

2.代码

2.1 FormMy1

复制代码
#ifndef FORMMY1_H
#define FORMMY1_H

#include <QWidget>
#include <QStackedLayout>

namespace Ui {
class FormMy1;
}

class FormMy1 : public QWidget
{
    Q_OBJECT

public:
    QStackedLayout *layout;
    explicit FormMy1(QWidget *parent = nullptr);
    ~FormMy1();
private slots:
    void onButtonClicked();
private:
    Ui::FormMy1 *ui;
};

#endif // FORMMY1_H

#include "formmy1.h"
#include "ui_formmy1.h"
#include <QPushButton>

FormMy1::FormMy1(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FormMy1)
{
    ui->setupUi(this);
    // 根据名称查找按钮
    QPushButton *foundButton = findChild<QPushButton *>("pushButton_2");
    if (foundButton) {
        // 连接点击事件
        connect(foundButton, &QPushButton::clicked, this, &FormMy1::onButtonClicked);
    }
}

void FormMy1::onButtonClicked() {
    // 处理按钮点击事件
    //qDebug("Button clicked!");
    layout->setCurrentIndex(1);
}

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

2.2 FormMy2

复制代码
#ifndef FORMMY2_H
#define FORMMY2_H

#include <QWidget>
#include <QStackedLayout>

namespace Ui {
class FormMy2;
}

class FormMy2 : public QWidget
{
    Q_OBJECT

public:
    QStackedLayout *layout;
    explicit FormMy2(QWidget *parent = nullptr);
    ~FormMy2();
private slots:
    void onButtonClicked();
private:
    Ui::FormMy2 *ui;
};

#endif // FORMMY2_H

#include "formmy2.h"
#include "ui_formmy2.h"

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

    // 根据名称查找按钮
    QPushButton *foundButton = findChild<QPushButton *>("pushButton");
    if (foundButton) {
        // 连接点击事件
        connect(foundButton, &QPushButton::clicked, this, &FormMy2::onButtonClicked);
    }
}

void FormMy2::onButtonClicked() {
    // 处理按钮点击事件
    //qDebug("Button clicked!");
    layout->setCurrentIndex(0);
}

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

2.3 FormStacked

复制代码
#ifndef FORMSTACKED_H
#define FORMSTACKED_H

#include <QWidget>

namespace Ui {
class FormStacked;
}

class FormStacked : public QWidget
{
    Q_OBJECT

public:
    explicit FormStacked(QWidget *parent = nullptr);
    ~FormStacked();

private:
    Ui::FormStacked *ui;
};

#endif // FORMSTACKED_H

#include "formstacked.h"
#include "ui_formstacked.h"
#include <QStackedLayout>
#include "formmy1.h"
#include "formmy2.h"

FormStacked::FormStacked(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::FormStacked)
{
    ui->setupUi(this);
    QStackedLayout *layout = new QStackedLayout(this);
    FormMy1* my1 = new FormMy1(this);
    my1->layout = layout;
    FormMy2* my2 = new FormMy2(this);
    my2->layout = layout;
    layout->addWidget(my1);
    layout->addWidget(my2);

    layout->setCurrentIndex(0); // 初始显示第二个窗口部件
    //this->setLayout(layout);
}

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

2.4 Widget

复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui {
class Widget;
}
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

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

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

#include "widget.h"
#include "ui_widget.h"

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

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

2.5 main.cpp

复制代码
#include "widget.h"

#include <QApplication>

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

3.运行结果

相关推荐
DKPT1 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
巴伦是只猫3 小时前
【机器学习笔记Ⅰ】13 正则化代价函数
人工智能·笔记·机器学习
程序员爱钓鱼5 小时前
【无标题】Go语言中的反射机制 — 元编程技巧与注意事项
开发语言·qt
无畏烧风6 小时前
[Qt] visual studio code 安装 Qt插件
qt
X_StarX8 小时前
【Unity笔记02】订阅事件-自动开门
笔记·学习·unity·游戏引擎·游戏开发·大学生
MingYue_SSS9 小时前
开关电源抄板学习
经验分享·笔记·嵌入式硬件·学习
巴伦是只猫9 小时前
【机器学习笔记 Ⅱ】1 神经网络
笔记·神经网络·机器学习
weixin_437398219 小时前
转Go学习笔记(2)进阶
服务器·笔记·后端·学习·架构·golang
teeeeeeemo10 小时前
回调函数 vs Promise vs async/await区别
开发语言·前端·javascript·笔记
坏柠11 小时前
C++ Qt 基础教程:信号与槽机制详解及 QPushButton 实战
c++·qt