使用QStandardItemModel委托在QTreeView中擦除文本 [英] Delegate erasing text in QTreeView using QStandardItemModel

查看:211
本文介绍了使用QStandardItemModel委托在QTreeView中擦除文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将代理添加到QTreeView时遇到了一些困难. 我通过一个运行良好的模型添加了一些QStandardItems,但是当我添加委托时,将删除文本,并且仅显示图标.

I'm having some difficulty adding a delegate to my QTreeView. I have added some QStandardItems through a model which works fine, but when I add the delegate, the text is erased and only the icons are visible.

这是我为我的代表使用的代码:

This is the code i'm using for my delegate:

void SeqNavDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
       const QModelIndex &index) const
{
    if (index.column() == 0 && option.state & QStyle::State_Enabled)
    {
        const QIcon icon(QLatin1String(":/SeqNavMenu/images/green.png"));

        QRect iconRect(option.rect.right() - option.rect.height(),
                   option.rect.top(),
                   option.rect.height(),
                   option.rect.height());

        icon.paint(painter, iconRect, Qt::AlignRight);
    }
}

我想做的就是将两者结合起来,也就是说,有文本和复选框,在右边有我放置在代表中的图标.

What i would like to do is combine the two, which is to say, have the text and checkboxes, and to the right have the icons that i have put in the delegate.

也许有人可以在这里向我指出正确的方向?

Maybe someone can point me in the right direction here ?

干杯.

推荐答案

当您将委托分配给视图时,视图将自行停止渲染项目(实际上,它是由另一个委托所代替,而该委托已被您的委托替代).因此,它将渲染委托给您.然后,您对委托进行了编程,使其仅绘制图标.这就是为什么您只看到图标的原因. 如果您还需要绘制一个复选框和一个文本,则需要您自己绘制它,或者在实现中的某个地方调用祖先方法paint.因此,如果您从QStyledItemDelegate调用继承了SeqNavDelegate:

When you assign a delegate to a view, the view stops rendering items by itself (actually it does it with another delegate which is replaced by yours). So it delegates the rendering to you. And you programed the delegate to draw icons only. Thats why you see only icons.
If you need to draw a check-box and a text as well you need to draw it by yourself or call the ancestors method paint somewhere in your implementation. So if you inherited SeqNavDelegate from QStyledItemDelegate call :

QStyledItemDelegate::paint(painter, option, index);

这篇关于使用QStandardItemModel委托在QTreeView中擦除文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