Results 1 to 3 of 3

Thread: QAbstractTreeModel & dataChanged()

  1. #1
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QAbstractTreeModel & dataChanged()

    Hi,

    I'm trying to update a QTreeView control when data inside its model is changed. I don't mean edited from the control directly, but instead either changed or added to the model from another point in the program.

    So in my QAbstractTreeModel I have

    Qt Code:
    1. void queue::addJob(...)
    2. {
    3. ...
    4. // Update model data
    5.  
    6. QModelIndex x = this->index(jobs.size(), 0, QModelIndex());
    7. QModelIndex y = this->index(jobs.size(), 5, QModelIndex());
    8.  
    9. // Update the new row
    10. emit dataChanged(x, y);
    11. }
    To copy to clipboard, switch view to plain text mode 

    This however does nothing when data is added to the model. I have implemented: rowCount(), columnCount(), headerData() and data().

    Please could someone tell me what im doing it wrong, do I have to implement setData()? Do I have to call insertRows() to add a new row, or does rowCount do this job for me?

    emit reset() works, but thats clearly an inefficient approach with lots of data.

    Many thanks,

    Jack
    Last edited by tntcoda; 2nd June 2009 at 23:32.

  2. #2
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QAbstractTreeModel & dataChanged()

    You have to call QAbstractItemModel::beginInsertRows() before you start adding your data and QAbstractItemModel::endInsertRows() afterwards:

    Qt Code:
    1. void queue::addJob(...)
    2. {
    3. beginInsertRows(...);
    4. // Update model data
    5. endInsertRows();
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    tntcoda (3rd June 2009)

  4. #3
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QAbstractTreeModel & dataChanged()

    Thanks very much, works perfectly now.

Similar Threads

  1. dataChanged does not re-sort the view
    By alex9099 in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2008, 20:14
  2. questions about datachanged() in model/view programming
    By calmspeaker in forum Qt Programming
    Replies: 1
    Last Post: 8th September 2008, 23:48

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.