2021-05-07

这学期C++实训,老师让用Qt写一个简易的的汽车销售系统,老师说写到可以修改密码就可以。(因为涉及到了登录界面以及sqlite,所以就想着能不能实现简单的用户注册功能<具有验证方式更好>。【现在注册功能还没有实现,想着可以赶着收作业之前实现,希望会的大佬可以指点指点】)

先看一下运行结果:

登录界面:

2021-05-07

点击登录按钮后:

2021-05-07

修改密码界面:(遗憾的是没有验证方式)

2021-05-07


 

 

因为代码比较多,下面仅列出有关注册功能的窗口程序:

1、厂家表、品牌表、品牌表

static bool createConnection()
{
    //在本进程空间中创建一个QSLite数据库
   QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");

   db.setHostName("beixin-001");                         //数据库服务器IP,设置数据库主机名
   db.setDatabaseName("data.db");                      //设置数据库名
   db.setUserName("lihaiyang");                           //用户名
   db.setPassword("123456");                               //密码
   if(!db.open())
   {
       return false;
   }
//创建查询对象
QSqlQuery query;



//创建厂家表
query.exec("create table factory(id varchar primary key,name varchar)");                       //主关键字(primary key)是表中的一个或多个字段,它的值用于唯一的标识表中的某一条记录。在两个表的关系中,主关键字用来在一个表中引用来自于另一个表中的特定记录。主关键字是一种唯一关键字,表定义的一部分。一个表的主键可以由多个关键字共同组成,并且主关键字的列不能包含空值。
query.exec(QString("insert into factory values('0','请选择厂家')"));                                  //四行两列
query.exec(QString("insert into factory values('01','一汽大众')"));
query.exec(QString("insert into factory values('02','二汽神龙')"));
query.exec(QString("insert into factory values('03','上海大众')"));


//创建品牌名
query.exec("create table brand(id varchar primary key,name varchar,factory varchar,price int,sum int,sell int,last int)");       //该name与上表中的name含义不同,sell是已销售的,last是剩余的。
query.exec(QString("insert into brand values('01','奥迪A6','一汽大众',36,50,10,40)"));
query.exec(QString("insert into brand values('02','捷达','一汽大众',34,20,5,15)"));
query.exec(QString("insert into brand values('03','宝来','一汽大众',41,80,20,60)"));
query.exec(QString("insert into brand values('04','奔驰','一汽大众',83,40,15,25)"));
query.exec(QString("insert into brand values('05','毕加索','二汽神龙',39,50,15,35)"));
query.exec(QString("insert into brand values('06','富康','二汽神龙',28,60,10,50)"));
query.exec(QString("insert into brand values('07','标致307','二汽神龙',27,70,20,50)"));
query.exec(QString("insert into brand values('08','桑塔纳','上海大众',25,75,25,50)"));
query.exec(QString("insert into brand values('09','帕萨特','上海大众',25,75,25,50)"));

//信息表
query.exec("create table password(account varchar primary key,pwd varchar)");

query.exec(QString("insert into password (account,pwd) values('%1','%2')").arg("lihaiyang").arg("123456"));


return true;
}

2、注册界面(.h&.cpp)

· .h

#ifndef REGISTERED_DIALOG_H
#define REGISTERED_DIALOG_H

#include <QDialog>

namespace Ui {
class Registered_Dialog;
}

class Registered_Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Registered_Dialog(QWidget *parent = 0);
    ~Registered_Dialog();

signals:
            void sendmessage(QString s1,QString s2);

private slots:
    void on_pushButton_clicked();

private:
    Ui::Registered_Dialog *ui;
};

#endif // REGISTERED_DIALOG_H

· .cpp

#include "registered_dialog.h"
#include "ui_registered_dialog.h"
#include<QString>
#include<QMessageBox>

Registered_Dialog::Registered_Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Registered_Dialog)
{
    ui->setupUi(this);
    setFixedSize(441,331);

    ui->lineEdit_2->setMaxLength(6);
    ui->lineEdit_3->setMaxLength(6);
}

Registered_Dialog::~Registered_Dialog()
{
    delete ui;
}

