QT 小项目:登录注册账号和忘记暗码(下一章实现长途登录数据库功能) ...

打印 上一主题 下一主题

主题 1058|帖子 1058|积分 3174

一、环境搭建
参考上一章环境
二、项目工程目次

三、主要源步伐如下:
registeraccountwindow.cpp
窗口初始化:
  1. void registeraccountWindow::reginit()
  2. {
  3.     //去掉?号
  4.     this->setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  5.     //更改名称
  6.     this->setWindowTitle("register");
  7.     //更换左上角图标
  8.     this->setWindowIcon(QIcon(":/image/logol.png"));//生成窗口图标
  9.     //禁止改变窗口大小 固定大小
  10.     this->setFixedSize(408,270);
  11.     //设置样式
  12.     ui->lineEdit_registeraccount->setStyleSheet("QLineEdit{border-width:1px;border-radius:4px;font-size:12px;color:black;border:1px solid gray;}"
  13.            "QLineEdit:hover{border-width:1px;border-radius:4px;font-size:16px;color:black;border:1px solid rgb(204,38,200);}");//边框宽度 边框圆角 字体大小 ...   选中边框颜色
  14.     ui->lineEdit_registerpassword->setStyleSheet("QLineEdit{border-width:1px;border-radius:4px;font-size:12px;color:black;border:1px solid gray;}"
  15.            "QLineEdit:hover{border-width:1px;border-radius:4px;font-size:12px;color:black;border:1px solid rgb(70,200,50);}");
  16.     ui->lineEdit_regosterpassword_ok->setStyleSheet("QLineEdit{border-width:1px;border-radius:4px;font-size:12px;color:black;border:1px solid gray;}"
  17.            "QLineEdit:hover{border-width:1px;border-radius:4px;font-size:12px;color:black;border:1px solid rgb(0,18,154);}");
  18.     //设置密码框-密文登录
  19.     ui->lineEdit_registerpassword->setEchoMode(QLineEdit::Password);
  20.     ui->lineEdit_regosterpassword_ok->setEchoMode(QLineEdit::Password);
  21.     //密码的隐藏和显示
  22.     // 设置样式表(图片为眼睛样式)
  23.     ui->checkBox_eye1->setStyleSheet("QCheckBox {spacing: 5px;border: none;background-color: transparent;}"
  24.     "QCheckBox::indicator {width: 20px;height: 20px;border: none;image: url(:/image/close_eye.png);}"
  25.     "QCheckBox::indicator:checked {image: url(:/image/open_eye.png);}");
  26.     ui->checkBox_eye2->setStyleSheet("QCheckBox {spacing: 5px;border: none;background-color: transparent;}"
  27.     "QCheckBox::indicator {width: 20px;height: 20px;border: none;image: url(:/image/close_eye.png);}"
  28.     "QCheckBox::indicator:checked {image: url(:/image/open_eye.png);}");
  29.     //提示信息
  30.     ui->lineEdit_registeraccount->setPlaceholderText("请输入设置的用户名 格式10位以内的数字");
  31.     ui->lineEdit_registerpassword->setPlaceholderText("请输入设置的密码 格式15位以内的数字") ;
  32.     ui->lineEdit_regosterpassword_ok->setPlaceholderText("请再次输入设置的密码 格式15位以内的数字");
  33.     ui->lineEdit_checkcode->setPlaceholderText("请输入验证码");
  34.     //返回主界面按钮样式  背景透明等
  35.     ui->pushButton_back->setStyleSheet("QPushButton {"
  36.                                                   "  background-color: transparent;"
  37.                                                   "  border: none;"
  38.                                                   "  color:rgb(255, 255, 255)"
  39.                                                   "}"
  40.                                                   "QPushButton:hover{color:rgb(15, 23, 253)}"
  41.                                                   "QPushButton:pressed{color:rgb(255, 44, 221)}"
  42.                                                   );
  43.      //验证码按键样式
  44.      ui->pushButton_checkcode->setStyleSheet("QPushButton {"
  45.                                            "  background-color: transparent;"
  46.                                            "  border: none;"
  47.                                            "}"
  48.                                            "QPushButton:pressed { background-color: none; }"  // 移除按键被按下时的视觉效果
  49.                                            "QPushButton:hover { background-color: none; }"   // 移除鼠标悬停时的视觉效果
  50.                                            );
  51.      //获取验证码
  52.      m_captcha = getCaptcha();
  53.      //生成随机颜色
  54.      m_color = generateRandomColor();
  55. }
