如何在 QTreeWidget 中使用委托绘制 ico+text 数据(选择行时)?

发布于 2024-12-10 01:17:11 字数 1881 浏览 0 评论 0原文

问题:当我用鼠标在桌子上选择一行时,图片 #1 变成图片 #2 - 如您所见,我的所有绘制的图标消失了!

问题:有人可以帮我解决这个问题吗?也许有人已经陷入了这个困境并解决了它?谢谢!

图片 #1

在此处输入图像描述

图片 #2

在此处输入图像描述

附加:我使用 QTreeWidget 作为表格一些数据(隐藏根)。我向 QTreeWidget 对象附加列委托(针对 1、2 和 3 列,但不针对 4! 列)。在所有委托(基于QStyledItemDelegate类)中,我重新实现了paint()方法,以绘制我的特定图标或文本数据。

这是其中一位代表的代码(1 列) - 它是某种链,一些项目组合在一起(父项 + 子项):

void ChainTableDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
     QStyleOptionViewItem op = option;
     op.state &= ~QStyle::State_HasFocus;

     if( index.column() == TreeView::ePosChain )
     {
          QModelIndex parentIndex = index.parent();

          if( !childCount( index ) && !parentIndex.isValid() )
          {
               QStyledItemDelegate::paint( painter, op, index );
               return;
          }

          if( !parentIndex.isValid() )
          {
               // top
               painter->drawPixmap( pos( op, topActivePix_ ), topActivePix_ );
          }
          else
          {
               int row = index.row();
               if( row != childCount( parentIndex ) - 1 )
               {
                    // middle
                    painter->drawPixmap( pos( op, middleActivePix_ ), middleActivePix_ );
               }
               else
               {
                    // bottom
                    painter->drawPixmap( pos( op, bottomActivePix_ ), bottomActivePix_ );
               }
          }
     }

     QStyledItemDelegate::paint( painter, op, index );
}

Problem: When I select a row with a mouse on the table, pic #1 becomes pic #2 - as you can see, all my drawn icons are disappear!

Question: could anyone help me with this problem? Maybe some one already fall in with this dilemma and solve it? Thnks!

pic #1

enter image description here

pic #2

enter image description here

Additional: I'm using QTreeWidget as a table with some data (hidden root). To QTreeWidget object I append column delegates (for 1, 2 and 3 columns, but not for the 4! column). In all delegates (which are based on QStyledItemDelegate class) I have re implement paint() method, to draw my specific icons or text data.

Here is the code of one of the delegates (1 column) - it's some kind of chain, some items grouped together (parent + childs):

void ChainTableDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
     QStyleOptionViewItem op = option;
     op.state &= ~QStyle::State_HasFocus;

     if( index.column() == TreeView::ePosChain )
     {
          QModelIndex parentIndex = index.parent();

          if( !childCount( index ) && !parentIndex.isValid() )
          {
               QStyledItemDelegate::paint( painter, op, index );
               return;
          }

          if( !parentIndex.isValid() )
          {
               // top
               painter->drawPixmap( pos( op, topActivePix_ ), topActivePix_ );
          }
          else
          {
               int row = index.row();
               if( row != childCount( parentIndex ) - 1 )
               {
                    // middle
                    painter->drawPixmap( pos( op, middleActivePix_ ), middleActivePix_ );
               }
               else
               {
                    // bottom
                    painter->drawPixmap( pos( op, bottomActivePix_ ), bottomActivePix_ );
               }
          }
     }

     QStyledItemDelegate::paint( painter, op, index );
}

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

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

发布评论

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

评论(1

窝囊感情。 2024-12-17 01:17:11

我认为你应该首先调用父方法,然后绘制像素图:)

否则,你只会用高亮效果覆盖刚刚绘制的图标

干杯

I think you should first call the parent method and then draw the pixmap :)

Otherwise, you'll just overwrite the icon you've just drawn with the highlight effect

Cheers

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