Results 1 to 10 of 10

Thread: Why does the last row remain in QTableView until the page is reloaded ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Why does the last row remain in QTableView until the page is reloaded ?

    Qt Code:
    1. while(qry.next())
    2. {
    3. QSqlQueryModel *model = new QSqlQueryModel(ui->tableView_staffLog);
    4. model->setQuery(qry);
    5. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    6. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    7. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Depart"));
    8. model->setHeaderData(3, Qt::Horizontal, QObject::tr("Car Reg"));
    9. model->setHeaderData(4, Qt::Horizontal, QObject::tr("Srt Date"));
    10. model->setHeaderData(5, Qt::Horizontal, QObject::tr("End Date"));
    11. model->setHeaderData(6, Qt::Horizontal, QObject::tr("Contact"));
    12. model->setHeaderData(7, Qt::Horizontal, QObject::tr("Photo"));
    13. ui->tableView_staffLog->setModel(model);
    14. }
    To copy to clipboard, switch view to plain text mode 

    This code makes no sense at all. Why would you create a new model for *every* result in your query? Each time through the loop, a new model is created, and you set it onto your table view (which replaces the model you created and set on the previous time through the loop).

    Quite likely, this is the source of your bug. If the query returns no result, this loop isn't executed so the model held by the table is not replaced. That model still holds the last row.
    Last edited by d_stranz; 10th August 2017 at 19:09.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  2. #2
    Join Date
    Sep 2016
    Location
    U.K.
    Posts
    16
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Why does the last row remain in QTableView until the page is reloaded ?

    Qt Code:
    1. QSqlQueryModel *model = new QSqlQueryModel(ui->tableView_staffLog);
    2. qry.prepare( "Some Query" );
    3. if( !qry.exec() )
    4. qDebug() << qry.lastError();
    5. else
    6. {
    7. while(qry.next())
    8. {
    9. model->setQuery(qry);
    10. model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
    11. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
    12. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Depart"));
    13. model->setHeaderData(3, Qt::Horizontal, QObject::tr("Car Reg"));
    14. model->setHeaderData(4, Qt::Horizontal, QObject::tr("Srt Date"));
    15. model->setHeaderData(5, Qt::Horizontal, QObject::tr("End Date"));
    16. model->setHeaderData(6, Qt::Horizontal, QObject::tr("Contact"));
    17. model->setHeaderData(7, Qt::Horizontal, QObject::tr("Photo"));
    18. ui->tableView_staffLog->setModel(model);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your reply I have changed it according to your suggestion unfortunately it didn't help.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Why does the last row remain in QTableView until the page is reloaded ?

    Thanks for your reply I have changed it according to your suggestion unfortunately it didn't help.
    Your code still shows a basic misunderstanding. There is absolutely no need to set the model header data on every call to qry.next(). Set it once, after you create the model in line 1. The header data never changes so why set it repeatedly for every result in every query?

    Think about what is happening in line 7: If the query succeeds (qry.exec() returns true) but the result contains ZERO rows (because you have successfully emptied the table), then qry.next() returns false and the code inside the while loop does not get executed. In particular, the line

    Qt Code:
    1. model->setQuery( qry );
    To copy to clipboard, switch view to plain text mode 

    does not get executed, so the model is left holding the last query, which contained ONE result.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Sep 2016
    Location
    U.K.
    Posts
    16
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Thumbs up Re: Why does the last row remain in QTableView until the page is reloaded ?

    d_stranz blue that was 10/10, I've been at it past two bloody days. Success at last Thank you

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Why does the last row remain in QTableView until the page is reloaded ?

    Sure, no worries. One more thing:

    Qt Code:
    1. QSqlQueryModel *model = new QSqlQueryModel(ui->tableView_staffLog);
    To copy to clipboard, switch view to plain text mode 

    This also needs to be done only once, when you create the class that contains this code. You do not need to create a new model each time you do a query (eg. on each data reload). Create the model one time, set it on the table, and remember the pointer as a member variable of your class (or retrieve it from the table in the data reload method via QAbstractItemView::model()). The only things you need to do in your reload data method are:

    - prepare the query (maybe; if it is the same query every time, then make it a member variable too and prepare it once)
    - exec() the query
    - if exec() returns true, set the query on the model, otherwise clear() the model.

    Everything else - setting the header data, setting the model on the view, etc. can be done once in the constructor.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    zed220 (12th August 2017)

Similar Threads

  1. Replies: 3
    Last Post: 20th February 2018, 14:55
  2. qtableview multiply page support
    By tdm in forum Newbie
    Replies: 1
    Last Post: 1st March 2017, 04:41
  3. Symbols remain after running strip
    By ScottBell in forum Qt Tools
    Replies: 0
    Last Post: 7th November 2011, 03:30
  4. Compiling QT programs in Visual C++ 2008 by including batch files, reloaded projects
    By Meek the Geek in forum Installation and Deployment
    Replies: 6
    Last Post: 12th July 2010, 20:11
  5. QMenu: remain visible while mouseOver
    By vonCZ in forum Newbie
    Replies: 10
    Last Post: 25th October 2007, 16:54

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.