QT5多窗口跳轉(zhuǎn)實(shí)現(xiàn)步驟詳解
前言
學(xué)習(xí)使用qt5完成多窗口(界面)跳轉(zhuǎn):從主界面可分別跳轉(zhuǎn)至界面一和界面二,從界面一可以返回主界面和跳轉(zhuǎn)至界面二,界面二可返回對(duì)應(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("前往頁(yè)面一"); page2_button =new QPushButton("前往頁(yè)面二"); btn_hlayout->addWidget(page1_button); btn_hlayout->addWidget(page2_button); // 跳轉(zhuǎn)到子窗口 connect(page1_button, &QPushButton::clicked, this, &MainWindow::on_page1_button_clicked); //接收返回信號(hào)顯示當(dāng)前窗口 connect(page1,&Page1Window::goback,this,[=](){page1->close();this->show();}); Page2Window *Page2FrommMin = new class Page2FromMain(this); // 使用匿名內(nèi)部類(lè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("返回主頁(yè)面"); page2_button =new QPushButton("前往頁(yè)面二"); 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)部類(lè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ù),需要在子類(lèi)中實(shí)現(xiàn) private: Ui::Page2Window *ui; QWidget *Widget; QPushButton *return_button; QHBoxLayout *btn_hlayout; //水平布局 }; //頁(yè)面二(從主頁(yè)面跳轉(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(); } }; //頁(yè)面二(從頁(yè)面一跳轉(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("返回父頁(yè)面"); btn_hlayout->addWidget(return_button); connect(return_button, &QPushButton::clicked, this, &Page2Window::on_return_btn_clicked); } Page2Window::~Page2Window() { delete ui; }
四、效果圖
總結(jié)
通過(guò)qt5實(shí)現(xiàn)了多頁(yè)面之間的跳轉(zhuǎn),在此過(guò)程中使用了虛函數(shù)(c語(yǔ)言沒(méi)有),看來(lái)學(xué)習(xí)的任務(wù)依舊任重而道遠(yuǎn)。另外,使用此方式進(jìn)行界面跳轉(zhuǎn)時(shí)Page2Window的基類(lèi)貌似只能使用MainWindow而不能是widget。
到此這篇關(guān)于QT5多窗口跳轉(zhuǎn)實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)QT5多窗口跳轉(zhuǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Visual?Studio?2022?激活碼(親測(cè)可用)
在?Visual?Studio?2019?的基礎(chǔ)上,新版集成開(kāi)發(fā)壞境提供了非常多的改進(jìn),包括對(duì)?64?位、.NET?6?的支持,為核心調(diào)試器提供更好的性能。本文給大家分享Visual?Studio?2022?激活碼,需要的朋友參考下吧2021-12-12數(shù)據(jù)結(jié)構(gòu)C語(yǔ)言鏈表的實(shí)現(xiàn)介紹
大家好,本篇文章主要講的是數(shù)據(jù)結(jié)構(gòu)C語(yǔ)言鏈表的實(shí)現(xiàn)介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2021-12-12C++動(dòng)態(tài)規(guī)劃計(jì)算最大子數(shù)組
所謂最大子數(shù)組就是連續(xù)的若干數(shù)組元素,如果其和是最大的,那么這個(gè)子數(shù)組就稱(chēng)為該數(shù)組的最大子數(shù)組2022-06-06c語(yǔ)言 字符串轉(zhuǎn)大寫(xiě)的簡(jiǎn)單實(shí)例
這篇文章主要介紹了c語(yǔ)言 字符串轉(zhuǎn)大寫(xiě)的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-12-12C++設(shè)計(jì)模式之簡(jiǎn)單工廠模式實(shí)例
這篇文章主要介紹了C++設(shè)計(jì)模式之簡(jiǎn)單工廠模式實(shí)例,工廠模式有一種非常形象的描述,建立對(duì)象的類(lèi)就如一個(gè)工廠,而需要被建立的對(duì)象就是一個(gè)個(gè)產(chǎn)品,需要的朋友可以參考下2014-09-09C語(yǔ)言練習(xí)題:自由落體的小球簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇C語(yǔ)言練習(xí)題:自由落體的小球簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧2016-05-05判斷一個(gè)數(shù)是不是素?cái)?shù)的方法
判斷一個(gè)數(shù)是不是素?cái)?shù)的方法,需要的朋友可以參考一下2013-03-03