Results 1 to 5 of 5

Thread: How to update model when data are stored and modified outside the model?

  1. #1
    Join Date
    Mar 2013
    Posts
    28
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default How to update model when data are stored and modified outside the model?

    This is what I do:
    Make a QVector object to store data. Then make a model which keeps a pointer to the vector. I do it this way to make modifying vector easier.
    Now I can display the data in a view. But what should I do when the vector has been updated? New item will be added so I don't think dataChanged() would always help.
    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to update model when data are stored and modified outside the model?

    In general you should call beginInsertRows() and endInsertRows(). But I would suggest to avoid a situation when the vector can be modified outside of the model. Instead add any methods you might need to the model class that will operate on the vector. In those methods make sure you emit signals the model expects to be emitted.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2013
    Posts
    28
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to update model when data are stored and modified outside the model?

    Quote Originally Posted by wysota View Post
    In general you should call beginInsertRows() and endInsertRows(). But I would suggest to avoid a situation when the vector can be modified outside of the model. Instead add any methods you might need to the model class that will operate on the vector. In those methods make sure you emit signals the model expects to be emitted.
    Thanks!
    I used to consider a model as an interface instead of a wrapper. Now it seems storing data in the model or not won't matter because you can only read/write the data with the model anyway.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to update model when data are stored and modified outside the model?

    It is an interface. It doesn't matter if the data is inside the model or not, what matters is that all operations on the data are done through the model.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    hind (21st October 2013)

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to update model when data are stored and modified outside the model?

    Quote Originally Posted by hind View Post
    I used to consider a model as an interface instead of a wrapper.anyway.
    That is a valid assumption, actually the very goal of most models.

    You only need to make sure the model knows about changes to the actual data, but that can be done in various ways.
    For example the class modifying the data could emit signals, the data could be in a class that has signals, the modifying code could tell the model, or, as wysota already said, the model can the only interface to the data.

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    hind (25th October 2013)

Similar Threads

  1. Replies: 1
    Last Post: 29th August 2013, 06:41
  2. Replies: 9
    Last Post: 14th February 2013, 20:39
  3. Do I need update model in QT UI thread?
    By ysilent in forum Newbie
    Replies: 3
    Last Post: 23rd March 2012, 11:59
  4. Replies: 0
    Last Post: 2nd November 2011, 12:13
  5. Replies: 1
    Last Post: 24th February 2011, 06:54

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.