文章目录
数据库信息相关代码.hmainwindow.hmyinfor.hyiban.h.cppmain.cppmainwindow.cpp(此模块是重点)myinfor.cppyiban.cppuimainwindow.uimyinfor.uiyiban.ui效果展示验证码展示一般用户登陆界面超级用户其他注意事项(重点)~~其他内容待扩展~~数据库信息
相关代码
.h
mainwindow.h
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QMainWindow>#include <QTimer>QT_BEGIN_NAMESPACEnamespace Ui {class MainWindow; }QT_END_NAMESPACEclass MainWindow : public QMainWindow{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();bool zhanghucunzai(QString zhanghu);void check(QString linezhanghao,QString linemima);QString yanzheng();bool yanzhengmayanzheng();void xiawan();QString yzm ;QTimer *yan = new QTimer(this);private:Ui::MainWindow *ui;};#endif // MAINWINDOW_H
myinfor.h
#ifndef MYINFOR_H#define MYINFOR_H#include <QWidget>namespace Ui {class myinfor;}class myinfor : public QWidget{Q_OBJECTpublic:explicit myinfor(QWidget *parent = nullptr);~myinfor();signals:void fanhui();private:Ui::myinfor *ui;};#endif // MYINFOR_H
yiban.h
#ifndef YIBAN_H#define YIBAN_H#include <QWidget>#include <QUdpSocket>namespace Ui {class yiban;}class yiban : public QWidget{Q_OBJECTpublic:explicit yiban(QWidget *parent ,QString zhanghao);//都不能带参数// yiban(QString zhanghao);~yiban();private:Ui::yiban *ui;};#endif // YIBAN_H
.cpp
main.cpp
#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);MainWindow w;w.show();return a.exec();}
mainwindow.cpp(此模块是重点)
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QtSql/QSqlDatabase>#include <QtSql/QSqlError>#include <QSqlQuery>//对sql语句操作#include <QMessageBox>#include <QDebug>#include <QVariantList>//#include <QCryptographicHash>//加密类#include <QTime>//随机数种子#include <QTimer>#include "myinfor.h"#include "yiban.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow){ui->setupUi(this);setFixedSize(320,520);//打印QT支持的数据库//qDebug()<<QSqlDatabase::drivers();QSqlDatabase sjk=QSqlDatabase::addDatabase("QMYSQL");sjk.setHostName("127.0.0.1");//sjk.setPort(3306);sjk.setUserName("root");sjk.setPassword("123456");sjk.setDatabaseName("mimabiao");if(!sjk.open())QMessageBox::warning(this,"警告",sjk.lastError().text());elseqDebug()<<"成功";//设置定时器,目的是在验证码输入错误后出发更新验证码 [34-43]yan->setInterval(100);yan->start();connect(yan,&QTimer::timeout,[=](){//验证功能ui->yanzhengma->setText(yanzheng());yzm = QString("%1").arg(ui->yanzhengma->text());yan->stop();});//瞎玩按钮connect(ui->pushButton_xiawan,&QPushButton::clicked,this,&MainWindow::xiawan);// //验证功能// ui->yanzhengma->setText(yanzheng());// yzm = QString("%1").arg(ui->yanzhengma->text());//注册账号connect(ui->pushButton_zhuce,&QPushButton::clicked,[=](){//这里是验证码验证模块if(!yanzhengmayanzheng()){QMessageBox::warning(this,"警告","验证码错误");return ;}QString linezhanghao = ui->lineEditzhanghao->text();QString linemima = ui->lineEdit_mima->text();if(linemima == NULL||linezhanghao==NULL){QMessageBox::warning(this,"警告","账户或密码输入为空");return;}// //加密密码方法MD5【暂不学习】// QCryptographicHash hash(QCryptographicHash::Md5);qDebug()<<linezhanghao;//不能这样找//QString caozuo = QString("select count(yonghu_zhanghao) from xinxi where yonghu_zhanghao = '%1'").arg(linezhanghao);QSqlQuery qq;// //插入(自定义占位符?)// qq.exec("insert into xinxi(yonghu_zhanghao,yonghu_mima) values (?,?)");// //给字段绑定相应的值// QVariantList zh;// zh<<linezhanghao;// QVariantList mm;// mm<<linemima;// //按顺序绑定【一个一个按顺序绑定】// qq.addBindValue(zh);// qq.addBindValue(mm);// //执行预处理命令// qq.execBatch();QString caozuo =QString("insert into xinxi(yonghu_zhanghu,yonghu_mima)values('%1','%2')").arg(linezhanghao).arg(linemima);//上面用了查找账户数量的方法判断是否重名在MYSQL中可行但由于QT中MYSQL只返回该语句是否执行成功,至于执行的查询数量等结果不反馈所以此处用主键的方式来设置账号是恰当的if(!qq.exec(caozuo) ){QMessageBox::warning(this,"警告","该账号存在");return ;}else{QMessageBox::information(this,"通知","注册成功\n");}});//登录账号connect(ui->pushButton_denglu,&QPushButton::clicked,[=](){//这里是验证码验证模块if(!yanzhengmayanzheng()){QMessageBox::warning(this,"警告","验证码错误");return ;}QSqlQuery qq;QString linezhanghao = ui->lineEditzhanghao->text();QString linemima = ui->lineEdit_mima->text();check(linezhanghao,linemima);QString caozuo =QString("select * from xinxi where yonghu_zhanghu='%1'and yonghu_mima='%2'").arg(linezhanghao).arg(linemima);// if(qq.exec(caozuo))//不能这样写,因为只要语句正确返回的一定是true// QMessageBox::information(this,"提示","登陆成功");// else// QMessageBox::warning(this,"警告","密码错误");if(!qq.exec(caozuo) )QMessageBox::warning(this,"警告","密码错误");if(qq.next()){//QMessageBox::information(this,"提示","登陆成功");if(linezhanghao == "1921084135"){myinfor *my = new myinfor;this->hide();my->show();//用户点返回进入登陆界面connect(my,&myinfor::fanhui,[=](){my->close();this->show();});}else{// QMessageBox::information(this,"提示","暂不支持非超级帐号登录");yiban *xuesheng= new yiban(0,linezhanghao);xuesheng->show();}}elseQMessageBox::warning(this,"警告","密码错误");});//修改密码connect(ui->pushButton_wangjimima,&QPushButton::clicked,[=](){//这里是验证码验证模块if(!yanzhengmayanzheng()){QMessageBox::warning(this,"警告","验证码错误");return ;}QSqlQuery qq;QString linezhanghao = ui->lineEditzhanghao->text();QString linemima = ui->lineEdit_mima->text();check(linezhanghao,linemima);QString caozuo =QString("update xinxi set yonghu_mima = '%1'where yonghu_zhanghu = '%2'").arg(linemima).arg(linezhanghao);if(qq.exec(caozuo))QMessageBox::information(this,"通知","修改密码完成:"+linemima);elseQMessageBox::warning(this,"警告","密码修改失败:请检查用户名是否正确");});}MainWindow::~MainWindow(){delete ui;}bool MainWindow::zhanghucunzai(QString zhanghu){QSqlQuery qq;QString caozuo = QString("select * from xinxi where yonghu_zhanghu = '%1'").arg(zhanghu);qq.exec(caozuo);if(qq.next())return true;return false;}void MainWindow::check(QString linezhanghao,QString linemima){if(linemima == NULL||linezhanghao==NULL){QMessageBox::warning(this,"警告","账户或密码输入为空");return;}if(!zhanghucunzai(linezhanghao)){QMessageBox::warning(this,"警告","账户不存在");return ;}}//生成验证码QString MainWindow::yanzheng(){a:int ret=0;for(int i=0 ; i<4;i++){//qsrand(QTime::currentTime().second()*1000);ret = ret*10+qrand()%10;}if(ret<1000)goto a;return QString::number( ret);}bool MainWindow::yanzhengmayanzheng(){//验证码验证if(yzm != ui->inputyanzhengma->text()){yanzheng();yan->start();//启动定时器(不必让定时器一直工作,只需让其更改验证码时工作即可)return 0;}return 1;}void MainWindow::xiawan(){QSqlQuery qq;QString caozuo = QString("drop table if exists new_table");qq.exec(caozuo);caozuo = QString("create table new_table(age int , name varchar(10))");qq.exec(caozuo);caozuo = QString("insert into new_table values('%1' , '%2')").arg(10).arg("张三");qq.exec(caozuo);}
myinfor.cpp
#include "myinfor.h"#include "ui_myinfor.h"#include <QStandardItemModel>myinfor::myinfor(QWidget *parent) :QWidget(parent),ui(new Ui::myinfor){//main中采用的是sql语句修改数据库//myinfor中采用数据模型修改数据库ui->setupUi(this);//初始化模型// QStandardItemModel model = new QStandardItemModel(this);// model->setHorizontalHeaderItem(0,new,QStandardItem("id"));// model->setHorizontalHeaderItem(1,new,QStandardItem("name"));// model->setHorizontalHeaderItem(2,new,QStandardItem("age"));connect(ui->pushButton,&QPushButton::clicked,[=](){emit this->fanhui();});}myinfor::~myinfor(){delete ui;}
yiban.cpp
#include "yiban.h"#include "ui_yiban.h"yiban::yiban(QWidget *parent,QString zhanghao) :QWidget(parent),ui(new Ui::yiban){ui->setupUi(this);setWindowTitle(zhanghao);}//yiban::yiban(QString zhanghao)//{// ui->setupUi(this);// setWindowTitle(zhanghao);// QString myport=zhanghao,toport,toip="127.0.0.1";//}yiban::~yiban(){delete ui;}
ui
mainwindow.ui
myinfor.ui
yiban.ui
效果展示
验证码展示
一般用户登陆界面
超级用户
其他
注意事项(重点)
不能用qq.exec(caozuo)判断,因为只要代码正确返回值就是true
所以再用一个qq.next();
QSqlQuery qq;QString caozuo = QString("select * from xinxi where yonghu_zhanghu = '%1'").arg(zhanghu);qq.exec(caozuo);if(qq.next())return true;return false; }