QTreeWidgetItem颜色
我正在QTreeWidget上使用以下样式表来更改items样式:QTreeWidgetItem颜色,qt,qt5,qtreewidget,qtreewidgetitem,Qt,Qt5,Qtreewidget,Qtreewidgetitem,我正在QTreeWidget上使用以下样式表来更改items样式: QTreeWidget::item { padding-left:10px; padding-top: 1px; padding-bottom: 1px; border-left: 10px; } 之后,我尝试使用以下代码更改某些特定单元格的颜色: // item is a QTreeWidgetItem item->setBackgroundColor(1, QColor(255, 12
QTreeWidget::item
{
padding-left:10px;
padding-top: 1px;
padding-bottom: 1px;
border-left: 10px;
}
之后,我尝试使用以下代码更改某些特定单元格的颜色:
// item is a QTreeWidgetItem
item->setBackgroundColor(1, QColor(255, 129, 123));
但是颜色没有改变。然后我发现,如果我从QTreeWidget中删除样式表,那么颜色的改变就会起作用
您知道如何更改背景颜色以保持样式表吗?使用自定义委托来绘制项目,而不是样式表
重新实现paint()
方法以控制项目的绘制方式:
class CMyDelegate : public QStyledItemDelegate
{
public:
CMyDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
void CMyDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex & index) const override;
}
void CMyDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
QStyleOptionViewItemV4 itemOption(option)
initStyleOption(&itemOption, index);
itemOption.rect.adjust(-10, 0, 0, 0); // Make the item rectangle 10 pixels smaller from the left side.
// Draw your item content.
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &itemOption, painter, nullptr);
// And now you can draw a bottom border.
painter->setPen(Qt::black);
painter->drawLine(itemOption.rect.bottomLeft(), itemOption.rect.bottomRight());
}
下面是如何使用您的委托:
CMyDelegate* delegate = new CMyDelegate(tree);
tree->setItemDelegate(delegate);
这里有更多文档:我已经准备好了,谢谢。现在我必须阅读文档才能理解它:)我现在的问题是
resizeColumnToContents
工作不好;我看看我是否能解决这个问题。