Qt Splash样式的登录窗实现
目录
0 结果
1. 设计界面
2.鼠标拖动界面
3. 密码MD5加密
4. 保用户名和密码到注册表
0 结果
1. 设计界面
继承自QDialog,图片使用QLabel加载,windowFlag样式为 Qt::SplashScreen
TLoginForm::TLoginForm(QWidget *parent)
: QDialog(parent)
, ui(new Ui::TLoginForm)
{
ui->setupUi(this);
this->setAttribute(Qt::WA_DeleteOnClose);
this->setWindowFlag(Qt::SplashScreen);
QApplication::setOrganizationName("CC");
QApplication::setApplicationName("CC_SPLAN");
readSettings();//读取配置
}
2.鼠标拖动界面
涉及到3个事件 mousePressEvent、mouseMoveEvent、mouseReleaseEvent
void TLoginForm::mousePressEvent(QMouseEvent *event)
{
if(event->button()== Qt::LeftButton)
{
m_moving =true;
m_lastPos =event->globalPosition().toPoint() - this->pos();
}
return QDialog::mousePressEvent(event);
}
void TLoginForm::mouseMoveEvent(QMouseEvent *event)
{
QPoint eventPos =event->globalPosition().toPoint();
if(m_moving && (event->buttons()&Qt::LeftButton)
&&(eventPos-m_lastPos).manhattanLength()>QApplication::startDragDistance())
{
move(eventPos-m_lastPos);
m_lastPos = eventPos-this->pos();
}
return QDialog::mouseMoveEvent(event);
}
void TLoginForm::mouseReleaseEvent(QMouseEvent *event)
{
m_moving = false;
event->accept();
}
3. 密码MD5加密
QString TLoginForm::encrypt(const QString &str)
{
QByteArray btArray= str.toLocal8Bit();
QCryptographicHash hash(QCryptographicHash::Md5);
hash.addData(btArray);
QByteArray resultArray = hash.result();
QString md5 = resultArray.toHex();
return md5;
}
4. 保用户名和密码到注册表
void TLoginForm::readSettings()
{
QSettings settings;
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->editUser->setText(m_user);
ui->chkBoxSave->setChecked(saved);
}
void TLoginForm::writeSettings()
{
QSettings settings;
settings.setValue("UserName",m_user);
settings.setValue("PSWD",m_pswd);
settings.setValue("saved",ui->chkBoxSave->isChecked());
}