登录按钮
bool isAccountLogin;
是否登录
登录成功,关闭当前登录页面
进入主程序界面 携带账号,登录状态信息
void UserLogin::onLoginBtnClicked()
{
bool isAccountLogin;
QString strAccount;//账号或者QQ号
if (!veryfyAccountCode(isAccountLogin,strAccount))
{
QMessageBox::information(NULL, QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("您输入的账号或密码有误,请重新输入!"));
//ui.editPassword->setText("");
//ui.editPassword->setText("");
return;
}
close();
CCMainWindow* mainwindow = new CCMainWindow(strAccount,isAccountLogin);
mainwindow->show();
}
处理登录信息
员工编号 或者 QQ账号登录
bool UserLogin::veryfyAccountCode(bool& isAccountLogin, QString& strAccount)
{
//获取登录用户名 和 密码
QString strAccountInput = ui.editUserAccount->text();
QString strtCodeInput = ui.editPassword->text();
//输入员工号(QQ号登录)
QString strSqlCode = QString("SELECT code FROM tab_accounts WHERE employeeID = '%1'").arg(strAccountInput);
QSqlQuery queryEmployeeID(strSqlCode);
queryEmployeeID.exec();
if (queryEmployeeID.first())//指向结果集第一条
{
//数据库中qq号对应的密码
QString strCode = queryEmployeeID.value(0).toString();
//数据库中的密码 和 用户输入的是否对应
if (strCode == strtCodeInput)
{ //记录登录的账号
gLoginEmployeeID = strAccountInput;
//登录状态
isAccountLogin = false;
//记录登录账号
strAccount = strAccountInput;
return true;
}
else
{
return false;
}
}
//账号登录
strSqlCode = QString("SELECT code,employeeID FROM tab_accounts WHERE account = '%1'")
.arg(strAccountInput);
QSqlQuery queryAccount(strSqlCode);
queryAccount.exec();
if (queryAccount.first())
{
QString strCode = queryAccount.value(0).toString();
if (strCode == strtCodeInput)
{
gLoginEmployeeID = queryAccount.value(1).toString();
strAccount = strAccountInput;
isAccountLogin = true;
return true;
}
else
{
return false;
}
}
return false;
}
登录类
初始化设窗体
设置属性
初始化标题栏
设置标题栏标题
加载UserLogin
样式
初始化UserLogin
#include "UserLogin.h"
#include "CCMainWindow.h"
#include<QMessageBox>
#include<QSqlDatabase>
#include<QSqlQuery>
QString gLoginEmployeeID;//登陆者QQ号(员工号)
UserLogin::UserLogin(QWidget *parent)
: BasicWindow(parent)
{
ui.setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
initTitleBar();
setTitleBarTitle("", ":/Resources/MainWindow/qqlogoclassic.png");
loadStyleSheet("UserLogin");
initControl();
}
UserLogin
类初始化
设置登录页面的头像
连接数据库
void UserLogin::initControl()
{
QLabel *headlabel = new QLabel(this);
headlabel->setFixedSize(68, 68);
QPixmap pix(":/Resources/MainWindow/head_mask.png");
headlabel->setPixmap(getRoundImage(QPixmap(":/Resources/MainWindow/app/logo.ico"), pix, headlabel->size()));
headlabel->move(width()/2 - 34,ui.titleWidget->height()- 34);
connect(ui.loginBtn,&QPushButton::clicked,this,&UserLogin::onLoginBtnClicked);
if(!connectMySql())
{
QMessageBox::information(NULL,QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("连接数据库失败"));
close();
}
}
连接数据库
bool UserLogin::connectMySql()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setDatabaseName("qtqq");//数据库名称
db.setHostName("localhost");//主机名
db.setUserName("root");//用户名
db.setPassword("123456");//密码
db.setPort(3306);//端口
if (db.open())
{
return true;
}
else
{
return false;
}
return false;
}