【问题标题】:HowTo draw QTreeWidgetItem with different style?如何绘制不同风格的 QTreeWidgetItem?
【发布时间】:2012-01-01 00:24:57
【问题描述】:

这是主要问题:我正在使用QTreeWidget 类作为主树,它必须向我展示这个树结构:

[Today]
   [Row1]
   [Row2]
      [SubRow21]
      [SubRow22]
   [Row3]
[Yesterday]
   [Row4]
      [SubRow41]
[etc]

使用 Qt Designer 我已经设置了这个样式表代码:

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);
}

并且所有项都使用 *row_back.png* 背景图像绘制,但是我需要为 [今天] 绘制另一个背景图像和 [昨天] 行!出于这些目的,我继承了 QStyledItemDelegate 类,但不知道将样式重置为新背景图像的方法:

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 );
}

有什么帮助吗?

谢谢!

【问题讨论】:

    标签: qt qt4 qtreewidget qstyle


    【解决方案1】:

    用这段代码解决了:

    if( isPeriod( index ) )
         {
              QPixmap pixmap( PERIOD_ROW_BACKGROUND );
              painter->drawPixmap( op.rect, pixmap, pixmap.rect() );
    
              return;
         }
    

    【讨论】:

      猜你喜欢
      • 2011-06-07
      • 1970-01-01
      • 2019-03-27
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      相关资源
      最近更新 更多