void Registered_Dialog::on_pushButton_clicked()
{
   QString s1=ui->lineEdit_1->text(),s2=ui->lineEdit_2->text(),s3=ui->lineEdit_3->text();

   //同理,先进行两次密码的比对。若相同,则emit,否则message
   if(s1.isEmpty()||s2.isEmpty()||s3.isEmpty())
   {
    QMessageBox::information(this,tr("error"),tr("请输入完整的账号和密码后,重新点击确认!"));
    ui->lineEdit_1->setFocus();
   }
   else
   {
    if(s2!=s3)
    {
       QMessageBox::warning(this,tr("error"),tr("两次密码不匹配"));
       ui->lineEdit_2->clear();
       ui->lineEdit_3->clear();
       ui->lineEdit_2->setFocus();
   }
   else
        emit sendmessage(s1,s2);                                                                    //发送信号(信息:account;pwd)
  }
}

2、登录界面(.h&.cpp)

· .h

#ifndef LOGIN_DIALOG_H
#define LOGIN_DIALOG_H

#include <QDialog>
#include<QString>
#include"registered_dialog.h"
#include"widget.h"
namespace Ui {
class Login_Dialog;
}

class Login_Dialog : public QDialog
{
    Q_OBJECT

    Registered_Dialog *registered;
public:
    explicit Login_Dialog(QWidget *parent = 0);
    ~Login_Dialog();

private slots:
    void on_loginBtn_clicked();

    void on_checkBox_clicked(bool checked);
    void on_pushButton_clicked();

    //接受个人信息
    void receivemessage(QString s1,QString s2);
private:
    Ui::Login_Dialog *ui;
};

#endif // LOGIN_DIALOG_H
· .cpp
#include "login_dialog.h"
#include "ui_login_dialog.h"
#include<QMessageBox>
#include<QtSql>
#include<QString>
Login_Dialog::Login_Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login_Dialog)
{
    ui->setupUi(this);
    setFixedSize(407,308);

    ui->accountLineEdit->setFocus();                      //获得焦点
    ui->loginBtn->setDefault(true);                         //设置该按钮为默认按钮,按回车键时默认选中该按钮

    ui->accountLineEdit->setPlaceholderText("系统账号");
    ui->pwdLineEdit->setPlaceholderText("密码");
}

Login_Dialog::~Login_Dialog()
{
    delete ui;
}

void Login_Dialog::on_loginBtn_clicked()
{
  if(ui->accountLineEdit->text().isEmpty()||ui->pwdLineEdit->text().isEmpty())
  {
      QMessageBox::information(this,tr("无效操作"),tr("请输入账号和密码后,重新点登录!"));
      ui->accountLineEdit->setFocus();
  }
  else
  {
    QSqlQuery query,query1;
    query.first();
    //query.next();
    query.exec("select account from password");                                                         //密码表中查询
    query.next();
    query.first();
    //query.next();
    query1.exec("select pwd from password");
    query1.next();
    if(query.value(0).toString()==ui->accountLineEdit->text()&&query1.value(0).toString()==ui->pwdLineEdit->text())
    {
      QDialog::accept();                  //若账号、密码均匹配成功,返回Accepted信号
      qDebug()<<"登录密码:"<<ui->pwdLineEdit->text()<<endl;
    }
    else
    {
        if(query.value(0).toString()!=ui->accountLineEdit->text()&&query1.value(0).toString()!=ui->pwdLineEdit->text())
        {
        QMessageBox::warning(this,tr("账号和密码均无效"),tr("请确保输入正确的账号和密码后再登录!"),QMessageBox::Ok);
        ui->accountLineEdit->clear();
        ui->pwdLineEdit->clear();                             //清空密码编辑框
        ui->accountLineEdit->setFocus();
        }


      else
      {
            if(query.value(0).toString()!=ui->accountLineEdit->text())
            {
              QMessageBox::warning(this,tr("账号错误"),tr("请确保输入正确的账号后再登录!"),QMessageBox::Ok);
              ui->accountLineEdit->clear();                             //清空密码编辑框
              ui->accountLineEdit->setFocus();                       //该框获得焦点
            }
           else
           {
              QMessageBox::warning(this,tr("密码错误"),tr("请确保输入正确的密码后再登录!"),QMessageBox::Ok);
              ui->pwdLineEdit->clear();                             //清空密码编辑框
              ui->pwdLineEdit->setFocus();
           }
      }
    }
  }
}




