QT5多窗口跳轉(zhuǎn)實(shí)現(xiàn)步驟詳解
前言
學(xué)習(xí)使用qt5完成多窗口(界面)跳轉(zhuǎn):從主界面可分別跳轉(zhuǎn)至界面一和界面二,從界面一可以返回主界面和跳轉(zhuǎn)至界面二,界面二可返回對應(yīng)的父界面(從主界面跳轉(zhuǎn)則返回主界面,從界面一跳轉(zhuǎn)則返回界面一)。
一、環(huán)境
qt版本:5.12.7
windows 11 下的 Qt Designer (已搭建)
編譯器:MingGW
二、步驟
1.在Designer 創(chuàng)建一個(gè)新的qt工程
2.選中工程選擇Add New.. 添加兩個(gè)新的ui界面page1window和page2window,界面模板使用MainWindow。



3.在主界面創(chuàng)建兩個(gè)button分別跳轉(zhuǎn)至界面一和界面二。
4.在界面一創(chuàng)建兩個(gè)button分別跳轉(zhuǎn)至界面二和返回主界面。
5.在界面三創(chuàng)建一個(gè)button用于返回其父界面。
6.連接槽函數(shù)。
三、代碼實(shí)現(xiàn)
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QHBoxLayout>
#include "page1window.h"
#include "page2window.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_page1_button_clicked();
void on_page2_button_clicked();
private:
Ui::MainWindow *ui;
Page1Window *page1=NULL;
Page2Window *page2=NULL;
QPushButton *page1_button=NULL;
QPushButton *page2_button=NULL;
QHBoxLayout *btn_hlayout; //水平布局
};
#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);
QWidget *widget=new QWidget(this);
this->setCentralWidget(widget); // 設(shè)置為中心部件
btn_hlayout = new QHBoxLayout(widget);
page1=new Page1Window(this);
page1_button =new QPushButton("前往頁面一");
page2_button =new QPushButton("前往頁面二");
btn_hlayout->addWidget(page1_button);
btn_hlayout->addWidget(page2_button);
// 跳轉(zhuǎn)到子窗口
connect(page1_button, &QPushButton::clicked, this, &MainWindow::on_page1_button_clicked);
//接收返回信號顯示當(dāng)前窗口
connect(page1,&Page1Window::goback,this,[=](){page1->close();this->show();});
Page2Window *Page2FrommMin = new class Page2FromMain(this); // 使用匿名內(nèi)部類
connect(page2_button, &QPushButton::clicked, this, [=]() {Page2FrommMin->show();this->hide();});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_page1_button_clicked()
{
this->hide();
page1->show();
}
void MainWindow::on_page2_button_clicked()
{
this->hide();
page2->show();
}
pgge1window.h:
#ifndef PAGE1WINDOW_H
#define PAGE1WINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QHBoxLayout>
namespace Ui {
class Page1Window;
}
class Page1Window : public QMainWindow
{
Q_OBJECT
public:
explicit Page1Window(QWidget *parent = nullptr);
~Page1Window();
signals:
void goback();
private slots:
void on_return_btn_clicked();
private:
Ui::Page1Window *ui;
QPushButton *return_button=NULL;
QPushButton *page2_button=NULL;
QHBoxLayout *btn_hlayout; //水平布局
};
#endif // PAGE1WINDOW_H
pgge1window.cpp:
#include "page1window.h"
#include "ui_page1window.h"
#include "page2window.h"
Page1Window::Page1Window(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Page1Window)
{
ui->setupUi(this);
QWidget *widget=new QWidget(this);
this->setCentralWidget(widget); // 設(shè)置為中心部件
btn_hlayout = new QHBoxLayout(widget);
return_button =new QPushButton("返回主頁面");
page2_button =new QPushButton("前往頁面二");
btn_hlayout->addWidget(return_button);
btn_hlayout->addWidget(page2_button);
connect(return_button, &QPushButton::clicked, this, &Page1Window::on_return_btn_clicked);
Page2Window *Page2FrommPage1 = new class Page2FromPage1(this); // 使用匿名內(nèi)部類
connect(page2_button, &QPushButton::clicked, this, [=]() {Page2FrommPage1->show();this->hide();});
}
Page1Window::~Page1Window()
{
delete ui;
}
void Page1Window::on_return_btn_clicked()
{
emit goback();
}
pgge2window.h:
#ifndef PAGE2WINDOW_H
#define PAGE2WINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include <QHBoxLayout>
namespace Ui {
class Page2Window;
}
class Page2Window : public QMainWindow
{
Q_OBJECT
public:
explicit Page2Window(QWidget *parent = nullptr);
~Page2Window();
virtual void on_return_btn_clicked() = 0; // 純虛函數(shù),需要在子類中實(shí)現(xiàn)
private:
Ui::Page2Window *ui;
QWidget *Widget;
QPushButton *return_button;
QHBoxLayout *btn_hlayout; //水平布局
};
//頁面二(從主頁面跳轉(zhuǎn))
class Page2FromMain : public Page2Window {
QWidget *parentWindow1;
public:
Page2FromMain(QWidget *parent = nullptr) : Page2Window(parent), parentWindow1(parent) {
}
void on_return_btn_clicked() override {
parentWindow1->show();
this->hide();
}
};
//頁面二(從頁面一跳轉(zhuǎn))
class Page2FromPage1 : public Page2Window {
QWidget *parentWindow2;
public:
Page2FromPage1(QWidget *parent = nullptr) : Page2Window(parent), parentWindow2(parent) {
}
void on_return_btn_clicked() override {
parentWindow2->show();
this->hide();
}
};
#endif // PAGE2WINDOW_H
pgge2window.cpp:
#include "page2window.h"
#include "ui_page2window.h"
Page2Window::Page2Window(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Page2Window)
{
ui->setupUi(this);
QWidget *widget=new QWidget(this);
this->setCentralWidget(widget); // 設(shè)置為中心部件
btn_hlayout = new QHBoxLayout(widget);
return_button =new QPushButton("返回父頁面");
btn_hlayout->addWidget(return_button);
connect(return_button, &QPushButton::clicked, this, &Page2Window::on_return_btn_clicked);
}
Page2Window::~Page2Window()
{
delete ui;
}
四、效果圖



總結(jié)
通過qt5實(shí)現(xiàn)了多頁面之間的跳轉(zhuǎn),在此過程中使用了虛函數(shù)(c語言沒有),看來學(xué)習(xí)的任務(wù)依舊任重而道遠(yuǎn)。另外,使用此方式進(jìn)行界面跳轉(zhuǎn)時(shí)Page2Window的基類貌似只能使用MainWindow而不能是widget。
到此這篇關(guān)于QT5多窗口跳轉(zhuǎn)實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)QT5多窗口跳轉(zhuǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(93.復(fù)原IP地址)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(93.復(fù)原IP地址),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++?RAII在HotSpot?VM中的重要應(yīng)用解析
RAII技術(shù)被認(rèn)為是C++中管理資源的最佳方法,進(jìn)一步引申,使用RAII技術(shù)也可以實(shí)現(xiàn)安全、簡潔的狀態(tài)管理,編寫出優(yōu)雅的異常安全的代碼,這篇文章主要介紹了C++?RAII在HotSpot?VM中的重要應(yīng)用,需要的朋友可以參考下2023-09-09
C語言實(shí)現(xiàn)QQ窗口抖動(dòng)功能
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)QQ窗口抖動(dòng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
用C# 控制Windows系統(tǒng)音量的實(shí)現(xiàn)方法
本篇文章是對使用C#控制Windows系統(tǒng)音量的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

