Results 1 to 8 of 8

Thread: How to notify QTableView of a modified CustomModel

  1. #1
    Join Date
    Sep 2008
    Posts
    54
    Thanks
    3
    Thanked 10 Times in 5 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    2

    Default How to notify QTableView of a modified CustomModel

    Hello,

    I was wondering if there is a way to have the QTableView reread the model data.

    If I set a custom model on a tableview all data is read. But if I after that add data, this is due to loading of data, to the custom model without using beginInsertRows() and endInsertRows() there seems to be no way of letting the View know that the whole model needs to be reread.

    If I set the model on the view after loading the data everything works fine. But than the programs does not work in other situations.

    Does anyone know how to force a total update of the TableView?

    Regards,

    Marcel

  2. #2
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to notify QTableView of a modified CustomModel

    You can always reset the model for the view. Notice though that any signals and slots will need to be reconnected and the memory for the old model deleted (pseudocode):

    CustomModel* oldModel = treeviewPtr->getModel();
    CustomModel* temp = new CustomModel();
    treeviewPtr->setModel(temp);
    delete oldModel;

  3. The following user says thank you to bmhautz for this useful post:

    mstegehu (24th February 2010)

  4. #3
    Join Date
    Apr 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to notify QTableView of a modified CustomModel

    What I would do here is first delete all rows (using beginRemoveRows/endRemoveRows), and then add the new rows as you did.

  5. #4
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to notify QTableView of a modified CustomModel

    Call beginResetModel; modify model; then call endResetModel. That should do the trick. It shouldn't be necessary to delete and replace the model.

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

    bmhautz (24th February 2010)

  7. #5
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to notify QTableView of a modified CustomModel

    Quote Originally Posted by stefanadelbert View Post
    Call beginResetModel; modify model; then call endResetModel. That should do the trick. It shouldn't be necessary to delete and replace the model.
    That would work, too, but only if you're using 4.6 and above.

  8. #6
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to notify QTableView of a modified CustomModel

    Ah, right. I have only recently started using Qt so only know 4.6+. I'm not going to be much help here.

  9. #7
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to notify QTableView of a modified CustomModel

    I think it was a great thing that you mentioned it! I didn't even know it existed until I looked at the recent Qt documentation for 4.6.x. I'm currently using 4.5.3, so I had to use the setModel, delete old model pointer method for my uses. But I'll be sure to use the new method you mentioned when I upgrade!

  10. #8
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to notify QTableView of a modified CustomModel

    Hi,

    I solved this kind of "situation" in a very "craft" way:

    Qt Code:
    1. ...
    2. Dialog::Dialog(QWidget *parent)
    3. : QDialog(parent), ui(new Ui::Dialog)
    4. {
    5. ui->setupUi(this);
    6. ui->fileNameLabel->setText("pessoa.png");
    7. model = new QSqlTableModel(this);
    8. setModel();
    9. setView();
    10. ...
    11. }
    12.  
    13. void Dialog::setModel()
    14. {
    15. model->setTable("photo");
    16. model->setHeaderData(photo_id, Qt::Horizontal, tr("ID"));
    17. model->setHeaderData(photo_name, Qt::Horizontal, trUtf8("Filename"));
    18. model->setHeaderData(photo_image, Qt::Horizontal, trUtf8("Image"));
    19. model->select();
    20. }
    21.  
    22. void Dialog::setView()
    23. {
    24. view = ui->tableView;
    25. view->setModel(model);
    26. view->setColumnHidden(photo_image, true);
    27. view->horizontalHeader()->setStretchLastSection(true);
    28. view->setSelectionMode(QAbstractItemView::SingleSelection);
    29. view->setSelectionBehavior(QAbstractItemView::SelectRows);
    30. }
    To copy to clipboard, switch view to plain text mode 

    This way i call setView() when i need to make sure that model and view are sinchronized.

Similar Threads

  1. how to implement QApplication.notify()
    By di_zou in forum Newbie
    Replies: 1
    Last Post: 15th February 2010, 18:12
  2. URL last modified
    By tsd-charlie in forum Qt Programming
    Replies: 1
    Last Post: 2nd October 2009, 15:09
  3. QApplication::notify()
    By jeffpogo in forum Qt Programming
    Replies: 1
    Last Post: 16th June 2009, 22:46
  4. Reimplementing QApplication::notify( )
    By Rayven in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2008, 17:22
  5. Qt web notify
    By bunjee in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 13:24

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.