Is it possible to change the color of a row in a QTreeView based on an index at runtime?
Is it possible to change the color of a row in a QTreeView based on an index at runtime?
Something like this?
Qt Code:
{ for( int i = 0; i < model.columnCount(); ++i ) { model.item( index.row(), i )->setBackground( Qt::red ); } }To copy to clipboard, switch view to plain text mode
Inherit QStyledItemDelegate.
Try reimplement this virtual function:
Qt Code:
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
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.QFileSystemModel rejects all calls to setData that is not of the role "Qt::EditRole".
Last edited by slv1; 27th February 2015 at 08:46.
Implement a custom delegate or a proxy model that will do the colouring. Reimplementing setData() for QDirModel or QFileSystemModel makes little sense.
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 14:47.
You should set one delegate for the whole view and teach it to decide about colour of each row it renders.
slv1 (3rd March 2015)
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.You should set one delegate for the whole view and teach it to decide about colour of each row it renders.
d_stranz (28th February 2015)
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:
class RedRowDelegate : public QStyledItemDelegate { public: QDirModel *pModel; QList<QModelIndex> *pRedIndexes; pModel = model; pRedIndexes = redIndexes; } void RedRowDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { int row = index.row(); if(pRedIndexes->contains(index)){ } QStyledItemDelegate::paint(painter, option, index); } };To copy to clipboard, switch view to plain text mode
After setting it for the QTreeView like this:, the delegate will be called iteratively for all the items in the tree, instead of the default one.Qt Code:
tree1->setItemDelegate(new RedRowDelegate(model, redIndexes));To copy to clipboard, switch view to plain text mode
You should be using QPersistentModelIndex instead of QModelIndex.
Bookmarks