2014-12-27 24 views
0

我試圖創建一個Qt的線自動填充編輯,但我的程序崩潰的原因,我無法理解,這使得它崩潰是Qt的程序意外成品

model_Customer->setTable("Customer"); 
model_Customer->select(); 
model_Product->setTable("Product"); 
model_Product->select(); 
Account_completer->setModel(model_Customer); 
Product_completer->setModel(model_Product); 
Account_completer->setCaseSensitivity(Qt::CaseInsensitive); 
Product_completer->setCaseSensitivity(Qt::CaseInsensitive); 
AutoComplete(); 
ui->lineEdit_Invoice_Account->setCompleter(Account_completer); 
ui->lineEdit_Invoice_Product->setCompleter(Product_completer); 

我.h文件中低於

代碼
QSqlDatabase db; 
QSqlTableModel *model_Customer; 
QSqlTableModel *model_Product; 
QCompleter *Account_completer; 
QCompleter *Product_completer; 

謝謝

+1

您是否創建了模型的新實例?像:'model_Customer = new QSqlTableModel(this,db);' – Nejat 2014-12-27 18:32:41

+0

謝謝,我剛剛注意到我忘了補充說 – Root0x 2014-12-27 18:35:29

回答

1

當你試圖使用未分配的指針,你使用的是懸掛指針,它是不確定的行爲,但大多數時候,它是一個崩潰。最糟糕的是,這是一個讓你的程序表現怪異的錯誤。

請記住始終初始化您的指針,即使它是0,NULLnullptr。最好的方法是如果你習慣了就地統一初始化,那麼你應該能夠使用C++ 11或更高版本。

此外,通過使用像valgrind,gdb和其他內存跟蹤器這樣的工具,這些問題相對容易被發現。