Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Program stopped when I use setModel in tableView

Program stopped when I use setModel in tableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 541 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    isan
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      My best bet is that the variable listTime
      is uninitialized.

      1 Reply Last reply
      3
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        3

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved