2012-12-26 98 views
2

我正在寫一箇中等大小的應用程序來存儲基於sqlite數據庫的數據。我創建了一個將數據添加到數據庫的對話框。在保存數據之前,它會檢查一些條件,以確保輸入的數據是最新的。這是一個簡單的步驟。並容易發現故障。即時通訊,他們是沒有錯誤的崩潰應用程序的重點。該應用程序編譯沒有錯誤。有時它運作良好,但最有可能它崩潰並關閉。爲什麼我的Qt應用程序沒有理由崩潰?

  1. 我的第一個問題是爲什麼會發生這種事情。 (有時它運行良好,有時在相同的條件下崩潰)。

  2. 如何找到在這種情況下故障。我所做的只是改變邏輯和改變代碼,認爲它會改變二進制和糾正錯誤。

    例如: -

    if(query.exec()){ 
        //codes here. 
    } 
    

    我將與

    bool ok=query.exec(); 
        if(ok){ 
         //code here.... 
    } 
    

替換此代碼,請幫我在這sitution,我會在ENY建議韋裏感激。我將添加使應用程序崩潰的newMember.hnewMember.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(); 
       } 

       } 
+4

運行在調試器,它會告訴你它的應用程序崩潰。 –

+0

我在調試Qt應用程序時沒有任何問題。 enyway我會嘗試。並給出結果。謝謝。 –

+0

@NikosC。在應用程序的輸出,它告訴'程序意外finished.' befoe該行有4條線作爲'QSqlDatabasePrivate :: addDatabase:重複在調試器中它不給ENY錯誤連接名稱「qt_sql_default_connection」,舊的連接removed.' –

回答

1
  1. 嘗試在調試器中運行你的應用程序。它會顯示您的應用崩潰的代碼位置。在你的情況下,它可能是段錯誤。
  2. 爲什麼你不檢查返回值?
+0

看到我的評論上面這是我的調試器輸出。他們沒有重要的地方檢查代碼中的返回值,不是。什麼是段錯誤。請幫助 –

+1

Segfault ==分段錯誤==訪問衝突。嘗試訪問處理器無法解決的內存時收到此消息。例如:引用空指針。 – Filippok

+0

對不起在調試器中它不給en錯誤,它按預期工作。在上面的評論中它不在調試器中,我剛從QtCreater運行它。當在調試器中運行時,它不會崩潰。 –

相關問題