
Originally Posted by
wysota
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:
class RedRowDelegate : public QStyledItemDelegate
{
public:
QList<QModelIndex> *pRedIndexes;
RedRowDelegate
::RedRowDelegate(QDirModel *model, QList<QModelIndex>
*redIndexes
){ pModel = model;
pRedIndexes = redIndexes;
}
{
int row = index.row();
if(pRedIndexes->contains(index)){
painter
->fillRect
(option.
rect,
QColor(Qt
::red));
}
QStyledItemDelegate::paint(painter, option, index);
}
};
class RedRowDelegate : public QStyledItemDelegate
{
public:
QDirModel *pModel;
QList<QModelIndex> *pRedIndexes;
RedRowDelegate::RedRowDelegate(QDirModel *model, QList<QModelIndex> *redIndexes){
pModel = model;
pRedIndexes = redIndexes;
}
void RedRowDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
int row = index.row();
if(pRedIndexes->contains(index)){
painter->fillRect(option.rect, QColor(Qt::red));
}
QStyledItemDelegate::paint(painter, option, index);
}
};
To copy to clipboard, switch view to plain text mode
After setting it for the QTreeView like this:
tree1->setItemDelegate(new RedRowDelegate(model, redIndexes));
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.
Bookmarks