//自动登录
void Login_Dialog::on_checkBox_clicked(bool checked)
{
     ui->checkBox->setEnabled(true);
   QSqlQuery query;
   QString s=ui->accountLineEdit->text();
   query.exec(QString("select pwd from password where account ='%1'").arg(s));
   query.next();
   ui->pwdLineEdit->setText(query.value(0).toString());

}



//注册账户按钮
void Login_Dialog::on_pushButton_clicked()
{
    registered=new Registered_Dialog(this);

    connect(registered,SIGNAL(sendmessage(QString,QString)),this,SLOT(receivemessage(QString,QString)));
    registered->exec();
}



//接收信息
void Login_Dialog::receivemessage(QString s1, QString s2)
{
  /*
    QSqlQuery query;

  query.exec(QString("insert into password (account,pwd) values('%1','%2')").arg(s1).arg(s2));
query.first();*/


   //使用占位符(名称绑定)
 /* query.exec(QString("insert into password(id,account,pwd) values(:NULL,'%1','%2)").arg(s1).arg(s2));*/


/*
  query.exec(QString("select pwd from password where account ='%1'").arg(s1));
  query.next();
  QString str=query.value(0).toString();
  qDebug()<<"新创建账户:账户"<<s1<<"密码:"<<str<<endl;

  if(!(str.isEmpty()))
  {
     QMessageBox::information(this,tr("success"),tr("创建成功!"));
     ui->accountLineEdit->setText(s1);
  }*/


  qDebug()<<"s1="<<s1<<"s2="<<s2<<endl;

  QSqlQuery query;
  query.exec(QString("select account from password"));
  query.next();
  QString str1=query.value(0).toString();
  if(str1==s1)
  {
    QMessageBox::critical(this,tr("error"),tr("当前账户已注册!"));
  }
  else
  {
     query.exec(QString("insert into password(account,pwd) values('%1','%2')").arg(s1).arg(s2));
     query.next();

     query.exec(QString("select pwd from password where account ='%1'").arg(s1));
     query.next();
     QString str2=query.value(0).toString();
     qDebug()<<"新创建账户:账户"<<s1<<"密码:"<<str2<<endl;

     QMessageBox::information(this,tr("success"),tr("注册成功!"));
     registered->close();

  }


}


  1.   if(str1==s1)

  2.   {

  3.     QMessageBox::critical(this,tr("error"),tr("当前账户已注册!"));

  4.   }

  5.   else

  6.   {

  7.      query.exec(QString("insert into password(account,pwd) values('%1','%2')").arg(s1).arg(s2));

  8.      query.next();

  9.      query.exec(QString("select pwd from password where account ='%1'").arg(s1));

  10.      query.next();

  11.      QString str2=query.value(0).toString();

  12.      qDebug()<<"新创建账户:账户"<<s1<<"密码:"<<str2<<endl;

  13.      QMessageBox::information(this,tr("success"),tr("注册成功!"));

  14.      registered->close();

  15.   }

 上方的块注释在注册界面的账号、密码发过来后,先把账户跟库内比对。若匹配,则已注册;否则,插入。【因为初次接触sqlite,所以在插入后还特意根据账号在库内搜索其对应的密码并输出。

但是在注册界面出现了问题。首先先拿可登录的账户测试一下(相关代码在底部)

2021-05-07

接着便输入一个新的账号和密码,也在库中select然后deBug显示正常。

2021-05-07

2021-05-07

等转至登录窗口时,输入账号和密码后,显示账号密码全部错误。(点击自动登录,自动填充的密码)

2021-05-07

 

跪求大佬指教!!!

上一篇:学习Qt Charts - 实时曲线


下一篇:element-ui修改dialog样式全局、局部修改问题 el-dialog__title/el-dialog__header/el-dialog__body/el-dialog__footer