一般的大型应用程序在启动时会显示一个启动画面,即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_OBJECTpublic: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);//设置为SplashScreenthis->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();
}