QTreeView没有显示标题

 chajian005_844 发布于 2023-02-07 19:39

我正在使用自定义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()方法似乎是无辜的;)

1 个回答
  • 问题是我"忘记"将以下内容添加到headerData()函数的开头:

    if (role != Qt::DisplayRole)
        return QVariant();
    

    虽然我不得不说你必须拥有这些线才能显示任何东西,这有点奇怪.如果需要这样的话,为什么不在调用headerData()之前进行检查呢?

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

    2023-02-07 19:43 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有