Solved comboBox->additem() crash application..
-
Hi,
i've populated from database n.1 comboBox and all is OKif i click on Annulla pushbutton (this is the code):
void inserisciarticolo::on_pushButton_Annulla_clicked() { // ANNULLA if (aDatabase.open()) { aDatabase.close(); } close(); }
and try to re-load the dialog (from application menu)
after first record of database additem() crashes the application
if i comment the additem() all works
this is the code i've used to load the comboBox data:
QSqlQuery cl; cl.prepare("Select RAGSOC from CLIENTI order by RAGSOC ASC"); ui->comboBox_Cliente->clear(); if (cl.exec()) { cl.first(); do { ui->comboBox_Cliente->addItem(cl.value(0).toString()); } while (cl.next()); }
ideas??
Thanks in advance.
-
Hi
Check if
cl.first(); returns true before using it. -
if (cl.exec()) { msg.setText("QUI"); msg.exec(); cl.first(); if (cl.first()) { msg.setText("OK"); msg.exec(); } do { ui->comboBox_Cliente->addItem(cl.value(0).toString()); } while (cl.next()); }
messagebox will appear with OK message
if i replace additem with messagebox all works correctly
-
Hi
Super.
Try
QString test=cl.value(0).toString();
and see if it crash on that line instead.
else it must mean
ui->comboBox_Cliente is not valid pointer. -
I know this does not address your problem but it should solve it anyway. Replace all your code with:
QSqlQueryModel* queryModel = new QSqlQueryModel(this); queryModel.setQuery(QStringLiteral("Select RAGSOC from CLIENTI order by RAGSOC ASC")); if(dynamic_cast<QSqlQueryModel*>(ui->comboBox_Cliente->model())) ui->comboBox_Cliente->model()->deleteLater(); ui->comboBox_Cliente->setModel(queryModel);
-
@mrjj i've tried and crash on additem()..
comboBox is not a pointer .. i've create it with designer in the ui
-
@VRonin it will crash however
-
Hi! Actually in designer it creates pointers,
ui->
is thenamespace
, andcomboBox_Cliente->
is aC++ pointer
. What is the error message when it crashes? -
in application output :
18:45:34: The process was ended forcefully.
18:45:34: /Users/matteo/Desktop/C++/QT/EasyGest/build-EasyGest-Desktop_Qt_5_11_2_clang_64bit18-Release/EasyGest.app/Contents/MacOS/EasyGest crashed. -
in the constructor of your class, can you add
QObject::connect(ui->comboBox_Cliente,&Qobject::destroyed,[]()->void{qDebug("Something exploded");});
?If you see the output it means you are deleting your ui before reusing it
-
@VRonin said in comboBox->additem() crash application..:
QObject::connect(ui->comboBox_Cliente,&Qobject::destroyed,->void{qDebug("Something exploded");});
i don't understand where i have to put it..
in the .h file
after Q_OBJECT ??
-
@VRonin said in comboBox->additem() crash application..:
in the constructor of your class
-
-
And it still crashes? did you set
Qt::WA_DeleteOnClose
? -
@VRonin No, i don't know what is it.. sorry but i'm new user of Qt
-
One more try, add
QObject::connect(this,&QObject::destroyed,[]()->void{qDebug("Something big exploded");});
and see if you have any output when you crash the application -
@VRonin said in comboBox->additem() crash application..:
QObject::connect(this,&QObject::destroyed,->void{qDebug("Something big exploded");});
no, i don't see any output..
-
This is turning into a mistery now. Can you post the stack trace at the moment of crash?
-
This crash happens after you
addItem()
to theui->comboBox_Cliente
?Try to change the code to:
if (cl.exec()) { msg.setText("QUI"); msg.exec(); if (cl.first()) { msg.setText("OK"); msg.exec(); } do { ui->comboBox_Cliente->addItem(cl.value(0).toString()); } while (cl.next()); }
and check if it still crashes?
-
after first record of database additem() crashes the application
Has nobody yet asked you to run this in a debugger (e.g. from Qt Creator)? And give us the stacktrace? EDIT: Oh, @VRonin has 2 posts above. Please do so!