Solve the problem of Qt Widget type UI interface switching at one time (the simplest)

This article is very suitable for my novice study, please check it carefully.
Note: The code is for learning only, not for commercial use!

1. The parent interface of the Qt Widget type UI interface calls the child interface

A more common solution found on the Internet is
https://blog.csdn.net/qq_40785363/article/details/79527343

But the second interface is the Dialog interface template. If you create a Widget template, d.exec(); will report an error because the exec() function is a Dialog function

So how to implement the Widget type interface?

After searching hard, I finally referred to https://wangbaiyuan.cn/qt-multi-window-switches.html and got the solution

Login sample core code

Take login switch to login as an example (practice):
login.h (parent window)

#include <QWidget>
#include "loginloading.h"

....

private slots:
    void on_login_clicked();

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

loginloading.h (child window)

#include <QWidget>
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();
}

2. Mutual call display of multiple interfaces

The above method is actually a bit tricky. It is only suitable for a case where a base class interface (parent interface) calls a derived class interface (child interface). When multiple interfaces call and display each other, you need to use Qt's unique slot function and signal.

How to understand signals and slots

For example, if loginid wants to jump to the login interface, loginid needs to send the show_login() signal, then define the slot function receive_loginid_login() corresponding to the login request sent by loginid in the login interface, and finally call show_login( in the slot function of the corresponding login button ) To display the login interface. For specific implementation, see the following code~

effect

The interfaces shown successively are loginid, login, loginloading
Insert picture description here

Engineering structure

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

Login to switch complete code

loginid.h

#ifndef LOGINID_H
#define LOGINID_H

#include <QWidget>

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 <QWidget>
#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 <QWidget>

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 <QApplication>
#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 <QMouseEvent>

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 <QMouseEvent>
#include <QMessageBox>
#include <QDebug>

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 <QMouseEvent>

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();
}

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

Guess you like

Origin blog.csdn.net/qq_16488989/article/details/108976045