如何在 QTreeWidget 中使用委托绘制 ico+text 数据(选择行时)?
问题:当我用鼠标在桌子上选择一行时,图片 #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
pic #2
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该首先调用父方法,然后绘制像素图:)
否则,你只会用高亮效果覆盖刚刚绘制的图标
干杯
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