Results 1 to 2 of 2

Thread: QTableView

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableView

    This should be simple, but I can't see how to do it.
    From the mainwiindow of the application, I create a QSqlTableModel and QTableView and show the view. It pops up in a nice separate window.

    Qt Code:
    1. void MainWindow::showLog(QString fileName) {
    2. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    3. QString logTitle = fileName.mid(fileName.lastIndexOf("/")+1,(fileName.indexOf(".")-4)-(fileName.lastIndexOf("/")+1));
    4. QString logName = logTitle + "_log.sqlite";
    5. db.setDatabaseName(logName);
    6. db.open();
    7. model->setTable("log");
    8. model->select();
    9.  
    10. QTableView *view = new QTableView;
    11. view->setModel(model);
    12. view->setWindowTitle(logTitle);
    13. view->setMinimumSize(1200,100);
    14. view->move(35,80);
    15. view->setSortingEnabled(TRUE);
    16. view->show();
    17.  
    18. ControlDB ctrl;
    19. ctrl.setLastLog(logTitle);
    20. }
    To copy to clipboard, switch view to plain text mode 

    Then I insert into the database behind the model. This works ok. I can see the data in the database with a browser tool.
    Qt Code:
    1. void MainWindow::writeLog() {
    2. ControlDB ctrl;
    3. QString logTitle = ctrl.getLastLog();
    4. QString logName = logTitle + "_log.sqlite";
    5. int newID = ctrl.getMaxID(logName) + 1;
    6.  
    7. QTime t(QTime::currentTime());
    8. QDate d(QDate::currentDate());
    9.  
    10. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    11. db.setDatabaseName(logName);
    12. db.open();
    13. QSqlQuery query;
    14. query.prepare("INSERT into log (id, call, sent, rcvd, date, timeon)VALUES(?, ?, ?, ?, ?, ?)");
    15. query.addBindValue(newID);
    16. query.addBindValue(ui->mwCall->text());
    17. query.addBindValue(ui->mwSent->text());
    18. query.addBindValue(ui->mwRcvd->text());
    19. query.addBindValue(d);
    20. query.addBindValue(t);
    21. query.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 

    So, the question is, after the new data is inserted, how do I refresh the query in the model/view so it is updated?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTableView

    To cause all attached views to refresh you could use the QAbstractItemModel::reset() method through your model. Doing this will lose all view selections, current record etc.

    For a single row insertion you should definitely consider doing it through the model using QSqlTableModel::insertRows(). The model will then look after notifying the attached views about the new row and values.

Similar Threads

  1. One Model, Two Views (QTreeView and QTableView)
    By dgarrett97 in forum Newbie
    Replies: 2
    Last Post: 14th September 2009, 18:10
  2. Replies: 2
    Last Post: 7th June 2009, 10:47
  3. QTableView
    By dragon in forum Qt Programming
    Replies: 0
    Last Post: 22nd September 2008, 16:53
  4. make QTableView work as a multi-column list view
    By wesley in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2008, 14:43
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.