Results 1 to 10 of 10

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

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

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

    Hi All,

    I'm using [Qt] QTableView and QSqlQueryModel with SQLite Database, I'm trying to delete rows from the QTableView(from the database) by clicking on a row which opens up another page where I carry out the deletion and using 'emit' signal and slot to update the parent page, it all works fine till the VERY LAST ROW. It deletes from the database but remains in the
    QTableView until I reload the page, why ?

    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. }
    15. connect(ui->tableView_staffLog, SIGNAL(pressed(const QModelIndex&)), this,
    16. SLOT(on_tableView_staffLog_clicked(const QModelIndex&)));
    17. }
    18. void ControlPanel::on_tableView_staffLog_clicked(const QModelIndex& index)
    19. {
    20. const QAbstractItemModel* tableModel = ui->tableView_staffLog->model();
    21. int emp_id = tableModel->data(tableModel->index(index.row(),0), Qt::DisplayRole).toInt();
    22.  
    23. employeedetails = EmployeeDetails::instance(emp_id, index.row(), this);
    24. connect( employeedetails, SIGNAL(employeeDeleted(int)), this, SLOT(deleteEmployee(int)) );
    25. employeedetails->show();
    26. }
    To copy to clipboard, switch view to plain text mode 

    Child page
    Qt Code:
    1. query.prepare("UPDATE employee SET is_hidden = '1' WHERE emp_id = (:emp_id)");
    2. query.bindValue(":emp_id", emp_id);
    3. success = query.exec();
    4. if(!success)
    5. {
    6. qDebug() << "removeEmployee error: "
    7. << query.lastError();
    8. }
    9. else
    10. {
    11. if( tableRow >= 0 )
    12. emit employeeDeleted(tableRow);
    13. this->close();
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by zed220; 10th August 2017 at 11:06.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    show the deleteEmployee() slot.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    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 ?

    Thanks for your reply

    Qt Code:
    1. void ControlPanel::deleteEmployee(int row)
    2. {
    3. loadAllData();
    4. }
    To copy to clipboard, switch view to plain text mode 

    It basically reloads all the data in parent page.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

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

    *sigh*
    Do you want to be helped or not?

    Show us the code that removes employees from your model, the problem is probably there.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    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 ?

    Thanks for your reply again, I have already posted that code it's a soft delete meaning it UPDATES and hides the record instead of complete delete.
    Qt Code:
    1. query.prepare("UPDATE employee SET is_hidden = '1' WHERE emp_id = (:emp_id)");
    2. query.bindValue(":emp_id", emp_id);
    3. success = query.exec();
    4. if(!success)
    5. {
    6. qDebug() << "removeEmployee error: "
    7. << query.lastError();
    8. }
    9. else
    10. {
    11. if( tableRow >= 0 )
    12. emit employeeDeleted(tableRow);
    13. this->close();
    14. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,249
    Thanks
    304
    Thanked 868 Times in 855 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 20: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.

  7. #7
    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.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,249
    Thanks
    304
    Thanked 868 Times in 855 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.

  9. #9
    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

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,249
    Thanks
    304
    Thanked 868 Times in 855 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.

  11. 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, 15:55
  2. qtableview multiply page support
    By tdm in forum Newbie
    Replies: 1
    Last Post: 1st March 2017, 05:41
  3. Symbols remain after running strip
    By ScottBell in forum Qt Tools
    Replies: 0
    Last Post: 7th November 2011, 04: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, 21:11
  5. QMenu: remain visible while mouseOver
    By vonCZ in forum Newbie
    Replies: 10
    Last Post: 25th October 2007, 17: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.