No need to go with abstract delegate. Styled delegate does most of the work you need. Use it and reimplement only needed behaviour.
.h:
#include <QStyledItemDelegate>
class MyDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit MyDelegate(QObject *parent = 0);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
private:
bool shouldBeBold(const QModelIndex &index);
}
.cpp:
MyDelegate::MyDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
QVariant data = index.data(...); // pick the data you need here
opt.font.setBold(shouldBeBold(data));
QStyledItemDelegate::paint(painter, opt, index);
}
bool MyDelegate::shouldBeBold(const QModelIndex &index)
{
// you need to implement this
}
Then apply delegate to the view. If shouldBeBold()
returns false, delegate will paint like a standard one. If it returns true, it will apply bold font.
I hope that's enought for you to get started.
Qt::ForegroundRole
we can return just theQColor
? From documentation what I understand is we can returnQBrush
. The link where I found this information Doc Link The doc states -Qt::ForegroundRole The foreground brush (text color, typically) used for items rendered with the default delegate. (QBrush)
– Martinsen