【发布时间】:2023-03-17 10:36:01
【问题描述】:
我正在显示一个带有自定义 sortfilterproxymodel(它以另一个自定义模型作为源)和一个自定义委托(覆盖绘制)的 TreeView,以影响每个项目的显示。
但是,我无法显示 TreeView 的标题。我查看了代理和普通模型,都调用了它们的 headerData() 并返回正确的值。我没有明确隐藏标题。实际上,我明确地 show() 了 TreeView 的 header 并且 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()方法似乎是无辜的;)
【问题讨论】:
-
如果我没记错的话,
QTreeVeiewdelegate 只负责绘制item,不负责绘制header。但是尝试用默认的QItemDelegate::paint(p_painter, p_option, p_index)调用替换你的MyDelegate::paint()中的所有代码。如果标题仍然隐藏,那么问题肯定不在你的paint()。 -
检查
isHeaderHidden属性值。 -
我用调用默认绘制替换了我的代码,但标题仍然隐藏。另外,我在 TreeView 上调用 setHeaderHidden(false),仍然没有可见的标题。
标签: c++ qt model-view-controller treeview