I want to override the function drawBranches() in QTreeView.
I subclassed the QTreeView class, then I copied drawBranches() function from here. Before change anything in this function, I want to ensure that it works first, but build failed with this error:
error: 'const QTreeViewPrivate* QTreeView::d_func() const' is private
inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(qGetPtrHelper(d_ptr)); } \
Here is my code:
class MyTreeView : public QTreeView
{
Q_OBJECT
public:
MyTreeView(QWidget *parent =0) : QTreeView(parent) {}
void drawBranches(QPainter * painter, const QRect &rect, const
QModelIndex &index)const
{
Q_D(const QTreeView);
const bool reverse = isRightToLeft();
const int indent = d->indent;
const int outer = d->rootDecoration ? 0 : 1;
const int item = d->current;
const QTreeViewItem &viewItem = d->viewItems.at(item);
int level = viewItem.level;
QRect primitive(reverse ? rect.left() : rect.right() + 1, rect.top(), indent, rect.height());
....// Moore lines that I copied
else
opt.state &= ~QStyle::State_MouseOver;
style()->drawPrimitive(QStyle::PE_IndicatorBranch, &opt, painter, this);
current = ancestor;
ancestor = current.parent();
}
painter->setBrushOrigin(oldBO);
}
};
There is many lines where d pointer is used and it is private, e.g d->indent;
.
How can I get a reference to this pointer without violating private role?
Why I want to override this function: I want to hide the expand/collapse indicator for all items except those who have level zero (height level) and I guess by overriding this function I can do that.
Thanks