一次性解决Qt Widget类型Ui界面切换问题(最简单)

本文非常适合我这种小白学习,请君仔细查看。
12月9日,回看两个月前写的此篇博客,第一种方法是父界面调用子界面,第二种方法多个界面之间没有继承关系。

目录

  • 一、Qt Widget类型Ui界面之父界面调用子界面
    • 登录示例核心代码
  • 二、多个界面相互调用显示
    • 如何理解信号与槽
    • 效果
    • 工程结构
    • 登录切换完整代码

一、Qt Widget类型Ui界面之父界面调用子界面

网上搜到的一种比较常见的解决方案是
https://blog.csdn.net/qq_40785363/article/details/79527343

但是第二个界面是Dialog界面模版,如果创建的是 Widget模版,d.exec();这句话就会报错,因为:exec()函数是Dialog的函数

那么对于Widget类型的界面怎么实现呢?

苦苦搜索了一番,最后参考了https://wangbaiyuan.cn/qt-multi-window-switches.html,得到了解决

登录示例核心代码

以登录切换到登录中作为例子(已实践):
login.h(父窗口)

#include 
#include "loginloading.h"

....

private slots:
    void on_login_clicked();

private:
    Ui::Login *ui;
    LoginLoading ll;
};

loginloading.h(子窗口)

#include 
private slots:
    void on_close_clicked();

main.cpp

#include "login.h"
    Login l;
    l.show();

login.cpp

#include "login.h"
#include "ui_login.h"
void Login::on_login_clicked()
{
    this->hide();
    ll.show();
    this->close();
}

loginloading.cpp

#include "loginloading.h"
#include "ui_loginloading.h"
void LoginLoading::on_close_clicked()
{
    this->close();
}

二、多个界面相互调用显示

上面的方法实际上有点讨巧,只适用于一个基类界面(父界面)调用一个派生类界面(子界面)的情况,当多个界面之间相互调用显示时,需要采用Qt特有的槽函数与信号。

如何理解信号与槽

比如loginid要跳转到login界面,就需要loginid发送show_login()信号,然后在login界面中定义loginid发送的登录请求对应的槽函数receive_loginid_login(),最后在对应的login按钮的槽函数中调用show_login()即可显示login界面了。具体实现参看下面的代码~

效果

先后展示的界面是loginid,login,loginloading
一次性解决Qt Widget类型Ui界面切换问题(最简单)_第1张图片

工程结构

Headers:login.h,loginid.h,loginloading.h
Sources:login.cpp,loginid.cpp,loginloading.cpp
Forms:login.ui,loginid.ui,loginloading.ui

登录切换完整代码

loginid.h

#ifndef LOGINID_H
#define LOGINID_H

#include 

namespace Ui {
class LoginId;
}

class LoginId : public QWidget
{
    Q_OBJECT

public:
    explicit LoginId(QWidget *parent = nullptr);
    ~LoginId();

protected:
    void mouseMoveEvent(QMouseEvent *e);//鼠标移动
    void mousePressEvent(QMouseEvent *e);//鼠标按下移动

private slots:
    void on_close_clicked();

    void on_minimized_clicked();

    void on_login_clicked();

    void receive_back();

signals:
    void show_login();

private:
    Ui::LoginId *ui;
    QPoint p;
};

#endif // LOGINID_H

login.h

#ifndef LOGIN_H
#define LOGIN_H
#include 
#include "loginloading.h"
QT_BEGIN_NAMESPACE
namespace Ui { class Login; }
QT_END_NAMESPACE

class Login : public QWidget
{
    Q_OBJECT

public:
    Login(QWidget *parent = nullptr);
    ~Login();

protected:

    void mouseMoveEvent(QMouseEvent *e);//鼠标移动
    void mousePressEvent(QMouseEvent *e);//鼠标按下移动

private slots:
    void on_close_clicked();

    void on_minimized_clicked();

    void on_login_clicked();

    void on_back_clicked();

    void receive_loginid_login();

signals:
    void show_loginloading();

    void show_loginid();
private:
    Ui::Login *ui;

    QPoint p;

};
#endif // Login_H

loginloading.h

#ifndef LOGINLOADING_H
#define LOGINLOADING_H

#include 

namespace Ui {
class LoginLoading;
}

class LoginLoading : public QWidget
{
    Q_OBJECT

public:
    explicit LoginLoading(QWidget *parent = nullptr);
    ~LoginLoading();

protected:

    void mouseMoveEvent(QMouseEvent *e);//鼠标移动
    void mousePressEvent(QMouseEvent *e);//鼠标按下移动

private slots:
    void on_pushButton_clicked();

    void receive_login_login();

private:
    Ui::LoginLoading *ui;

    QPoint p;
};

#endif // LOGINLOADING_H

