问题
Here is the main problem: I'm using QTreeWidget
class as a main tree, which must show me this tree structure:
[Today]
[Row1]
[Row2]
[SubRow21]
[SubRow22]
[Row3]
[Yesterday]
[Row4]
[SubRow41]
[etc]
With Qt Designer
I have set this style sheet code:
QTreeWidget#treeWidget::item
{
height: 24px;
border: none;
background-position: bottom left;
background-image: url(:/backgrounds/images/backgrounds/row_back.png);
}
QTreeWidget#treeWidget::item:selected
{
color: #000000;
background-position: bottom left;
background-image: url(:/spreadsheet/images/spreadsheet/row_back_selected.png);
}
And all items drawn with the *row_back.png* background image, but I need to draw another background image to [Today] and [Yesterday] rows! For these purposes I had inherit QStyledItemDelegate
class, but don't know methods, to reset style to new background image:
void MyColumnDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
QStyleOptionViewItemV4 op = option;
if( isPeriod( index ) )
{
// here I heed to change background image!!!
//QStyle* style = op.widget ? op.widget->style() : QApplication::style();
//style->drawControl( QStyle::CE_ItemViewItem, &op, painter, op.widget );
//QRect backgroundRect( option.rect.x() + 5, option.rect.y() + 5, 18, 18 );
//style->drawItemPixmap( painter, backgroundRect, Qt::AlignCenter, QPixmap( SOME_PIC ) );
//QStyledItemDelegate::paint( painter, op, index );
return;
}
op.text = "";
QStyle* style = op.widget ? op.widget->style() : QApplication::style();
style->drawControl( QStyle::CE_ItemViewItem, &op, painter, op.widget );
QModelIndex dataIndex = index.model()->index( index.row(), ePosChain, index.parent() );
LetterInfo data = index.model()->data( dataIndex, PackageDataRole ).value< LetterInfo >();
switch( data.document_.state_ )
{
case( eStateSignedByOwner ):
painter->drawPixmap( getPos( op, index, QPixmap( IMAGE2 ) ), QPixmap( IMAGE2 ) );
break;
case( eStateSignedByHost ):
painter->drawPixmap( getPos( op, index, QPixmap( IMAGE1 ) ), QPixmap( IMAGE1 ) );
break;
}
painter->drawText( getTextPos( op, index ), data.text );
}
Any help?
Thanks!
回答1:
Solved with this code:
if( isPeriod( index ) )
{
QPixmap pixmap( PERIOD_ROW_BACKGROUND );
painter->drawPixmap( op.rect, pixmap, pixmap.rect() );
return;
}
来源:https://stackoverflow.com/questions/8179037/howto-draw-qtreewidgetitem-with-different-style