欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QT 界面几秒后消失并跳转到另一个界面

程序员文章站 2022-05-13 23:51:59
...

需求

登录界面(login)点击登录(login)按钮后,跳转到等待界面(loginloading),然后3s后关闭等待界面并切换到主界面(mainwindow)。

核心代码

login.h

#include <QWidget>
#include "loginloading.h"
#include <QTimer>
public:
    Login(QWidget *parent = nullptr);
    ~Login();

    void Qtimer();
    
...........省略

private slots:
    void on_login_clicked();
signals:
    void show_loginloading();

    void show_mainwindow();

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

    QTimer *m_timer;
};
#endif // Login_H

login.cpp

void Login::on_login_clicked()//登录按钮槽函数
{
    if(ui->lineEdit->text().trimmed() == tr("admin") && ui->lineEdit_2->text() == tr("admin"))//账号密码
        {
            this->hide();
            emit show_loginloading();
            m_timer = new QTimer(this);
            m_timer->setSingleShot(true);
            this->Qtimer();
            connect(m_timer, &QTimer::timeout, this, [=](){emit show_mainwindow();});
            connect(m_timer, &QTimer::timeout, this, [=](){emit close_loginloading();});
        }
        
..........省略

}
void Login::Qtimer()
{
    m_timer->start(3000);
}

loginloading.h

private slots:
    void receive_login_login();

    void receive_close_loginloading();

loginloading.cpp

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

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

mainwindow.h

private slots:
	void receive_login_mainwindow();

mainwindow.cpp

void MainWindow::receive_login_mainwindow()
{
    this->show();
}

main.cpp

..........省略
   
    Login l;
    MainWindow w;
    LoginLoading ll;
    l.show();
    QObject::connect(&l,SIGNAL(show_loginloading()),&ll,SLOT(receive_login_login()));
    QObject::connect(&l,SIGNAL(show_mainwindow()),&w,SLOT(receive_login_mainwindow()));
    QObject::connect(&l,SIGNAL(close_loginloading()),&ll,SLOT(receive_close_loginloading()));
    return a.exec();
}

参考

https://blog.csdn.net/xiezhongyuan07/article/details/79885546