Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ QTreeView不显示标题_C++_Qt_Model View Controller_Treeview - Fatal编程技术网

C++ QTreeView不显示标题

C++ QTreeView不显示标题,c++,qt,model-view-controller,treeview,C++,Qt,Model View Controller,Treeview,我正在显示一个树状视图,其中包含一个自定义sortfilterproxymodel(将另一个自定义模型作为源)和一个自定义委托(覆盖的绘制),以影响每个项目的显示 但是,我无法显示树视图的标题。我查看了代理和普通模型,都调用了headerData()并返回了正确的值。我没有显式地隐藏标题。事实上,我显式地将树视图的头()和setHeaderHidden()显示为false 什么可能导致标题未显示 下面是委托的paint()函数,我怀疑其中有错误: //----------------------

我正在显示一个树状视图,其中包含一个自定义sortfilterproxymodel(将另一个自定义模型作为源)和一个自定义委托(覆盖的绘制),以影响每个项目的显示

但是,我无法显示树视图的标题。我查看了代理和普通模型,都调用了headerData()并返回了正确的值。我没有显式地隐藏标题。事实上,我显式地将树视图的头()和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();
}
如果您想知道为什么我要手动执行所有的painter操作(save()、drauckground等),那是因为这似乎是更改paint()函数中显示的文本的唯一方法。至少是我唯一能弄明白的。但我不知道这是否与视图中未显示标题有关

编辑:现在我试图用默认的油漆替换我自己的油漆。标头仍然没有显示,因此paint()方法似乎是无辜的;)

问题是我“忘记”在headerData()函数的开头添加以下内容:

if (role != Qt::DisplayRole)
    return QVariant();
虽然我不得不说,为了显示任何东西,你必须有这些行,这有点奇怪。如果这样需要它们,为什么不在调用headerData()之前进行检查呢


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

如果我没记错的话,
qtreeview
代理只负责项目绘图,不负责标题绘图。但是试着用默认调用替换
MyDelegate::paint()
中的所有代码
QItemDelegate::paint(p\u painter,p\u选项,p\u索引)
。如果标头仍处于隐藏状态,则问题肯定不在
paint()
中。请检查
ISHEADERHINDED
属性值。我使用默认绘图调用替换了代码,但标头仍处于隐藏状态。另外,我在TreeView上调用setHeaderHidden(false),仍然没有可见的头。也有同样的问题。我已经在做
if(role!=DisplayRole)return QString(),但这不起作用-它必须是
return QVariant()