Unsolved Program stopped when I use setModel in tableView
-
I set tableView in .ui
QTableView *tableView; tableView = new QTableView(page_3); tableView->setObjectName(QStringLiteral("tableView")); tableView->setGeometry(QRect(0, 0, 751, 501));
And I wrote this to add the SQLite database data to the tableView:
void dbFunc::dbinfoGet(QString dbname,QString year,QString mths) { QSqlDatabase db1 = QSqlDatabase::addDatabase( "QSQLITE" ); QSqlQueryModel *model =new QSqlQueryModel(); db1.setDatabaseName( dbname ); if( !db1.open() ) { qDebug() << db1.lastError(); qDebug( "Failed to connect." ); } else qDebug( "Connected to database!" ); QSqlQuery *qry=new QSqlQuery(db1); qry->prepare( "SELECT * FROM _"+mths+""); qry->exec(); model->setQuery(*qry); listTime->ui->tableView->setModel(model); }
but this line ( listTime->ui->tableView->setModel(model);) cause the program to crash and stop
where is the problem? -
Hi
My best bet is that the variable listTime
is uninitialized. -
Apart from what @mrjj says you create a memleak when creating the QSqlQuery on the heap - there is no need for it.
Also I don't understand why you call addDatabase here - why do you open a new db connection on every call to dbinfoGet()?