3

I am displaying a TreeView with a custom sortfilterproxymodel (which takes another custom model as source) and a custom delegate (overwritten paint) to affect the display of each item.

However, I cannot get the header of the TreeView to show. I had a look at both the proxy and the normal model and both have their headerData() called and return the correct values. I do not explicitly hide the header. In fact, I explicitly show() the header of the TreeView and setHeaderHidden() to false.

What could cause the header not being shown?

Here is the paint() function of the delegate, as I suspect the mistake somewhere there:

//---------------------------------------------------------------------------------
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();
}

If you wonder why I do all the painter stuff (save(), drawBackground, etc.) manually, it is because it seems to be the only way to change the displayed text inside the paint() function. At least the only one I could figure out. But I do not know if this has anything to do with the header not being shown in the view.

Edit: By now I tried to replace my own paint with the default one. The header is still not shown, so the paint() method seems to be innocent ;)

3
  • 1
    If I remember correctly, QTreeVeiew delegate is only responsible for item drawing, not for header drawing. But try to replace all the code in your MyDelegate::paint() with the call of default QItemDelegate::paint(p_painter, p_option, p_index). If the header is still hidden, the problem is definitely not in your paint(). Commented Dec 27, 2013 at 16:42
  • Check isHeaderHidden property value. Commented Dec 29, 2013 at 10:31
  • I replaced my code with the call of the default paint, but the header is still hidden. Also, I call setHeaderHidden(false) on the TreeView, still no visible header. Commented Dec 30, 2013 at 10:52

1 Answer 1

14

The problem was that I "forgot" to add the following to the beginning of the headerData() function:

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

Though I have to say it is a bit weird that you have to have those lines in order to display anything at all. If they are required like that, why not do that check before headerData() is even called?

Anyway, I hope that may help some people with the same problem :)

Sign up to request clarification or add additional context in comments.

3 Comments

Had the same problem. I was already doing if(role != DisplayRole) return QString();, but that does not work - it has to be return QVariant().
My goodness, I've been battling this for days, you just saved my bacon. I really didn't think that would matter, although you say you "forgot." Does that mean it's documented somewhere that I missed?
@kyrofa Well, "forgot" in "", so... you know ;) Anyway, this was 11 years ago. If it is STILL required for recent versions of Qt and STILL not documented, then what the heck is the Qt Company doing?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.