我正在写一个中等大小的应用程序来存储基于sqlite数据库的数据。我创建了一个将数据添加到数据库的对话框。在保存数据之前,它会检查一些条件,以确保输入的数据是最新的。这是一个简单的步骤。并容易发现故障。即时通讯,他们是没有错误的崩溃应用程序的重点。该应用程序编译没有错误。有时它运作良好,但最有可能它崩溃并关闭。为什么我的Qt应用程序没有理由崩溃?
我的第一个问题是为什么会发生这种事情。 (有时它运行良好,有时在相同的条件下崩溃)。
如何找到在这种情况下故障。我所做的只是改变逻辑和改变代码,认为它会改变二进制和纠正错误。
例如: -
if(query.exec()){ //codes here. }
我将与
bool ok=query.exec(); if(ok){ //code here.... }
替换此代码,请帮我在这sitution,我会在ENY建议韦里感激。我将添加使应用程序崩溃的newMember.h
和newMember.cpp
。 (这是我的new member
对话框的类)。如果需要更多的数据需要告诉我,我也会添加它们。
,错误签名中提到ModName:qtgui4.dll , Offset: 000c14e6
newMember.h
newMember.cpp
#include "newmember.h"
#include<QtGui>
#include<QtSql/QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
newMember::newMember(QString str, QWidget *parent)
{
setupUi(this);
lineEdit_7->setText(str);
radioButton->setChecked(true); ;
connect(pushButton,SIGNAL(clicked()),this,SLOT(browse()));
connect(pushButton_2,SIGNAL(clicked()),this,SLOT(save_2()));
}
void newMember::browse(){
path_1=QFileDialog::getOpenFileName(this,"choose an image for the new house", QString::null,"Image Files (*.jpg *.bmp)");
pic.load(path_1);
pic=pic.scaled(284,213,Qt::KeepAspectRatio, Qt::SmoothTransformation);
label_14->setPixmap(pic);
}
QString input1(QString str){
if(str=="")
return "-NA-";
else
return str;
}
void newMember::save_2(){
QByteArray array;
QBuffer buf(&array);
buf.open(QIODevice::WriteOnly);
pic.save(&buf,"jpg");
QString mof;
if(radioButton->isChecked())mof="male";
if(radioButton_2->isChecked())mof="female";
QString isgm="false";
if(checkBox->isChecked())isgm="true";
QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("data");
db.open();
QSqlQuery query;
query.exec("create table members(aname text, homeno text, namein text, fname text, onames text, nic text, sex text, bday text,gm text,occupation text,contactno text,qulification text,note text, img BLOB) ");
if(lineEdit_8->text()==""){
QMessageBox::about(this,"error","you should enter a name to identify this member \n within the specific house");
return;
}
query.prepare("select aname from members where homeno=? and aname=? ");
query.bindValue(0,lineEdit_7->text());
query.bindValue(1,lineEdit_8->text());
query.exec();
if(query.next()){
QMessageBox::about(this, "error", "the name you entered to identify this member \n is already available, please enter another one") ;
return;
}
if(isgm=="true"){
query.prepare("select aname from members where homeno=? and gm=?");
query.bindValue(0,lineEdit_7->text());
query.bindValue(1,"true");
query.exec();
if(query.next()){
QMessageBox::about(this, "error", "there is a gruha mulikaya set already");
return;
}
}
query.prepare("insert into members(aname, homeno , namein , fname , onames , nic , sex , bday ,gm ,occupation ,contactno ,qulification ,note ,img) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?) ");
query.bindValue(0,lineEdit_8->text());
query.bindValue(1,input1(lineEdit_7->text()));
query.bindValue(2,input1(lineEdit->text()));
query.bindValue(3,input1(lineEdit_2->text()));
query.bindValue(4,input1(lineEdit_3->text()));
query.bindValue(5,input1(lineEdit_4->text()));
query.bindValue(6,mof);
query.bindValue(7,dateEdit->date().toString("yyyy-MM-dd"));
query.bindValue(8,isgm);
query.bindValue(9,input1(lineEdit_5->text()));
query.bindValue(10,input1(lineEdit_6->text()));
query.bindValue(11,input1(textEdit->toPlainText()));
query.bindValue(12,input1(textEdit_2->toPlainText()));
query.bindValue(13,array);
bool ok=query.exec();
if(!ok){
QSqlError error;
error=query.lastError();
QMessageBox::about(this,"error",error.text() );
}
else{
QMessageBox::about(this,"message","data added successfully");
newMember::close();
}
}
运行在调试器,它会告诉你它的应用程序崩溃。 –
我在调试Qt应用程序时没有任何问题。 enyway我会尝试。并给出结果。谢谢。 –
@NikosC。在应用程序的输出,它告诉'程序意外finished.' befoe该行有4条线作为'QSqlDatabasePrivate :: addDatabase:重复在调试器中它不给ENY错误连接名称“qt_sql_default_connection”,旧的连接removed.' –