Qtreewidget项将颜色更改为Qtreewidgetitem中的图标
如何以友好方式更改qtreewidgetitem内的背景图标: 一些代码示例Qtreewidget项将颜色更改为Qtreewidgetitem中的图标,qt,qtreewidgetitem,qicon,Qt,Qtreewidgetitem,Qicon,如何以友好方式更改qtreewidgetitem内的背景图标: 一些代码示例 if item.text(0)=="INL" item.icon(0).setBackground(Qt.green) else: item.icon(0).setBackground(Qt.yellow) 我只想要图标背景,而不是所有项目(图标+文本)。。 使用并覆盖该方法 范例 h文件 class MyItemDelegate : public QItemDelegate { public:
if item.text(0)=="INL"
item.icon(0).setBackground(Qt.green)
else:
item.icon(0).setBackground(Qt.yellow)
我只想要图标背景,而不是所有项目(图标+文本)。。
使用并覆盖该方法
范例
h文件
class MyItemDelegate : public QItemDelegate
{
public:
MyItemDelegate(QObject *parent = Q_NULLPTR);
void paint ( QPainter * painter, const QStyleOptionViewItem & oStyleOption, const QModelIndex & index ) const;
}
cpp
要将项目设置为表格,请使用您可以使用字体图标并更改其颜色
void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &oStyleOption, const QModelIndex &index) const
{
// Apply for column 0
if (index.column() == 0) {
// background color
Qt::GlobalColor eColor;
// Get table data
if (index.model()->data(index).toString() == "INL")
eColor = Qt::green;
else
eColor = Qt::yellow;
painter->save();
// background rect size (icon size 16x16 + padding)
QRect oRect(oStyleOption.rect.x() + 2, oStyleOption.rect.y() + 6 , 16, 16);
// background color
painter->fillRect(oRect, eColor);
painter->restore();
}
return QItemDelegate::paint(painter,oStyleOption,index);
}