Results 1 to 18 of 18

Thread: QTreeView repaint

  1. #1
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTreeView repaint

    I am updating some data that is displayed in a QTreeView, but the change in the data it is not being displayed. I have tried explicit calls to repaint(), update(), dataChanged(), (on both the QtreeView and the containing window) plus a few others but no joy.

    However if I click on any item in the QTreeView, or on another window then the change is updated, so I know that the change is in my model. Does anyone have any ideas on how I can get the QTreeView to automatically display the new state?

    thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    Do you use a custom model with that QTreeView?

  3. #3
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    Yes I do have a custom model.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    Quote Originally Posted by graeme
    Yes I do have a custom model.
    Does it emit dataChanged() signal?

    void QAbstractItemModel::dataChanged ( const QModelIndex & topLeft, const QModelIndex & bottomRight ) [signal]
    This signal is emitted whenever the data in an existing item changes. The affected items are those between topLeft and bottomRight inclusive (of the same parent).
    Note that this signal must be emitted explicitly when reimplementing the setData() function.
    See also headerDataChanged(), setData(), and layoutChanged().

  5. #5
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    I do not use the model to change the data. My window has a QTreeView, when an item is selected the details are displayed in a separate area in edit boxes. If these values are changed then the underlying data items are changed. Because the model referes to these via pointers the change is in the model. But I can't get the QTreeView to repaint itself.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    But does this model emit dataChanged() signal when you change the data?

  7. #7
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    I don't use the setData() because I'm not changing it from within the model but I did try and fire off the dataChanged() from the window but that didn't work. However I can try and see what happens if I do it via the setData.

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTreeView repaint

    You will have to do the changes through the model, or at least inform the view somehow that the model has been changed. Otherwise the view has no way of knowing about data changes.

    Check this thread.
    J-P Nurmi

  9. #9
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    How do I inform the view that the data has changed?

    I've tried repaint(), update(), and dataChanged() but none appear to work. But as soon as I click to another window (even if it doesn't overlap) then the change is reflected.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    Quote Originally Posted by graeme
    I've tried repaint(), update(), and dataChanged() but none appear to work.
    How and when did you try to emit the dataChanged() signal?

  11. #11
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    This is how I sent the signal

    Qt Code:
    1. wdgt.trvModules->dataChanged(wdgt.trvModules->currentIndex(),wdgt.trvModules->currentIndex());
    To copy to clipboard, switch view to plain text mode 

    I had it right at the end of the method that changes the data. I have also added a call to setData in the same place.

    Qt Code:
    1. wdgt.trvModules->model()->setData(wdgt.trvModules->currentIndex(),classSize);
    2.  
    3. wdgt.trvModules->dataChanged(wdgt.trvModules->currentIndex(),wdgt.trvModules->currentIndex());
    To copy to clipboard, switch view to plain text mode 

    Where wdgt.trvModules is a pointer to teh QTreeView and I'm changing the selected (singular) item.

  12. #12
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    I have also tried the following:

    Qt Code:
    1. bool RootModel::setData ( const QModelIndex & index, const QVariant & value, int role)
    2. {
    3. if (!index.isValid())
    4. return false;
    5.  
    6. if (role != Qt::EditRole)
    7. return false;
    8.  
    9. RootItem *item = getRootItem(index);
    10. item->setData(index.column(),value);
    11. emit dataChanged(index, index);
    12. return true;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Where item->setData() changes the value held in the item. Again if I click on an item it will reflect the change (on on another window) but not otherwise.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    Quote Originally Posted by graeme
    This is how I sent the signal

    Qt Code:
    1. wdgt.trvModules->dataChanged(wdgt.trvModules->currentIndex(),wdgt.trvModules->currentIndex());
    To copy to clipboard, switch view to plain text mode 
    You didn't send the QAbstractItemModel::dataChanged() signal, instead you have invoked the QAbstractItemView::dataChanged() protected slot.

    Although it should have a similar effect. Are you sure that wdgt.trvModules->currentIndex() is valid?

    Quote Originally Posted by graeme
    I have also added a call to setData in the same place.
    Forget about setData() --- it doesn't do anything, if you didn't implement it.

  14. #14
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    Tweeking with the setData I have the following which works...

    Qt Code:
    1. bool RootModel::setData ( const QModelIndex & index, const QVariant & value, int role)
    2. {
    3. if (!index.isValid())
    4. return false;
    5.  
    6. if (role != Qt::EditRole)
    7. return false;
    8.  
    9. beginInsertRows(index.parent(), rowCount(index.parent()), rowCount(index.parent()));
    10. RootItem *item = getRootItem(index);
    11. item->setData(index.column(),value);
    12. emit dataChanged(index, index);
    13. endInsertRows();
    14. return true;
    15. }
    To copy to clipboard, switch view to plain text mode 

    By telling it that I am inserting some rows at the end it will then repaint the view. This doesn't look right to me but it does work.

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    Quote Originally Posted by graeme
    emit dataChanged(index, index);
    Maybe you should try to emit that signal for the whole row?

    Try something like:
    Qt Code:
    1. emit dataChanged( index( row, 0 ), index( row, nCols-1 ) );
    To copy to clipboard, switch view to plain text mode 

  16. The following user says thank you to jacek for this useful post:

    graeme (3rd May 2006)

  17. #16
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    I tried that and as many variants that I could think of but no luck.
    Finishing with:
    Qt Code:
    1. emit dataChanged(index( 0, 0, pIndex.parent())
    2. , index( rowCount(), item->columnCount()-1,pIndex.parent() ));
    To copy to clipboard, switch view to plain text mode 

    Where pIndex is the index of the selected item.

    I'd also tried with the row being pIndex.row(), with and without the last argument to index. But I still can't get it to repaint.

  18. #17
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView repaint

    Eat my own words...
    On the last tests I wasn't actually calling the setData() method. Once I did that it worked. I have now been able to move it back out of the setData() method and just emit the dataChanged() signal via a new method of the model class and it works.

    So essentially I needed to call the whole row as part of the dataChanged() signal.

    Thank you very much jacek for your assistance.

  19. #18
    Join Date
    Mar 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView repaint

    you can replace standart model by QSortFilterProxyModel and use public slot QSortFilterProxyModel::invalidate().

Similar Threads

  1. Direct root childs inline QTreeView
    By faraslacks in forum Qt Programming
    Replies: 2
    Last Post: 15th January 2009, 07:45
  2. QDirModel and QTreeView cut and paste
    By Micawber in forum Qt Programming
    Replies: 4
    Last Post: 28th May 2008, 20:16
  3. QTreeView help
    By bepaald in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2007, 21:22
  4. QTreeView: Holding a line on screen
    By gri in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 11:42
  5. background image in QTreeView
    By momesana in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 06:25

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.