最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

QT5多窗口跳轉(zhuǎn)實(shí)現(xiàn)步驟詳解

 更新時(shí)間:2024年12月30日 11:02:06   作者:有什么昵稱不存在  
這篇文章主要介紹了使用Qt5實(shí)現(xiàn)多窗口界面跳轉(zhuǎn)的過程,包括創(chuàng)建多個(gè)UI界面、設(shè)計(jì)按鈕連接槽函數(shù)以及實(shí)現(xiàn)界面之間的導(dǎo)航,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

學(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地址)

    這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(93.復(fù)原IP地址),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • c++的字符串string基本操作大全

    c++的字符串string基本操作大全

    C++中的string類型提供了豐富的字符串操作功能,包括創(chuàng)建、賦值、輸入輸出、查找、插入、刪除、截取等,同時(shí),string還支持關(guān)系運(yùn)算和與int值的轉(zhuǎn)換,本文給大家介紹c++的字符串string,感興趣的朋友跟隨小編一起看看吧
    2025-12-12
  • 深入理解void以及void指針的含義

    深入理解void以及void指針的含義

    本篇文章對小編void以及void指針的含義進(jìn)行了詳細(xì)的分析和介紹。需要的朋友參考下
    2013-05-05
  • C++?RAII在HotSpot?VM中的重要應(yīng)用解析

    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)單詞小助手改進(jìn)版

    C語言實(shí)現(xiàn)單詞小助手改進(jìn)版

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)單詞小助手的改進(jìn)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • 一篇帶你了解C語言--位操作詳情

    一篇帶你了解C語言--位操作詳情

    這篇文章主要介紹了關(guān)于C語言位運(yùn)算的簡單示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-08-08
  • C語言實(shí)現(xiàn)QQ窗口抖動(dòng)功能

    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)方法

    本篇文章是對使用C#控制Windows系統(tǒng)音量的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C++中四種加密算法之AES源代碼

    C++中四種加密算法之AES源代碼

    本篇文章主要介紹了C++中四種加密算法之AES源代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。
    2016-11-11
  • c++?虛繼承的實(shí)現(xiàn)示例

    c++?虛繼承的實(shí)現(xiàn)示例

    本文主要介紹了c++?虛繼承的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-06-06

最新評論

二连浩特市| 吉水县| 固安县| 报价| 磐石市| 庆阳市| 民乐县| 龙海市| 青神县| 永胜县| 肥东县| 樟树市| 安顺市| 泰兴市| 绥芬河市| 上饶县| 利津县| 北安市| 绍兴市| 台州市| 城步| 永年县| 平安县| 远安县| 连山| 泽库县| 葵青区| 娱乐| 尤溪县| 万全县| 孟连| 扶沟县| 化隆| 当雄县| 兴业县| 合江县| 喀喇| 乌拉特中旗| 南靖县| 柯坪县| 永兴县|