Problem:
- I have
QTreeView
object, and aQStandardItemModel
as model to view widget; - For some item's I have set data with
setData
method to split them with a parameter; - So I need to draw different background pixmap for
QStandardItem
items, which are with icon's and some text data; - And don't want to redraw all the items objects, I mean icon and text. Just change background.
First, I was thinking that:
- I could set CSS stylesheets in
Qt Designer
for the object with 2 different background pictures, BUTQStandardItem
doesn't havesetProperty
method...
Example:
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;
}
- then I created my own delegate, inherited from
QStyledItemDelegate
class, and reimplementpaint
method, BUT I can't just change background, becauseQStyledItemDelegate::paint( painter, opt, index );
code will overdraw mydrawPixmap
...
Example:
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 );
}
So I'm stuck...
Here is my trick:
The
Designer
's stylesheet part:Delegate reimplemented
paint()
method:Resulting
QTreeView
view:Have a nice day! :)
PS: no need redrawing icons, text, selection...