Results 1 to 7 of 7

Thread: Problem showing QTableView

  1. #1
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Problem showing QTableView

    I have the following code inside a pushbutton event. When it runs the data does not show up on the QTableView's grid. There are 74 rows in the model, the "for" statement sends them out to the console correctly.

    What am I doing wrong?

    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3.  
    4. DBMgr dbmgr;
    5. if (!dbmgr.OpenDB("moz_downloads","","downloads.sqlite"))
    6. return;
    7.  
    8. wrkTable = dbmgr.theModel;
    9. ui->tableView->setModel(wrkTable);
    10. ui->tableView->show();
    11.  
    12. for (int i = 0; i < wrkTable->rowCount(); i++)
    13. {
    14. QSqlRecord r = wrkTable->record(i);
    15. QString s = r.value("Name").toString();
    16. qDebug() << s;
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem showing QTableView

    Is wrkTable a QSqlQueryModel ?

  3. #3
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: Problem showing QTableView

    No I am using a QSqlTableModel.

    Here is the code. Looking at all the examples they create the db/model before they run the app.exec() routine. Wonder if the data is there but the QTableView is not getting updated.

    Qt Code:
    1. bool DBMgr::OpenDB(QString tablename, QString filter, QString filename)
    2. {
    3. error = "";
    4. if (!QFile::exists(filename))
    5. {
    6. error = "File does not exist";
    7. return false;
    8. }
    9.  
    10. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    11. db.setDatabaseName("downloads.sqlite");
    12. qDebug() << "Is Valid:" << db.isValid();
    13. if (!db.open())
    14. {
    15. error = "Unable to open DataBase";
    16. return false;
    17. }
    18.  
    19. theModel = new QSqlTableModel(NULL,db);
    20. theModel->setTable(tablename);
    21. if (!filter.isEmpty())
    22. theModel->setFilter(filter);
    23. theModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    24. theModel->select();
    25.  
    26. qDebug() << "Row Count = " << theModel->rowCount();
    27.  
    28. return true;
    29. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem showing QTableView

    I know this works for me:
    Qt Code:
    1. model = new QSqlTableModel;
    2. view->setModel(model);
    3. model->setQuery(aQuery); // executes the query
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: Problem showing QTableView

    Very odd, The vary same code to open the DB and set up a model works in the main line, but when I put it in a method in another class and pass back the Model it fails.

    I think it has something to do with the fact if you do the following code the QSqlDatabase and QSqlTAbleModel are magically associated with one another. Even though nothing ties them together. I mean how does the QSqlTableModel know that the "moz_downloads" table is associated with the "downloads.sqlite" database?

    And this code does work. But when I take the DB part and the QSqlTableModel and put them in a different class and pass back the QSqlTableModel it fails.

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("downloads.sqlite");
    3. if (!db.open()) {
    4. qDebug() << "Error opening DB.";
    5. }
    6.  
    7. qtm->setTable("moz_downloads");
    8. qtm->select();
    9.  
    10. ui->tableView->setModel(qtm);
    11. ui->tableView->show();
    To copy to clipboard, switch view to plain text mode 
    Last edited by weaver4; 23rd November 2009 at 01:26.

  6. #6
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem showing QTableView

    There's no "magic", QSqlDatabase is a static class and anything you do in your app is thru the database you opened with:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    To copy to clipboard, switch view to plain text mode 
    When you need to access it from your methods, you create an instance of it:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    db now points to the database you opened at the start, until you close it.

    Also, your table model that you created in your method will be destroyed as soon as your method returns, that's why it fails. The correct way is to create the table model from your calling routine and pass it to your function.
    Last edited by vieraci; 23rd November 2009 at 13:09. Reason: updated contents

  7. #7
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: Problem showing QTableView

    My confusion codes from something like this.

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. QSqlDatabase db2 = QSqlDatabase::addDatabase("QSQLITE");
    3.  
    4. db.setDatabaseName("downloads.sqlite");
    5. if (!db.open()) {
    6. qDebug() << "Error opening DB.";
    7. }
    8.  
    9. db2.setDatabaseName("history.sqlite");
    10. if (!db2.open()) {
    11. qDebug() << "Error opening DB.";
    12. }
    13.  
    14. qtm->setTable("moz_downloads");
    15. qtm->select();
    To copy to clipboard, switch view to plain text mode 

    In this case which db is the QSqlTableModel qtm model going to associate the "moz_downloads" table with?

    If the table-model was destroyed then it could not print out the records in my button routine could it? But it does.

Similar Threads

  1. QTreeView problem scrolling to end.
    By seneca in forum Qt Programming
    Replies: 7
    Last Post: 22nd December 2015, 12:08
  2. QTableView and QSqlTableModel problem
    By Misenko in forum Qt Programming
    Replies: 5
    Last Post: 19th August 2008, 21:58
  3. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  4. QTableView showing empty fields
    By ederbs in forum Qt Programming
    Replies: 5
    Last Post: 4th October 2007, 00:30
  5. QTableView not corectly showing empty fields
    By Sergey B. in forum Qt Programming
    Replies: 4
    Last Post: 15th June 2007, 08:18

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.