main.cpp`

#include "login.h"
#include 
#include "loginid.h"
#include "loginloading.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    LoginId li;
    Login l;
    MainWindow w;
    LoginLoading ll;
    li.show();

    QObject::connect(&li,SIGNAL(show_login()),&l,SLOT(receive_loginid_login()));
    QObject::connect(&l,SIGNAL(show_loginid()),&li,SLOT(receive_back()));
    QObject::connect(&l,SIGNAL(show_loginloading()),&ll,SLOT(receive_login_login()));

//    w.checkLicense();

    return a.exec();

}
loginid.cpp

#include "loginid.h"
#include "ui_loginid.h"
#include 

LoginId::LoginId(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginId)
{
    ui->setupUi(this);

    setWindowFlags(Qt::FramelessWindowHint | windowFlags());

    //把窗口背景设置为透明;
        setAttribute(Qt::WA_TranslucentBackground);
}

LoginId::~LoginId()
{
    delete ui;
}


void LoginId::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        //求坐标差值
        //当前点击坐标-窗口左上角坐标
        p = e->globalPos() - this->frameGeometry().topLeft();
    }
}

void LoginId::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons() & Qt::LeftButton)
    {
        //移到左上角
        move(e->globalPos() - p);
    }

}

void LoginId::on_close_clicked()
{
    close();
}
void LoginId::on_minimized_clicked()
{
    showMinimized();
}

void LoginId::on_login_clicked()
{
    this->hide();
    emit show_login();
}


void LoginId::receive_back()
{
    this->show();
}


login.cpp

#include "login.h"
#include "ui_login.h"
#include 
#include 
#include 

Login::Login(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Login)
{
    ui->setupUi(this);

    //去窗口边框
        setWindowFlags(Qt::FramelessWindowHint | windowFlags());

    //把窗口背景设置为透明;
        setAttribute(Qt::WA_TranslucentBackground);

}

Login::~Login()
{
    delete ui;
}


void Login::mousePressEvent(QMouseEvent *e)
{
//    if(e->button() == Qt::RightButton)
//    {
//        //如果是右键
//        close();
//    }
    if(e->button() == Qt::LeftButton)
    {
        //求坐标差值
        //当前点击坐标-窗口左上角坐标
        p = e->globalPos() - this->frameGeometry().topLeft();

    }
}

void Login::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons() & Qt::LeftButton)
    {
        //移到左上角
        move(e->globalPos() - p);
    }

}

void Login::on_close_clicked()
{
    close();
}
void Login::on_minimized_clicked()
{
    showMinimized();
}


void Login::on_login_clicked()
{
//    int i = ui->lineEdit->cursorPosition();
//    i+=10;
//    qDebug() << i;
//    ui->lineEdit->setCursorPosition(i);
//    ui->lineEdit_2->setCursorPosition(i);
//    ui->lineEdit->setFocus();
//    ui->lineEdit_2->setFocus();
    ui->lineEdit->setAlignment(Qt::AlignJustify);
    if(ui->lineEdit->text().trimmed() == tr("admin") && ui->lineEdit_2->text() == tr("admin"))
        {
            this->hide();
            emit show_loginloading();
        }
        else
        {
           QMessageBox mess(QMessageBox::Information,tr("Warning!"),tr("Wrong user name or password!"));
           mess.setWindowIcon(QIcon(":/main/logo"));
           mess.exec();

        // 清空输入框内容
           ui->lineEdit->clear();
           ui->lineEdit_2->clear();
           //光标定位
           ui->lineEdit->setFocus();
        }
}

void Login::on_back_clicked()
{
    this->hide();
    emit show_loginid();

}


void Login::receive_loginid_login()
{
    this->show();
}


loginloading.cpp

#include "loginloading.h"
#include "ui_loginloading.h"
#include 

LoginLoading::LoginLoading(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::LoginLoading)
{
    ui->setupUi(this);

    //去窗口边框
        setWindowFlags(Qt::FramelessWindowHint | windowFlags());

    //把窗口背景设置为透明;
        setAttribute(Qt::WA_TranslucentBackground);



}

LoginLoading::~LoginLoading()
{
    delete ui;
}


void LoginLoading::mousePressEvent(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        //求坐标差值
        //当前点击坐标-窗口左上角坐标
        p = e->globalPos() - this->frameGeometry().topLeft();
    }
}

void LoginLoading::mouseMoveEvent(QMouseEvent *e)
{
    if(e->buttons() & Qt::LeftButton)
    {
        //移到左上角
        move(e->globalPos() - p);
    }

}

void LoginLoading::on_pushButton_clicked()
{
    this->close();
}

void LoginLoading::receive_login_login()
{
    this->show();
}

参考:
https://jingyan.baidu.com/article/020278114309b51bcc9ce5af.html
https://blog.csdn.net/qq_40785363/article/details/79527343
https://wangbaiyuan.cn/qt-multi-window-switches.html

你可能感兴趣的:(#,Qt,Question,Qt,Practice,qt5,qt)