亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

前言

學(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)文章

最新評(píng)論