Results 1 to 13 of 13

Thread: Set the color of a row in a qtreeview

  1. #1
    Join Date
    Feb 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Set the color of a row in a qtreeview

    Is it possible to change the color of a row in a QTreeView based on an index at runtime?

  2. #2
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set the color of a row in a qtreeview

    Something like this?

    Qt Code:
    1. void MainWindow::changeTreeViewRowColor(QModelIndex index)
    2. {
    3. for( int i = 0; i < model.columnCount(); ++i ) {
    4. model.item( index.row(), i )->setBackground( Qt::red );
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2012
    Location
    Warsaw, Poland
    Posts
    37
    Thanks
    3
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Set the color of a row in a qtreeview

    Inherit QStyledItemDelegate.

  4. #4
    Join Date
    Feb 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set the color of a row in a qtreeview

    Quote Originally Posted by atomic View Post
    Something like this?

    Qt Code:
    1. void MainWindow::changeTreeViewRowColor(QModelIndex index)
    2. {
    3. for( int i = 0; i < model.columnCount(); ++i ) {
    4. model.item( index.row(), i )->setBackground( Qt::red );
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    exactly like that, except QDirModel and QFileSystemModel don't have an "item()" method...

  5. #5
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set the color of a row in a qtreeview

    Try reimplement this virtual function:

    Qt Code:
    1. bool QFileSystemModel::setData(const QModelIndex & idx, const QVariant & value, int role = Qt::EditRole)
    To copy to clipboard, switch view to plain text mode 

    http://doc.qt.io/qt-5/qfilesystemmodel.html#setData
    http://doc.qt.io/qt-5/qabstractitemmodel.html#setData

  6. #6
    Join Date
    Feb 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set the color of a row in a qtreeview

    reimplement it how?

    from this post here : http://www.cplusplusdevelop.com/855_3935440/

    QFileSystemModel rejects all calls to setData that is not of the role "Qt::EditRole".
    I tried using setData() and Qt::BackgroundRole with QSortFilterProxyModel instead, it still doesn't do anything. I am not sure what exactly I should reimplement setData to do.
    Last edited by slv1; 27th February 2015 at 09:46.

  7. #7
    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: Set the color of a row in a qtreeview

    Quote Originally Posted by slv1 View Post
    reimplement it how?

    from this post here : http://www.cplusplusdevelop.com/855_3935440/



    I tried using setData() and Qt::BackgroundRole with QSortFilterProxyModel instead, it still doesn't do anything. I am not sure what exactly I should reimplement setData to do.
    Implement a custom delegate or a proxy model that will do the colouring. Reimplementing setData() for QDirModel or QFileSystemModel makes little sense.
    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.


  8. #8
    Join Date
    Feb 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set the color of a row in a qtreeview

    I implemented a custom QStyledItemDelegate() but :

    When I iterate through the items in my model, I decide which delegate method should be used for each item, but then the actual drawing takes place after the loop is over, so the last call of "view->setItemDelegate()" will be considered.

    How do I force a repaint of a specific item after setting the right itemDelegate for the view?

    Update:
    Tried using setItemDelegateForRow(), that doesn't work properly because I'm using a QTreeView and that doesn't have unique rows. What would work is a setItemDelegateForIndex() method......
    Last edited by slv1; 27th February 2015 at 15:47.

  9. #9
    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: Set the color of a row in a qtreeview

    You should set one delegate for the whole view and teach it to decide about colour of each row it renders.
    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.


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

    slv1 (3rd March 2015)

  11. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Set the color of a row in a qtreeview

    You should set one delegate for the whole view and teach it to decide about colour of each row it renders.
    Can you please explain the pros and cons of doing it this way vs. implementing a proxy that would change the colour via the data() method and the Qt::BackgroundRole. In both cases, it would seem that "teach it to decide" would require inside knowledge about the model.

  12. #11
    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: Set the color of a row in a qtreeview

    Quote Originally Posted by d_stranz View Post
    Can you please explain the pros and cons of doing it this way vs. implementing a proxy that would change the colour via the data() method and the Qt::BackgroundRole. In both cases, it would seem that "teach it to decide" would require inside knowledge about the model.
    They are more or less equivalent. Using a custom delegate is a bit faster performance-wise.
    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.


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

    d_stranz (28th February 2015)

  14. #12
    Join Date
    Feb 2015
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Set the color of a row in a qtreeview

    Quote Originally Posted by wysota View Post
    You should set one delegate for the whole view and teach it to decide about colour of each row it renders.
    I solved this by writing a "QStyledItemDelegate" that takes a list of indexes as arguments. It checks if the current index is in the list and paints accordingly.

    Here's the code:

    Qt Code:
    1. class RedRowDelegate : public QStyledItemDelegate
    2. {
    3. public:
    4. QDirModel *pModel;
    5. QList<QModelIndex> *pRedIndexes;
    6.  
    7. RedRowDelegate::RedRowDelegate(QDirModel *model, QList<QModelIndex> *redIndexes){
    8. pModel = model;
    9. pRedIndexes = redIndexes;
    10. }
    11.  
    12. void RedRowDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    13. {
    14. int row = index.row();
    15.  
    16. if(pRedIndexes->contains(index)){
    17. painter->fillRect(option.rect, QColor(Qt::red));
    18. }
    19.  
    20. QStyledItemDelegate::paint(painter, option, index);
    21. }
    22. };
    To copy to clipboard, switch view to plain text mode 

    After setting it for the QTreeView like this:
    Qt Code:
    1. tree1->setItemDelegate(new RedRowDelegate(model, redIndexes));
    To copy to clipboard, switch view to plain text mode 
    , the delegate will be called iteratively for all the items in the tree, instead of the default one.

  15. #13
    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: Set the color of a row in a qtreeview

    You should be using QPersistentModelIndex instead of QModelIndex.
    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.


Similar Threads

  1. Replies: 1
    Last Post: 14th April 2011, 07:34
  2. Qt4.7.0 Alternate Row Color from QSS ignored (QTreeView)
    By DancingFirefly in forum Qt Programming
    Replies: 2
    Last Post: 15th February 2011, 23:12
  3. QTreeView item color
    By tmmak in forum Qt Programming
    Replies: 1
    Last Post: 11th January 2011, 07:08
  4. bg color of current item for a QTreeView
    By joeld42 in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2009, 18:06
  5. About the background color of QTreeView itmes
    By yandy in forum Qt Programming
    Replies: 3
    Last Post: 26th March 2009, 16:38

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.