如何使用QStyledItemDelegate绘制不同的背景?

mos*_*osg 3 qt qt4 qstyleditemdelegate

问题:

  • 我有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;
}
Run Code Online (Sandbox Code Playgroud)
  • 然后我创建了自己的委托,继承自QStyledItemDelegate类,重新实现paint方法,我不能只改变背景,因为QStyledItemDelegate::paint( painter, opt, index );代码会透支我的drawPixmap...

例:

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 );
}
Run Code Online (Sandbox Code Playgroud)

所以我被困了......

mos*_*osg 6

这是我的诀窍:

Designer的样式表的一部分:

QTreeView#treeView
{
    border: none;
    background-color:#f0f0f1;
}   

QTreeView#treeView::item,
QTreeView#treeView::branch
{
    height: 25px;
    border: none;
    color: #564f5b;
}

QTreeView#treeView::item:selected,
QTreeView#treeView::branch:selected
{
    border-bottom: none;
    color: #ffffff;    
    background-image: url(:/backgrounds/images/backgrounds/kontragents_row_selection.png);
    background-position: top left;  

}

QTreeView#treeView::item:selected:!active,
QTreeView#treeView::branch:selected:!active
{
    color: #ffffff;
}
Run Code Online (Sandbox Code Playgroud)

委托重新实现的paint()方法:

void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
 {
      QStyleOptionViewItemV4 opt = option; // ??? ?????? QTBUG-4310
      opt.state &= ~QStyle::State_HasFocus; // ????? ?? ????????? ????????????? ??????

      QBrush brush = opt.backgroundBrush;
      brush.setTexture( QPixmap( index.data( SORT_ROLE ).toBool()
           ? BACKGROUND_HIGH_PRIORITY
           : BACKGROUND_STANDARD ) );

      // FILL BACKGROUND     
      painter->save();
      painter->fillRect( opt.rect, brush );
      painter->restore();

      // DRAW ICON & TEXT
      QStyledItemDelegate::paint( painter, opt, index );

      // IF ( CHILD ) THEN PAINT OVER ONLY! BRANCH RECT
      bool isIndexParent = !index.parent().isValid();
      if( !isIndexParent )
      {
           QRect rect( 0, opt.rect.y(), 20, opt.rect.height() );

           if( opt.state & QStyle::State_Selected )
           {
                brush.setTexture( QPixmap( BACKGROUND_SELECTED ) );
           }

           painter->save();
           painter->fillRect( rect, brush );
           painter->restore();
      }
 }
Run Code Online (Sandbox Code Playgroud)

结果QTreeView视图:

在此输入图像描述

祝你今天愉快!:)

PS:不需要重绘图标,文字,选择......