开发者

HowTo draw ico+text data with delegate in QTreeWidget (when selecting a row)?

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

HowTo draw ico+text data with delegate in QTreeWidget (when selecting a row)?

pic #2

HowTo draw ico+text data with delegate in QTreeWidget (when selecting a row)?

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 )
     {
          QMo开发者_开发百科delIndex 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 );
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