Reputation: 3132
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 ;)
Upvotes: 3
Views: 2489
Reputation: 3132
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 :)
Upvotes: 14