UserLogin代码

登录按钮

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值