qt如何做一个记住密码和自动登陆的界面

您所在的位置:网站首页 怎样取消记住密码自动登录 qt如何做一个记住密码和自动登陆的界面

qt如何做一个记住密码和自动登陆的界面

2024-03-26 00:12| 来源: 网络整理| 查看: 265

qt如何做一个记住密码和自动登陆的界面? 首先在做自动登陆和记住密码的功能的时候,我们需要一个介质保存是否自动登陆和是否记住密码的功能,保存的方式一般可以有两种。一种方式是读配置文件;另一种方式是访问数据库信息(适合多人员操作)。而本文采用的则是前一种方式(读配置文件)。 相关知识 qt提供了一个类可以读取配置文件和设置配置文件属性的类QSettings; 例如读取配置文件信息的方式value():

QSettings config("UserConfig.ini", QSettings::IniFormat); QString uerName = config.value("USERCONFIG/UserName", "").toString();

设置配置文件信息的方式setvalue():

QSettings config("UserConfig.ini", QSettings::IniFormat); config.setValue("USERCONFIG/IsRemberPas", 0); 具体实现?

logdialog.h

#ifndef LOGDIALOG_H #define LOGDIALOG_H #include #include "ui_logdialog.h" class LogDialog : public QDialog { Q_OBJECT public slots: void LogMainSlots(); bool AutoLogSlots(int); bool ChangeRemberStateSlots(int); public: LogDialog(QWidget *parent = 0); ~LogDialog(); QString m_NameStr; QString m_PasStr; bool m_isRember; bool m_isAutoLog; bool m_isLog; bool testLog(); private: Ui::LogDialog ui; }; #endif // LOGDIALOG_H

logdialog.cpp

#include "logdialog.h" #include #include LogDialog::LogDialog(QWidget *parent) : QDialog(parent) { ui.setupUi(this); QSettings config("UserConfig.ini", QSettings::IniFormat); QString uerName = config.value("USERCONFIG/UserName", "").toString(); QString passWord = config.value("USERCONFIG/PassWord", "").toString(); int isRember = config.value("USERCONFIG/IsRemberPas", "0").toInt(); int isAuto = config.value("USERCONFIG/IsAutoLog", "0").toInt(); if (!uerName.isEmpty() && !passWord.isEmpty()) { ui.lineEdit_Name->setText(uerName); ui.lineEdit_PasWorld->setText(passWord); m_NameStr = uerName; m_PasStr = passWord; } if (isRember == 1) { ui.checkBox->setChecked(true); m_isRember = true; } else { m_isRember = false; } if (isAuto == 1) { ui.checkBox_2->setChecked(true); m_isAutoLog = true; m_isLog = true; } else { m_isAutoLog = false; m_isLog = false; } connect(ui.pushButtonlog, SIGNAL(clicked()), this, SLOT(LogMainSlots())); connect(ui.checkBox_2, SIGNAL(stateChanged(int)), this, SLOT(AutoLogSlots(int))); connect(ui.checkBox, SIGNAL(stateChanged(int)), this, SLOT(ChangeRemberStateSlots(int))); } LogDialog::~LogDialog() { } bool LogDialog::testLog() { if (m_isAutoLog == false || m_isRember == false) { return false; } if (m_NameStr.isEmpty() || m_PasStr.isEmpty()) { m_NameStr = ui.lineEdit_Name->text(); m_PasStr = ui.lineEdit_PasWorld->text(); if (m_NameStr.isEmpty() || m_PasStr.isEmpty()) { QMessageBox::warning(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("用户名或密码输入错误,请重新输入!")); return false; } } if ((m_NameStr == "admin") && (m_PasStr == "123456")) { this->close(); accept(); } else { QMessageBox::warning(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("用户名或密码输入错误,请重新输入!")); return false; } return true; } void LogDialog::LogMainSlots() { m_NameStr = ui.lineEdit_Name->text(); m_PasStr = ui.lineEdit_PasWorld->text(); if (m_NameStr.isEmpty() || m_PasStr.isEmpty()) { QMessageBox::warning(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("用户名或密码输入错误,请重新输入!")); return; } if ((m_NameStr == "admin") && (m_PasStr == "123456")) { this->close(); accept(); } else { QMessageBox::warning(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("用户名或密码输入错误,请重新输入!")); return; } } bool LogDialog::AutoLogSlots(int state) { if (state != Qt::Checked) { return false; } m_NameStr = ui.lineEdit_Name->text(); m_PasStr = ui.lineEdit_PasWorld->text(); if (m_NameStr.isEmpty() || m_PasStr.isEmpty()) { QMessageBox::warning(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("用户名或密码输入错误,请重新输入!")); return false; } if ((m_NameStr == "admin") && (m_PasStr == "123456")) { this->close(); accept(); } else { QMessageBox::warning(this, QString::fromLocal8Bit("错误"), QString::fromLocal8Bit("用户名或密码输入错误,请重新输入!")); return false; } return true; } bool LogDialog::ChangeRemberStateSlots(int state) { if (state != Qt::Checked) { m_isRember = false; QSettings config("UserConfig.ini", QSettings::IniFormat); config.setValue("USERCONFIG/IsRemberPas", 0); } return true; }

登陆界面: 在这里插入图片描述

testlog.h

#ifndef TESTLOG_H #define TESTLOG_H #include #include "ui_testlog.h" class TestLog : public QWidget { Q_OBJECT public: TestLog(QWidget *parent = 0); ~TestLog(); private: Ui::TestLogClass ui; }; #endif // TESTLOG_H

testlog.cpp

#include "testlog.h" TestLog::TestLog(QWidget *parent) : QWidget(parent) { ui.setupUi(this); } TestLog::~TestLog() { }

TestLog.ui

在这里插入图片描述

main.cpp

#include "testlog.h" #include #include "logdialog.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); TestLog w; LogDialog log; log.show(); if (log.m_isLog && log.testLog() == true) { w.show(); return a.exec(); } if (log.exec() == QDialog::Accepted) { w.show(); return a.exec(); } else { return 0; } }

当登陆信息输入通过后将弹出TestLog界面。配置文件的具体信息如下: [USERCONFIG] UserName=admin PassWord=123456 IsAutoLog=0 IsRemberPas=0 至此一个记住密码和自动登陆界面已经做好了。

注意:登陆界面和主界面的相互依赖关系,和建立顺序关系。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3