复制代码
返回登录页面:
  1. void registeraccountWindow::on_pushButton_back_clicked()
  2. {
  3.     this->close();
  4.     lo->show();
  5. }
复制代码
注册按钮及其相关函数:
  1. void registeraccountWindow::on_pushButton_register_clicked()
  2. {
  3.     lo->connent_mysql();
  4.     //获取内容
  5.     QString reg_account = ui->lineEdit_registeraccount->text();
  6.     QString reg_password = ui->lineEdit_registerpassword->text();
  7.     QString reg_password_ok = ui->lineEdit_regosterpassword_ok->text();
  8.     QString check_code = ui->lineEdit_checkcode->text().replace(" "," ");//去除空格
  9.     QSqlQuery query;
  10.     //查询数据库的所有账户 避免重复
  11.     QString qs_temp = QString("select * from os_user where account = '%1'").arg(reg_account);
  12. //    qDebug() << query.next();
  13.     query.exec(qs_temp);
  14.     if(query.next()){          //获取查询结果集
  15.     QMessageBox::information(this,"注册","账号已经被注册");
  16.     }
  17.     else
  18.     {
  19.         if(reg_password_ok==reg_password)  //两次输入的密码一致
  20.         {
  21.             if(check_code.toLower() == m_captcha.toLower())  //用户输入的验证码与生成的验证码比较
  22.             {
  23.                 QString qs = QString("insert into os_user(account,pwd)"
  24.                                       "values(%1, %2)"
  25.                                      ).arg(reg_account).arg(reg_password);
  26.                 if(!query.exec(qs)){          //获取查询结果集
  27.                  QMessageBox::information(this,"注册","注册失败");
  28.                  }
  29.                 else
  30.                  {
  31.                  QMessageBox::information(this,"注册","注册成功");
  32.                  }
  33.             }
  34.             else
  35.             {
  36.                 QMessageBox::warning(this, "Warning", "验证码输入错误");
  37.             }
  38.         }
  39.         else
  40.         {
  41.             QMessageBox::warning(this, "Warning", "两次输入的密码不一致");
  42.         }
  43.     }
  44. }
  45. QColor registeraccountWindow::generateRandomColor()
  46. {
  47.     int red = QRandomGenerator::global()->bounded(256);
  48.     // 生成0到255之间的随机整数作为红色通道的值
  49.     int green = QRandomGenerator::global()->bounded(256);
  50.     // 生成0到255之间的随机整数作为绿色通道的值
  51.     int blue = QRandomGenerator::global()->bounded(256);
  52.     // 生成0到255之间的随机整数作为蓝色通道的值
  53.     return QColor(red, green, blue);
  54.     // 使用生成的RGB值创建并返回一个QColor对象
  55. }
  56. void registeraccountWindow::paintEvent(QPaintEvent *event)
  57. {
  58.        QPainter painter(this);//直接绘制在该窗口上
  59.        // 填充背景为白色
  60.        painter.fillRect(ui->label_checkcode->x()+ui->widget->x(), ui->label_checkcode->y()+ui->widget->y(), ui->label_checkcode->width(), ui->label_checkcode->height(), Qt::white);
  61.        // 设置字体样式
  62.        painter.setFont(QFont("Lucida Console", 18,QFont::Bold));
  63.        // 绘制验证码字符
  64.        for(int i = 0; i < 4; i++)
  65.        {
  66.            QColor color = generateRandomColor();
  67.            // 生成随机颜色
  68.            QPen pen(color);
  69.            pen.setWidth(1);  //画笔宽度
  70.            painter.setPen(pen);//相当于将画笔交给画家
  71.            painter.drawText(ui->label_checkcode->x() +ui->widget->x()+ 30*i, ui->label_checkcode->y()+ui->widget->y(), 30, ui->label_checkcode->height(), Qt::AlignCenter,
  72.                             QString(m_captcha[i]));//1,2,3,4绘制文本的矩形区域 文本的对齐方式 文本内容
  73.                             // 绘制验证码字符
  74.        }
  75.        // 绘制噪点
  76.            for(int i=0; i<1500; i++)
  77.            {
  78.                QColor color = generateRandomColor();
  79.                // 生成随机颜色
  80.                QPen pen(color);
  81.                pen.setWidth(1);
  82.                painter.setPen(pen);
  83.                painter.drawPoint(ui->label_checkcode->x()+ui->widget->x()+ (qrand() % ui->label_checkcode->width()), ui->label_checkcode->y()+ui->widget->y() + (qrand() % ui->label_checkcode->height()));
  84.                //保证随机数的坐标在矩形区域内
  85.            }
  86. //           // 绘制干扰线
  87. //           for(int i = 0;i < 10;++i)
  88. //           {
  89. //               painter.drawLine(ui->label_checkcode->x()+ui->widget->x()+qrand()%ui->label_checkcode->width(),ui->label_checkcode->y()+ui->widget->y()+qrand()%ui->label_checkcode->height(),
  90. //                                ui->label_checkcode->x()+ui->widget->x()+qrand()%ui->label_checkcode->width(),ui->label_checkcode->y()+ui->widget->y()+qrand()%ui->label_checkcode->height());
  91. //           }
  92. }
  93. QString registeraccountWindow::getCaptcha()
  94. {
  95.     const QString possibleCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  96.     const int captchaLength = 4;
  97.     QString result = "";
  98.     // 生成验证码字符串
  99.     for (int i = 0; i < captchaLength; ++i) {   //控制生成4位的字符串
  100.         int index = QRandomGenerator::global()->bounded(possibleCharacters.length());  //QRandomGenerator随机数生成器 QRandomGenerator::global()指针变量 返回创建的随机数指针 bounded(给定随机数生成的范围)
  101.         // 生成一个0到possibleCharacters长度之间的随机整数
  102.         result.append(possibleCharacters.at(index));
  103.         // 将随机位置的字符添加到结果字符串中
  104.     }
  105.     return result; // 返回生成的验证码字符串
  106. }
