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.运行结果