【嵌入式——QT】Splash与登录窗口

一般的大型应用程序在启动时会显示一个启动画面,即Splash窗口,Splash窗口是一个无边对话框,一般显示一个图片,展示软件信息,Splash窗口显示时,程序在后台做一些比较耗时的启动准备工作,Splash窗口显示一段时间后自动关闭,然后软件的主窗口显示出来,Qt有一个QSplashScreen类可以实现Splash窗口的功能,它提供了载入图片,自动设置窗口无边框效果等功能。

//设置为SplashScreen
this->setWindowFlags(Qt::SplashScreen);

图示
在这里插入图片描述

代码示例

SplashDialog.h

#ifndef SPLASHDIALOG_H
#define SPLASHDIALOG_H

#include <QDialog>
#include <QMouseEvent>
namespace Ui
{
    class SplashDialog;
}

class SplashDialog : public QDialog
{
    Q_OBJECT

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

    //鼠标按压事件
    void mousePressEvent(QMouseEvent* event) override;
    //鼠标移动事件
    void mouseMoveEvent(QMouseEvent* event) override;
    //鼠标释放事件
    void mouseReleaseEvent(QMouseEvent* event) override;

private slots:
    void on_pushButtonOk_clicked();

    void on_pushButtonCancel_clicked();

private:
    Ui::SplashDialog* ui;

    //窗口是否在鼠标操作下移动
    bool m_moving= false;
    //上一次鼠标位置
    QPoint m_lastPos;
    //用户名
    QString m_user = "user";
    //密码
    QString m_pswd = "12345";
    //试错次数
    int m_tryCount = 0;
    //读取设置
    void readSettings();
    //写入设置
    void writeSettings();
    //加密
    QString encrypt(const QString& str);

};

#endif // SPLASHDIALOG_H

SplashDialog.cpp

#include "SplashDialog.h"
#include "ui_SplashDialog.h"
#include <QSettings>
#include <QCryptographicHash>
#include <QMessageBox>
SplashDialog::SplashDialog(QWidget* parent)
    : QDialog(parent)
    , ui(new Ui::SplashDialog)
{
    ui->setupUi(this);
    //设置密码为输入模式
    ui->lineEditPassword->setEchoMode(QLineEdit::Password);
    //设置关闭时删除
    this->setAttribute(Qt::WA_DeleteOnClose);
    //设置为SplashScreen
    this->setWindowFlags(Qt::SplashScreen);
    readSettings();
}

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

void SplashDialog::mousePressEvent(QMouseEvent* event)
{
    if(event->button() == Qt::LeftButton) {
        m_moving = true;
        //记录下鼠标相对于窗口的位置
        m_lastPos = event->globalPos()-pos();
    }
    return QDialog::mousePressEvent(event);
}

void SplashDialog::mouseMoveEvent(QMouseEvent* event)
{
    if(m_moving && (event->buttons() && Qt::LeftButton) &&
       (event->globalPos()-m_lastPos).manhattanLength() > QApplication::startDragDistance()) {
        move(event->globalPos()-m_lastPos);
        m_lastPos = event->globalPos()-pos();
    }
    return QDialog::mouseMoveEvent(event);
}

void SplashDialog::mouseReleaseEvent(QMouseEvent* event)
{
    m_moving = false;
}

void SplashDialog::readSettings()
{
    QString organization = "WWB-Qt";
    QString appName = "samp6_5";
    QSettings settings(organization, appName);
    bool saved = settings.value("saved", false).toBool();
    m_user = settings.value("UserName", "user").toString() ;
    QString defaultPswd = encrypt("12345");
    m_pswd = settings.value("Pswd", defaultPswd).toString();
    if(saved) {
        ui->lineEditUserName->setText(m_user);
    }
    ui->checkBox->setChecked(saved);
}

void SplashDialog::writeSettings()
{
    QString organization = "WWB-Qt";
    QString appName = "samp6_5";
    QSettings settings(organization, appName);
    settings.setValue("UserName", m_user);
    settings.setValue("Pswd", m_pswd);
    settings.setValue("saved", ui->checkBox->isChecked());
}

QString SplashDialog::encrypt(const QString& str)
{
    QByteArray btArray;
    btArray.append(str);
    QCryptographicHash hash(QCryptographicHash::Md5);
    hash.addData(btArray);
    QByteArray result = hash.result();
    return result.toHex();;
}



void SplashDialog::on_pushButtonOk_clicked()
{
    QString userName = ui->lineEditUserName->text().trimmed();
    QString pswd = ui->lineEditPassword->text().trimmed();
    QString encrptyPwd = encrypt(pswd);
    if((userName == m_user) && (encrptyPwd == m_pswd)) {
        writeSettings();
        this->accept();
    } else {
        m_tryCount++;
        if(m_tryCount > 3) {
            QMessageBox::critical(this, u8"错误", u8"输入次数过多");
            this->reject();
        } else {
            QMessageBox::warning(this, u8"错误提示", u8"密码错误");
        }
    }
}


void SplashDialog::on_pushButtonCancel_clicked()
{
    this->reject();
}


  • 12
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值