QTreeView未显示标题

QTreeView not showing header

本文关键字:标题 显示 QTreeView      更新时间:2023-10-16

我正在显示一个TreeView,其中包含一个自定义sortfilterfroxymodel(以另一个自定义模型为源)和一个自定义委托(覆盖的绘画),以影响每个项目的显示。

但是,我无法显示TreeView的标题。我查看了代理和普通模型,它们都调用了headerData()并返回了正确的值。我没有明确隐藏标题。事实上,我显式地显示了TreeView的标头(),并将HeaderHidden()设置为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()之前进行检查呢?

无论如何,我希望这可能会帮助一些有同样问题的人:)