我正在使用自定义sortfilterproxymodel(它将另一个自定义模型作为源)和自定义委托(覆盖绘制)显示TreeView以影响每个项目的显示.
但是,我无法显示TreeView的标题.我查看了代理和普通模型,并且都调用了headerData()并返回正确的值.我没有明确隐藏标题.实际上,我明确地将()将TreeView的头部和setHeaderHidden()显示为false.
什么可能导致标题没有显示?
这是委托的paint()函数,因为我怀疑那里的错误:
//--------------------------------------------------------------------------------- void MyDelegate::paint(QPainter* p_painter, const QStyleOptionViewItem& p_option, const QModelIndex& p_index) const { // Get a custom text QString text = ""; // Code that changes the text variable, nothing fancy, no pre-mature return // Left out for convenience // Call painter methods for drawing p_painter->save(); p_painter->setClipRect(p_option.rect); drawBackground(p_painter, p_option, p_index); drawDisplay(p_painter, p_option, p_option.rect, text); drawFocus(p_painter, p_option, p_option.rect); p_painter->restore(); }
如果你想知道我为什么要手动完成所有画家的东西(save(),drawBackground等),那是因为它似乎是改变paint()函数中显示文本的唯一方法.至少我能想到的唯一一个.但我不知道这是否与视图中未显示的标题有关.
编辑:到现在为止,我尝试用默认的涂料替换自己的涂料.标题仍未显示,因此paint()方法似乎是无辜的;)
问题是我"忘记"将以下内容添加到headerData()函数的开头:
if (role != Qt::DisplayRole) return QVariant();
虽然我不得不说你必须拥有这些线才能显示任何东西,这有点奇怪.如果需要这样的话,为什么不在调用headerData()之前进行检查呢?
无论如何,我希望可能会帮助一些有同样问题的人:)