头文件:
#ifndef QDLGLOGIN_H
#define QDLGLOGIN_H#include <QDialog>namespace Ui {
class dlgLogin;
}class QDlgLogin : public QDialog
{Q_OBJECTprivate: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);public:explicit QDlgLogin(QWidget *parent = 0);~QDlgLogin();private slots:void on_btnOK_clicked(); //OK按键private:Ui::dlgLogin *ui;
};#endif // DLGLOGIN_H
cpp:
#include "qdlglogin.h"
#include "ui_qdlglogin.h"#include <QMouseEvent>
#include <QSettings>
//#include "qwmisc.h"
#include <QMessageBox>
#include <QByteArray>
#include <QCryptographicHash>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());
}QString QDlgLogin::encrypt(const QString &str)
{ //字符串MD5算法加密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; //停止移动
}QDlgLogin::QDlgLogin(QWidget *parent) :QDialog(parent),ui(new Ui::dlgLogin)
{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, "提示", "登录窗口被删除");delete ui;
}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(),关闭对话框}else{m_tryCount++; //错误次数if (m_tryCount>3){QMessageBox::critical(this, "错误", "输入错误次数太多,强行退出");this->reject(); //退出}elseQMessageBox::warning(this, "错误提示", "用户名或密码错误");}
}
用法:
#include "qwmainwindow.h"
#include <QApplication>#include <QPixmap>
#include <QSplashScreen>
#include "qdlgLogin.h"#include <QTextCodec>int main(int argc, char *argv[])
{QTextCodec *codec = QTextCodec::codecForName("UTF-8");QTextCodec::setCodecForLocale(codec); //解决汉字乱码问题QApplication a(argc, argv);QDlgLogin *dlgLogin=new QDlgLogin; //创建对话框if (dlgLogin->exec()==QDialog::Accepted){
// delete dlgLogin;QWMainWindow w;w.show();return a.exec();}elsereturn 0;
}