Results 1 to 13 of 13

Thread: How to inform a Model/TableView that some data changed?

  1. #1
    Join Date
    Aug 2009
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default How to inform a Model/TableView that some data changed?

    I have a QTableView and a Table Model, how do I inform the view that some data changed? or do I have to inform the model instead?
    Is there a method to call? or should it be handled with slots/signals?

  2. #2
    Join Date
    Apr 2010
    Posts
    98
    Thanks
    19
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to inform a Model/TableView that some data changed?

    Check the QAbstractItemModel docs.

    #1 You can just emit layoutAboutToBeChanged() and layoutChanged().The view will then update itself thoroughly.
    #2 If you are inserting/removing/moving items.Check the protected function.
    example:
    Qt Code:
    1. void MyModel::removeOneRow(int index)
    2. {
    3. beginRemoveRows(xx);
    4. ...//Do the remove action here.
    5. endRemoveRows(xx);
    6. }
    To copy to clipboard, switch view to plain text mode 
    After calling this function,the view will also update itself.
    #3 If you want to update a range of item.Check the dataChanged(xxx) signal.
    It's not the goodbye that hurts,but the flashback that follow.

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

    jcyangzh (6th October 2012)

  4. #3
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to inform a Model/TableView that some data changed?

    Depends on the model and view design.
    For example, if you change data in a database directly, that the model depends on, then all you would have to do is model->select() to refresh it. The view will get a signal from the model and show the changes.
    When a user edits data directly in the view, and hits enter, the model should be updated.

  5. #4
    Join Date
    Aug 2009
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to inform a Model/TableView that some data changed?

    No, I'm not inserting or deleting rows or columns, just changing individual cell's values.
    It's not a database model either.
    It's a custom model subclassed from QAbstractTableModel, the underlying data structure is a simple char[] array, with its dimensions not changing, and the Table View just displays their contents.
    So, should I fire signal dataChanged() from the model or from the view?

  6. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to inform a Model/TableView that some data changed?

    How will the view know what has changed? Only the class (model) that has the data will know that, so you fire it from there. The view will receive the signal and update accordingly.

  7. #6
    Join Date
    Aug 2009
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to inform a Model/TableView that some data changed?

    I wasn't clear enough, what I meant is if I had to call the view's signal dataChanged() or the model's signal dataChanged()

  8. #7
    Join Date
    Aug 2009
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to inform a Model/TableView that some data changed?

    Ok I'll try to explain better:

    I have a char array that's the underlying data structure.
    I've subclassed QAbstractTableModel to act as the model.
    I've added a Table view like this:



    I have a timer that increments the underlying data, in its [0,0] position.
    Right now, when I select a cell, or interact in some way with the table, then the cell's value is updated, because the table view somehow calls the model's data() method.
    What I want to achieve is on each timer event, twice per second, the view updates the cell's values on the screen.
    So please tell me what object should be the sender, and what signal of it, and what object should be the receiver, and what slot of it.
    Or if I can achieve this with just a method call, instead of signals.

    Edit:
    I tried emitting layoutChanged() from the model, which works ok.
    But I'm setting a timer with 0 msec timout and all I get is the signal emitted no more than 30 times per second, I need something faster.

    Can't I just inform the view to update exactly the cell I know has changed?
    I tried with the signal QAbstractItemModel::dataChanged, but I can't emit it from a QAbstractTableModel subclass, although it should inherit QAbstractItemModel's members, so what, aren't signals inherited?
    Last edited by Petruza; 20th June 2010 at 21:30.

  9. #8
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to inform a Model/TableView that some data changed?

    What about this?
    void QAbstractItemModel::dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight ) [signal]

    This signal is emitted whenever the data in an existing item changes.

    If the items are of the same parent, the affected ones are those between topLeft and bottomRight inclusive
    Then connect the signal to a slot where you view->show to refresh it?

    How are you setting the model to the data? Seems like all you should have to do is re-run that code on the timer and the model/view signal slot will do what you want without further code.
    Last edited by waynew; 20th June 2010 at 21:39.

  10. The following user says thank you to waynew for this useful post:

    moviemax (24th June 2011)

  11. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to inform a Model/TableView that some data changed?

    Quote Originally Posted by Petruza View Post
    I wasn't clear enough, what I meant is if I had to call the view's signal dataChanged() or the model's signal dataChanged()
    The model doesn't have any idea about the view, or the number of views (there could be multiple views depending on the same model), so the only one you can call is the models.

  12. #10
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to inform a Model/TableView that some data changed?

    Hey Mole - doesn't seem right to me that the model doesn't know about the view.
    What about: view->setModel(model)
    and from the M/V architecture doc: "Signals from the model inform the view about changes to the data held by the data source."
    What did you mean? Maybe only the view now knows about the model, but then what about that line in the doc?

  13. #11
    Join Date
    Jun 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to inform a Model/TableView that some data changed?

    Quote Originally Posted by waynew View Post
    Hey Mole - doesn't seem right to me that the model doesn't know about the view.
    What about: view->setModel(model)
    and from the M/V architecture doc: "Signals from the model inform the view about changes to the data held by the data source."
    What did you mean? Maybe only the view now knows about the model, but then what about that line in the doc?

    I think you might just be misinterpreting the documents. The way I see it (and the way MVC is supposed to work) is that models simply inform any listeners of changes. The way this is done in Qt is that models emit signals (like void rowsInserted( const QModelIndex & parent, int start, int end) or void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight) ) and views simply connect to those. At no point is the model aware that something is connected to it, nor does it need to know.

    Models are supposed to be just that, models of some underlying data structure. The whole point of MVC is keeping the model, view and controller separate from each other.

  14. #12
    Join Date
    Jul 2010
    Location
    Cairo, Egypt
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: How to inform a Model/TableView that some data changed?

    Well, I tried something and it worked fine. Each time I update my database, I set my tableview one more time

    Qt Code:
    1. model->setTable("Contacts");
    2. model->select();
    3. ui->tableView->setModel(model);
    To copy to clipboard, switch view to plain text mode 

  15. #13
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: How to inform a Model/TableView that some data changed?

    Quote Originally Posted by Petruza View Post
    Can't I just inform the view to update exactly the cell I know has changed?
    I tried with the signal QAbstractItemModel::dataChanged, but I can't emit it from a QAbstractTableModel subclass, although it should inherit QAbstractItemModel's members, so what, aren't signals inherited?
    You should emit QAbstractItemModel::dataChanged() signal inside QAbstractItemModel::setData() function.

    So, how do you implement QAbstractItemModel::setData() function in your custom model?

    For more informations about model subclassing, you can look at Model Subclassing Reference.
    Last edited by saa7_go; 24th July 2010 at 09:12. Reason: updated contents

Similar Threads

  1. TableView/Model Display Question
    By SixDegrees in forum Qt Programming
    Replies: 9
    Last Post: 27th August 2010, 10:06
  2. Replies: 1
    Last Post: 1st February 2010, 19:42
  3. tableview model view problem
    By skuda in forum Qt Programming
    Replies: 5
    Last Post: 3rd December 2007, 15:02
  4. getting data from tableView
    By mkarakaplan in forum Newbie
    Replies: 1
    Last Post: 7th November 2007, 10:51
  5. Informing a model its data has changed
    By saknopper in forum Newbie
    Replies: 3
    Last Post: 17th January 2007, 20:58

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.