如何绘制不同风格的QTreeWidgetItem?

发布于 2024-12-16 20:34:14 字数 2420 浏览 3 评论 0 原文

这是主要问题:我使用 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 );
}

有帮助吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2024-12-23 20:34:14

用这段代码解决了:

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

          return;
     }

Solved with this code:

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

          return;
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文