复制代码
registeraccontwindow.h
  1. #ifndef REGISTERACCOUNTWINDOW_H
  2. #define REGISTERACCOUNTWINDOW_H
  3. #include "login.h"
  4. #include <QDialog>
  5. namespace Ui {
  6. class registeraccountWindow;
  7. }
  8. class registeraccountWindow : public QDialog
  9. {
  10.     Q_OBJECT
  11. public:
  12.     explicit registeraccountWindow(QWidget *parent = nullptr);
  13.     ~registeraccountWindow();
  14.     QString m_captcha;
  15.     QColor m_color;
  16.     login *lo = new login;
  17.     void reginit();
  18.     void paintEvent(QPaintEvent *event);
  19.     QColor generateRandomColor();
  20.     QString getCaptcha();
  21. private slots:
  22.     void on_pushButton_back_clicked();
  23.     void on_pushButton_register_clicked();
  24.     void on_pushButton_checkcode_clicked();
  25.     void on_checkBox_eye1_stateChanged(int arg1);
  26.     void on_checkBox_eye2_stateChanged(int arg1);
  27. private:
  28.     Ui::registeraccountWindow *ui;
  29. };
  30. #endif // REGISTERACCOUNTWINDOW_H
复制代码
forgetpasswordwindow.cpp
修改暗码功能:
  1. void forgetpasswordwindow::on_pushButton_forget_clicked()
  2. {
  3.     lo_forget->connent_mysql();
  4.     //获取内容
  5.     QString forget_account = ui->lineEdit_forgetaccount->text();
  6.     QString forget_password = ui->lineEdit_forgetpassword->text();
  7.     QSqlQuery query;
  8.     //查询数据库的所有账户 是否有该账号
  9.     QString qs = QString("select * from os_user where account = '%1'").arg(forget_account);
  10.     query.exec(qs);//执行SQL语句
  11.     if(query.next()){          //获取查询结果集
  12.         QString qs = QString("UPDATE os_user SET pwd = '%1' WHERE account = '%2'" ).arg(forget_password).arg(forget_account);
  13.         query.exec(qs);//执行SQL语句
  14.         if(!query.exec(qs))
  15.         {
  16.             QMessageBox::information(this,"修改","修改密码失败");
  17.         }
  18.         else
  19.         QMessageBox::information(this,"修改","修改密码成功");
  20.     }
  21.     else
  22.     {
  23.       QMessageBox::information(this,"修改","该账号不存在");
  24.     }
  25. }
复制代码
四、实现结果




注意:点击验证码的位置即可刷新验证码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

自由的羽毛

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表