qt点击按钮跳转页面
时间: 2023-10-22 12:30:58 浏览: 229
在Qt中,要实现按钮点击后跳转页面,可以使用QStackedWidget、QStackedLayout或QTabWidget来管理多个页面。下面是一个简单的示例:
1. 创建两个页面的QWidget子类,比如Page1和Page2。
```cpp
// page1.h
#ifndef PAGE1_H
#define PAGE1_H
#include <QWidget>
class Page1 : public QWidget
{
Q_OBJECT
public:
explicit Page1(QWidget *parent = nullptr);
signals:
void goToPage2();
private slots:
void onButtonClicked();
};
#endif // PAGE1_H
// page1.cpp
#include "page1.h"
#include <QPushButton>
#include <QVBoxLayout>
Page1::Page1(QWidget *parent) : QWidget(parent)
{
QPushButton *button = new QPushButton("Go to Page 2", this);
connect(button, &QPushButton::clicked, this, &Page1::onButtonClicked);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(button);
setLayout(layout);
}
void Page1::onButtonClicked()
{
emit goToPage2();
}
```
```cpp
// page2.h
#ifndef PAGE2_H
#define PAGE2_H
#include <QWidget>
class Page2 : public QWidget
{
Q_OBJECT
public:
explicit Page2(QWidget *parent = nullptr);
signals:
void goToPage1();
private slots:
void onButtonClicked();
};
#endif // PAGE2_H
// page2.cpp
#include "page2.h"
#include <QPushButton>
#include <QVBoxLayout>
Page2::Page2(QWidget *parent) : QWidget(parent)
{
QPushButton *button = new QPushButton("Go back to Page 1", this);
connect(button, &QPushButton::clicked, this, &Page2::onButtonClicked);
QVBoxLayout *layout = new QVBoxLayout(this);
layout->addWidget(button);
setLayout(layout);
}
void Page2::onButtonClicked()
{
emit goToPage1();
}
```
2. 创建一个主窗口的QWidget子类,比如MainWindow,使用QStackedWidget作为主要的页面管理器。
```cpp
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStackedWidget>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void goToPage2();
void goToPage1();
private:
QStackedWidget *stackedWidget;
};
#endif // MAINWINDOW_H
// mainwindow.cpp
#include "mainwindow.h"
#include "page1.h"
#include "page2.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
stackedWidget = new QStackedWidget(this);
setCentralWidget(stackedWidget);
Page1 *page1 = new Page1(this);
Page2 *page2 = new Page2(this);
stackedWidget->addWidget(page1);
stackedWidget->addWidget(page2);
connect(page1, &Page1::goToPage2, this, &MainWindow::goToPage2);
connect(page2, &Page2::goToPage1, this, &MainWindow::goToPage1);
}
MainWindow::~MainWindow()
{
}
void MainWindow::goToPage2()
{
stackedWidget->setCurrentIndex(1); // 切换到页面2
}
void MainWindow::goToPage1()
{
stackedWidget->setCurrentIndex(0); // 切换到页面1
}
```
3. 在main函数中创建并显示MainWindow。
```cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
```
以上代码演示了如何通过点击按钮在两个页面间跳转。Page1和Page2作为两个不同的QWidget子类,通过信号和槽机制实现按钮点击后的页面切换。MainWindow则使用QStackedWidget作为中央部件,将Page1和Page2添加进去,并连接按钮点击信号和页面切换槽函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)