QString number=ui->numberLe->text(); QString password=ui->passwordLe->text(); QString password2=ui->password2Le->text(); QString name=ui->nameLe->text(); QString sex; QSqlQuery query; BG->addButton(ui->radioButton,0); BG->addButton(ui->radioButton_2,1); switch (BG->checkedId()) { case 0: sex="女"; break; case 1: sex="男"; break; } if(!iscard(number)) { QMessageBox::warning(NULL,"注册失败","您的证号不符合规定,请重新输入!"); } else { if(!ispassword(password)) { QMessageBox::warning(NULL,"注册失败","您的密码不符合规定,请重新输入!"); } else { if(QString::compare(password,password2)!=0) { QMessageBox::warning(NULL,"注册失败","两次密码输入不相同!"); } else { QString InsertStr=QString("insert into user(number,password,name,sex)""values('%1','%2','%3','%4')").arg(number).arg(password).arg(name).arg(sex); qDebug()<<InsertStr; if(number!=NULL&&password!=NULL&&name!=NULL&&sex!=NULL) { if(isonly(number)) { if(query.exec(InsertStr)) { QMessageBox::information(NULL,"注册成功","注册成功!",QMessageBox::Ok); this->close(); } else { QMessageBox::warning(NULL,"注册失败","注册失败,请稍后重试!"); } } else { QMessageBox::warning(NULL,"注册失败","该用户已存在!"); } } else { QMessageBox::warning(NULL,"注册失败","信息不能为空!"); } } } }根据这段代码可以注册账号但是为什么会出现崩溃问题
时间: 2024-01-24 22:18:21 浏览: 161
代码中可能存在潜在的问题,导致程序崩溃。以下是可能导致崩溃的原因:
1. BG(QButtonGroup)指针为 NULL,导致调用 BG->addButton() 函数时程序崩溃。
2. 在 if(!iscard(number))、if(!ispassword(password))、if(QString::compare(password,password2)!=0) 的条件判断语句中,如果 number、password 或 password2 为空,会导致程序崩溃。
3. 如果查询语句 query.exec(InsertStr) 执行失败,会导致程序崩溃。
为了解决这些问题,可以在代码中进行以下修改:
1. 在使用 BG 前,先进行 BG 指针的判断,避免指针为空时调用函数导致程序崩溃。
2. 在判断 number、password、password2 是否为空之前,先进行非空判断,避免空指针引用导致程序崩溃。
3. 在调用 query.exec(InsertStr) 函数前,先进行查询语句的语法检查,避免语法错误导致程序崩溃。同时,在执行查询语句后,需要进行查询结果的判断,避免执行失败导致程序崩溃。
相关问题
void Login::on_loginBtn_clicked() { ui->passwordLe->setEchoMode(QLineEdit::Password); Login::number=ui->numberLe->text(); QString password=ui->passwordLe->text(); QSqlQuery query; if(Login::number.length()!=0&&password.length()!=0){ QString instruction=QString("select password from users where number='%1'").arg(Login::number); query.exec(instruction); while(query.next()) { if(Login::number=="0000"&&password=="123456") {//可以自己更改管理员的账号和密码 this->close(); MainWindow* w=new MainWindow(); w->show(); break; } else if(Login::number!="0000"&&query.value(0).toString()==password){ this->close(); user* w2=new user(); w2->show(); break; } else{ QMessageBox::information(this, "登录失败", "用户名或者密码错误"); break; } } } else{ QMessageBox::warning(this,"登录失败","不能为空!"); } QString getId=QString("select id from users where number='%1'").arg(Login::number); query.exec(getId); while(query.next()){ Login::id=query.value(0).toInt(); } }
这是一个Qt界面程序的登录按钮的点击事件处理函数。该函数首先设置密码框的回显模式为密码,然后获取输入的用户名和密码。接着,它会执行一个SQL查询,从数据库中查找与输入的用户名相匹配的密码。如果是管理员账号,且密码正确,则关闭登录窗口并打开主窗口;如果是普通用户账号,且密码正确,则关闭登录窗口并打开用户窗口;如果用户名或密码错误,则弹出一个信息框提示登录失败。最后,该函数再次执行一个SQL查询,获取与输入的用户名相匹配的用户ID,并将其存储在静态变量id中。
#include "dialog.h" #include "ui_dialog.h" #include "student.h" #include "studentwork.h" #include <studentmanager.h> #include <QFile> #include <QTextCodec> #include <QTimer> #include <QCoreApplication> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); ui->lineEditPassword->setEchoMode(QLineEdit::Password); setWindowTitle("Login"); } Dialog::~Dialog() { delete ui; } void Dialog::on_buttonLogin_clicked() { student* newStudent = new student; static int count=0; QString ID=ui->lineEditPassword->text(); auto it = studentManager::m_students.find(ID); QString filePath = QCoreApplication::applicationDirPath() + "/student.txt"; QFile file(filePath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Could not open file" << filePath; return; } QTextStream in(&file); while (!in.atEnd()&&ui->lineEditID->text()!=newStudent->m_name) { QString line = in.readLine(); if(line == "end"){ break; } QStringList data = line.split(" "); newStudent->m_name = data[0]; newStudent->m_ID = data[1]; if(ui->lineEditID->text()==newStudent->m_name){ break; } } if (ui->lineEditPassword->text()==newStudent->m_ID) { accept(); } else { count++; qDebug()<<count; if(count>2) { this->close(); // 3次错误退出 } else { /* QMessageBox::warning(NULL,"warning",newStudent->m_name+newStudent->m_ID);*/ QMessageBox::warning(NULL, "warning","Account password error,please login again.you have "+QString::number(3-count)+" chances"); } } file.close(); }
这是一个登录界面的代码,通过输入用户名和密码来进行登录,其中使用了student类和studentManager类来进行数据管理。文件读取使用了QFile和QTextStream类。在点击登录按钮时,会读取文件中的内容,并将用户名和密码与输入的内容进行比对,如果一致则登录成功,否则会有三次机会重新输入密码,超过三次会强制退出程序。
阅读全文
相关推荐









