Important: Please read the Qt Code of Conduct -

Program stopped when I use setModel in tableView

  • I set tableView in .ui

    QTableView *tableView;
         tableView = new QTableView(page_3);
         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( ! )
            qDebug() << db1.lastError();
            qDebug( "Failed to connect." );
            qDebug( "Connected to database!" );
        QSqlQuery  *qry=new QSqlQuery(db1);
        qry->prepare( "SELECT * FROM _"+mths+"");

    but this line ( listTime->ui->tableView->setModel(model);) cause the program to crash and stop
    where is the problem?

  • Lifetime Qt Champion

    My best bet is that the variable listTime
    is uninitialized.

  • Lifetime Qt Champion

    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()?

Log in to reply