Qt Splash 与登录窗口
开发工具:VS2013 + QT5.8
1、实例功能概述:
2、对话框界面设计与类定义
(1)QDlgLogin.h 头文件中QDlgLogin 类的定义:
QDlgLogin.h 头文件:
#pragma once#include <QDialog>
namespace Ui { class QDlgLogin; };class QDlgLogin : public QDialog
{Q_OBJECTpublic:QDlgLogin(QWidget *parent = Q_NULLPTR);~QDlgLogin();private:Ui::QDlgLogin *ui;private: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);//字符串加密protected://用于鼠标拖动窗口的鼠标事件操作void mousePressEvent(QMouseEvent *event);void mouseMoveEvent(QMouseEvent *event);void mouseReleaseEvent(QMouseEvent *event);private slots:void on_btnOK_clicked(); //OK按键void on_btnCancel_clicked(); //取消按键};
(2)QDlgLogin 类功能实现:
//读取存储的用户名和密码, 密码是经过加密的
void QDlgLogin::readSettings()
{QString organization = "WWB-Qt";//用于注册表,QString appName = "samp6_5";// HKEY_CURRENT_USER/Software/WWB-Qt/amp6_5QSettings settings(organization, appName);//创建bool saved = settings.value("saved", false).toBool();//读取 saved键的值m_user = settings.value("Username", "user").toString();//读取 Username 键的值,缺省为“user”QString defaultPSWD = encrypt("12345"); //缺省密码"12345"加密后的数据m_pswd = settings.value("PSWD", defaultPSWD).toString();//读取PSWD 键的值,if (saved)ui->editUser->setText(m_user);ui->chkBoxSave->setChecked(saved);
}//保存用户名,密码等设置
void QDlgLogin::writeSettings()
{QSettings settings("WWB-Qt", "samp6_5"); //注册表键组settings.setValue("Username", m_user); //用户名settings.setValue("PSWD", m_pswd); //密码,经过加密的settings.setValue("saved", ui->chkBoxSave->isChecked());
}
(3)字符串加密
//字符串MD5算法加密
QString QDlgLogin::encrypt(const QString &str)
{ QByteArray btArray;btArray.append(str);//加入原始字符串QCryptographicHash hash(QCryptographicHash::Md5);//Md5加密算法hash.addData(btArray);//添加数据到加密哈希值QByteArray resultArray = hash.result();//返回最终的哈希值QString md5 = resultArray.toHex();//转换为16进制字符串return md5;
}
(4)用户名和密码输入判断
在登录界面,输入用户名和密码后,单击“确定”按钮,程序会对输入的内容进行判断:
//"确定"按钮相应
void QDlgLogin::on_btnOK_clicked()
{QString user = ui->editUser->text().trimmed();//输入用户名QString pswd = ui->editPSWD->text().trimmed(); //输入密码QString encrptPSWD = encrypt(pswd); //对输入密码进行加密if ((user == m_user) && (encrptPSWD == m_pswd)) //如果用户名和密码正确{writeSettings();//保存设置this->accept(); //对话框 accept(),关闭对话框QMessageBox::information(this, "提示", "用户名和密码正确");}else{m_tryCount++; //错误次数if (m_tryCount > 3){QMessageBox::critical(this, "错误", "输入错误次数太多,强行退出");this->reject(); //退出}else{QMessageBox::warning(this, "错误提示", "用户名或密码错误");}}
}//"取消"按钮相应
void QDlgLogin::on_btnCancel_clicked()
{this->reject();//退出
}
3、窗口拖动功能的实现
//鼠标按键被按下
void QDlgLogin::mousePressEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton){m_moving = true;//记录下鼠标相对于窗口的位置//event->globalPos()鼠标按下时,鼠标相对于整个屏幕位置//pos() this->pos()鼠标按下时,窗口相对于整个屏幕位置m_lastPos = event->globalPos() - pos();}return QDialog::mousePressEvent(event); //
}//鼠标按下左键移动
void QDlgLogin::mouseMoveEvent(QMouseEvent *event)
{//(event->buttons() && Qt::LeftButton)按下是左键//鼠标移动事件需要移动窗口,窗口移动到哪里呢?就是要获取鼠标移动中,窗口在整个屏幕的坐标,然后move到这个坐标,怎么获取坐标?//通过事件event->globalPos()知道鼠标坐标,鼠标坐标减去鼠标相对于窗口位置,就是窗口在整个屏幕的坐标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 QDlgLogin::mouseReleaseEvent(QMouseEvent *event)
{Q_UNUSED(event);m_moving = false; //停止移动
}
QDlgLogin.cpp 文件的完整代码如下:
#include "QDlgLogin.h"
#include "ui_QDlgLogin.h"#include <QMouseEvent>
#include <QSettings>
//#include "qwmisc.h"
#include <QMessageBox>
#include <QByteArray>
#include <QCryptographicHash>#include "smaple6_5QDlgLogin.h"//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")QDlgLogin::QDlgLogin(QWidget *parent): QDialog(parent)
{ui = new Ui::QDlgLogin();ui->setupUi(this);ui->editPSWD->setEchoMode(QLineEdit::Password); //密码输入编辑框设置为密码输入模式this->setAttribute(Qt::WA_DeleteOnClose);//设置为关闭时删除this->setWindowFlags(Qt::SplashScreen); //设置为SplashScreen, 窗口无边框,不在任务栏显示//this->setWindowFlags(Qt::FramelessWindowHint);//无边框,但是在任务显示对话框标题readSettings(); //读取存储的用户名和密码
}QDlgLogin::~QDlgLogin()
{QMessageBox::information(this, "提示", "QDlgLogin-登录窗口被删除");delete ui;
}//读取存储的用户名和密码, 密码是经过加密的
void QDlgLogin::readSettings()
{QString organization = "WWB-Qt";//用于注册表,QString appName = "samp6_5";// HKEY_CURRENT_USER/Software/WWB-Qt/amp6_5QSettings settings(organization, appName);//创建bool saved = settings.value("saved", false).toBool();//读取 saved键的值m_user = settings.value("Username", "user").toString();//读取 Username 键的值,缺省为“user”QString defaultPSWD = encrypt("12345"); //缺省密码"12345"加密后的数据m_pswd = settings.value("PSWD", defaultPSWD).toString();//读取PSWD 键的值,if (saved)ui->editUser->setText(m_user);ui->chkBoxSave->setChecked(saved);
}//保存用户名,密码等设置
void QDlgLogin::writeSettings()
{QSettings settings("WWB-Qt", "samp6_5"); //注册表键组settings.setValue("Username", m_user); //用户名settings.setValue("PSWD", m_pswd); //密码,经过加密的settings.setValue("saved", ui->chkBoxSave->isChecked());
}//字符串MD5算法加密
QString QDlgLogin::encrypt(const QString &str)
{ QByteArray btArray;btArray.append(str);//加入原始字符串QCryptographicHash hash(QCryptographicHash::Md5);//Md5加密算法hash.addData(btArray);//添加数据到加密哈希值QByteArray resultArray = hash.result();//返回最终的哈希值QString md5 = resultArray.toHex();//转换为16进制字符串return md5;
}//鼠标按键被按下
void QDlgLogin::mousePressEvent(QMouseEvent *event)
{if (event->button() == Qt::LeftButton){m_moving = true;//记录下鼠标相对于窗口的位置//event->globalPos()鼠标按下时,鼠标相对于整个屏幕位置//pos() this->pos()鼠标按下时,窗口相对于整个屏幕位置m_lastPos = event->globalPos() - pos();}return QDialog::mousePressEvent(event); //
}//鼠标按下左键移动
void QDlgLogin::mouseMoveEvent(QMouseEvent *event)
{//(event->buttons() && Qt::LeftButton)按下是左键//鼠标移动事件需要移动窗口,窗口移动到哪里呢?就是要获取鼠标移动中,窗口在整个屏幕的坐标,然后move到这个坐标,怎么获取坐标?//通过事件event->globalPos()知道鼠标坐标,鼠标坐标减去鼠标相对于窗口位置,就是窗口在整个屏幕的坐标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 QDlgLogin::mouseReleaseEvent(QMouseEvent *event)
{Q_UNUSED(event);m_moving = false; //停止移动
}//"确定"按钮相应
void QDlgLogin::on_btnOK_clicked()
{QString user = ui->editUser->text().trimmed();//输入用户名QString pswd = ui->editPSWD->text().trimmed(); //输入密码QString encrptPSWD = encrypt(pswd); //对输入密码进行加密if ((user == m_user) && (encrptPSWD == m_pswd)) //如果用户名和密码正确{writeSettings();//保存设置this->accept(); //对话框 accept(),关闭对话框QMessageBox::information(this, "提示", "用户名和密码正确");}else{m_tryCount++; //错误次数if (m_tryCount > 3){QMessageBox::critical(this, "错误", "输入错误次数太多,强行退出");this->reject(); //退出}else{QMessageBox::warning(this, "错误提示", "用户名或密码错误");}}
}//"取消"按钮相应
void QDlgLogin::on_btnCancel_clicked()
{this->reject();//退出
}
4、多文档界面设计
多文档界面如下:
smaple6_5QDlgLogin.h 文件
#pragma once#include <QtWidgets/QMainWindow>
#include "ui_smaple6_5QDlgLogin.h"#include "qformdoc.h"class smaple6_5QDlgLogin : public QMainWindow
{Q_OBJECTpublic:smaple6_5QDlgLogin(QWidget *parent = Q_NULLPTR);private:Ui::smaple6_5QDlgLoginClass ui;private slots:void on_tabWidget_tabCloseRequested(int index); //tab页关闭时void on_actDoc_Open_triggered();//打开文件void on_actDoc_New_triggered();//新建文件void on_tabWidget_currentChanged(int index); //tabWidget当前页切换void on_actCut_triggered(); //cutvoid on_actFont_triggered();//字体设置void on_actCopy_triggered(); //copyvoid on_actPaste_triggered(); //pastevoid on_actCloseALL_triggered(); //关闭全部void on_actQuit_triggered(); //actQuitpublic slots:void on_fileOpended(QString& aFileName); //主窗口打开文件后触发private:void paintEvent(QPaintEvent *event);//绘制背景};
smaple6_5QDlgLogin.cpp 文件
#include "smaple6_5QDlgLogin.h"#include <QPainter>
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")smaple6_5QDlgLogin::smaple6_5QDlgLogin(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);//ui.tabWidget->setVisible(false); //tabWidget初始设置为不可见ui.tabWidget->setDocumentMode(true); //设置为文档模式ui.tabWidget->setTabsClosable(true);//设置为单页可关闭this->setCentralWidget(ui.tabWidget); //tabWidget填充工作区this->setWindowState(Qt::WindowMaximized); //窗口最大化显示
}//绘制背景图片
void smaple6_5QDlgLogin::paintEvent(QPaintEvent *event)
{Q_UNUSED(event);QPainter painter(this);painter.drawPixmap(0, ui.mainToolBar->height(), this->width(),this->height() - ui.mainToolBar->height() - ui.statusBar->height(),QPixmap("./smaple6_5QDlgLogin/images/back.jpg"));
}//关闭Tab
void smaple6_5QDlgLogin::on_tabWidget_tabCloseRequested(int index)
{QFormDoc *formDoc;formDoc = static_cast<QFormDoc*>(ui.tabWidget->widget(index));formDoc->close(); //子窗口关闭时自动释放ui.tabWidget->removeTab(index);bool en = ui.tabWidget->count() > 0; //再无页面时,actions禁用ui.actCut->setEnabled(en);ui.actCopy->setEnabled(en);ui.actPaste->setEnabled(en);ui.actFont->setEnabled(en);if (!en){ui.statusBar->clearMessage();}
}//打开一个文档
void smaple6_5QDlgLogin::on_actDoc_Open_triggered()
{//curPath=QCoreApplication::applicationDirPath(); //获取应用程序的路径QString curPath = QDir::currentPath();QString aFileName = QFileDialog::getOpenFileName(this, tr("打开一个文件"), curPath,"C程序文件文件(*.h *cpp);;文本文件(*.txt);;所有文件(*.*)");if (aFileName.isEmpty()){return; //如果未选择文件,退出}QFormDoc *formDoc;int cur;if (ui.tabWidget->count() == 0) //一个页面都没有{formDoc = new QFormDoc();//指定父窗口,必须在父窗口为Widget窗口提供一个显示区域//formDoc->setAttribute(Qt::WA_DeleteOnClose); //关闭时自动删除cur = ui.tabWidget->addTab(formDoc, QString::asprintf("Doc %d", ui.tabWidget->count()));connect(formDoc, SIGNAL(fileOpended(QString&)), this, SLOT(on_fileOpended(QString&)));}else //在当前Tab文档窗口里打开{cur = ui.tabWidget->currentIndex();formDoc = (QFormDoc*)ui.tabWidget->widget(cur);}formDoc->loadFromFile(aFileName); //调用文档窗口的函数打开文件//ui.tabWidget->setVisible(true);ui.actCut->setEnabled(true);ui.actCopy->setEnabled(true);ui.actPaste->setEnabled(true);ui.actFont->setEnabled(true);
}void smaple6_5QDlgLogin::on_actDoc_New_triggered()
{ //新建空文档QFormDoc *formDoc = new QFormDoc(); //不指定父窗口,可以单独用show()方法显示// formDoc->setAttribute(Qt::WA_DeleteOnClose); //关闭时自动删除//tabWidget新增一个页面int cur = ui.tabWidget->addTab(formDoc, QString::asprintf("Doc %d", ui.tabWidget->count()));ui.tabWidget->setCurrentIndex(cur);//文件打开信号与槽关联connect(formDoc, SIGNAL(fileOpended(QString&)), this, SLOT(on_fileOpended(QString&)));ui.tabWidget->setVisible(true);ui.actCut->setEnabled(true);ui.actCopy->setEnabled(true);ui.actPaste->setEnabled(true);ui.actFont->setEnabled(true);
}//Tab当前页面变化,更新状态显示的文件名
void smaple6_5QDlgLogin::on_tabWidget_currentChanged(int index)
{if (ui.tabWidget->count() > 0){QFormDoc *formDoc;formDoc = (QFormDoc*)ui.tabWidget->widget(index);ui.statusBar->showMessage(formDoc->currentFileName());}
}//文件打开槽函数,子窗口打开文件后触发
void smaple6_5QDlgLogin::on_fileOpended(QString &aFileName)
{ui.statusBar->showMessage(aFileName); //状态栏上显示文件名QFileInfo fileInfo(aFileName); //文件信息QString shortname = fileInfo.fileName(); //去掉路径的文件名ui.tabWidget->setTabText(ui.tabWidget->currentIndex(), shortname); //显示为tab标签
}//调用当前tab 页的QFormDoc的相应功能函数,cut
void smaple6_5QDlgLogin::on_actCut_triggered()
{QFormDoc *formDoc;formDoc = (QFormDoc*)ui.tabWidget->widget(ui.tabWidget->currentIndex());formDoc->textCut();
}//调用当前tab 页的QFormDoc的相应功能函数, 字体设置
void smaple6_5QDlgLogin::on_actFont_triggered()
{QFormDoc *formDoc;formDoc = (QFormDoc*)ui.tabWidget->widget(ui.tabWidget->currentIndex());formDoc->setEditFont();
}//调用当前tab 页的QFormDoc的相应功能函数,copy
void smaple6_5QDlgLogin::on_actCopy_triggered()
{QFormDoc *formDoc;formDoc = (QFormDoc*)ui.tabWidget->widget(ui.tabWidget->currentIndex());formDoc->textCopy();
}//调用当前tab 页的QFormDoc的相应功能函数, paste
void smaple6_5QDlgLogin::on_actPaste_triggered()
{QFormDoc *formDoc;formDoc = (QFormDoc*)ui.tabWidget->widget(ui.tabWidget->currentIndex());formDoc->textPaste();
}//关闭所有窗口
void smaple6_5QDlgLogin::on_actCloseALL_triggered()
{QFormDoc *formDoc;for (int i = 0; i < ui.tabWidget->count(); i++){formDoc = static_cast<QFormDoc*>(ui.tabWidget->widget(i));formDoc->close(); //子窗口关闭时自动释放}ui.tabWidget->clear(); //清除所有tabui.statusBar->clearMessage(); //清除状态栏消息
}//窗口关闭时询问是否退出
void smaple6_5QDlgLogin::on_actQuit_triggered()
{QMessageBox::information(this, "提示", "退出");QMessageBox::StandardButton result = QMessageBox::question(this, "确认", "确定要退出本程序吗?",QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::No);if (result == QMessageBox::Yes){this->close();//}else{}//this->close();
}
5、QFormDoc 界面
QFormDoc.h 文件
#pragma once#include <QWidget>namespace Ui { class QFormDoc; };class QFormDoc : public QWidget
{Q_OBJECTpublic:QFormDoc(QWidget *parent = Q_NULLPTR);~QFormDoc();private:QString mCurrentFile;//当前文件bool mFileOpened = false;//文件已打开public:void loadFromFile(QString& aFileName);//打开文件QString currentFileName();//返回当前文件名bool isFileOpened();//文件已经打开void setEditFont();//设置字体void textCut();void textCopy();void textPaste();private:Ui::QFormDoc *ui;
};
QFormDoc.cpp 文件
#include "QFormDoc.h"
#include "ui_QFormDoc.h"#include <QMessageBox>
#include <QFile>
#include <QTextstream>
#include <QFileInfo>
#include <QFontDialog>
#include <QVBoxLayout>//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")QFormDoc::QFormDoc(QWidget *parent): QWidget(parent)
{ui = new Ui::QFormDoc();ui->setupUi(this);this->setWindowTitle("New Doc");//窗口标题this->setAttribute(Qt::WA_DeleteOnClose);//关闭时自动删除
}QFormDoc::~QFormDoc()
{QMessageBox::information(this, "信息", "Doc文档窗口被释放");delete ui;
}//打开文件
void QFormDoc::loadFromFile(QString &aFileName)
{QFile aFile(aFileName);if (aFile.open(QIODevice::ReadOnly | QIODevice::Text)){QTextStream aStream(&aFile);//用文本流读取文件ui->plainTextEdit->clear();ui->plainTextEdit->setPlainText(aStream.readAll());//读取文本文件aFile.close();//关闭文件mCurrentFile = aFileName;//保存当前文件名QFileInfo fileInfo(aFileName);//文件信息QString str = fileInfo.fileName();//去除路径后的文件名this->setWindowTitle(str);mFileOpened = true;}
}QString QFormDoc::currentFileName()
{return mCurrentFile;
}
//文件是否已打开
bool QFormDoc::isFileOpened()
{return mFileOpened;
}void QFormDoc::setEditFont()
{QFont font;font = ui->plainTextEdit->font();bool ok;font = QFontDialog::getFont(&ok, font);ui->plainTextEdit->setFont(font);
}void QFormDoc::textCut()
{ui->plainTextEdit->cut();
}void QFormDoc::textCopy()
{ui->plainTextEdit->copy();
}void QFormDoc::textPaste()
{ui->plainTextEdit->paste();
}
完成代码,运行后界面,如下图:
登录后界面: