【发布时间】:2012-05-25 16:16:06
【问题描述】:
问题:
- 我有
QTreeView对象和一个QStandardItemModel作为模型来查看小部件; - 对于某些项目,我使用
setData方法设置数据,以使用参数对其进行拆分; - 所以我需要为
QStandardItem项目绘制不同背景像素图,其中包含图标和一些文本数据; - 并且不想重绘所有项目对象,我的意思是图标和文本。只需更改背景即可。
首先,我在想:
- 我可以在
Qt Designer中为具有 2 个不同背景图片的对象设置 CSS 样式表,但是QStandardItem没有setProperty方法...
例子:
QTreeView#treeView::item[ROLE="AAA"],
QTreeView#treeView::branch[ROLE="AAA"]
{
height: 25px;
border: none;
color: #564f5b;
background-image: url(:/backgrounds/images/row1.png);
background-position: top left;
}
QTreeView#treeView::item[ROLE="BBB"],
QTreeView#treeView::branch[ROLE="BBB"]
{
height: 25px;
border: none;
color: #564f5b;
background-image: url(:/backgrounds/images/row2.png);
background-position: top left;
}
- 然后我创建了自己的委托,继承自
QStyledItemDelegate类,并重新实现paint方法,但是我不能只更改背景,因为QStyledItemDelegate::paint( painter, opt, index );代码会透支我的@987654332 @...
例子:
QStyleOptionViewItemV4 opt = option; // Для обхода QTBUG-4310
opt.state &= ~QStyle::State_HasFocus; // Чтобы не рисовался прямоугольник фокуса
QStyledItemDelegate::paint( painter, opt, index );
// HERE I WANT TO CHANGE BACKGROUND (DEFAULT IS ALREADY SET IN DESIGNER WITH ABOVE CODE)
if( index.data( SORT_ROLE ).toBool() )
{
const QPixmap pixmap( ":/backgrounds/images/backgrounds/contractor_row__high_priority.png" );
painter->drawPixmap( option.rect, pixmap, pixmap.rect() );
QStyledItemDelegate::paint( painter, opt, index );
}
所以我被卡住了......
【问题讨论】:
标签: qt qt4 qstyleditemdelegate