Important: Please read the Qt Code of Conduct -

Using methods from private classes

  • Hi.

    I need to check whenever user press on the string or folders sign on QTreeView.
    I saw qt source of mousePressEvent:

    void QTreeView::mousePressEvent(QMouseEvent *event)
        bool handled = false;
        if (style()->styleHint(QStyle::SH_ListViewExpand_SelectMouseType, 0, this) == QEvent::MouseButtonPress)
            handled = d->expandOrCollapseItemAtPos(event->pos());
        if (!handled && d->itemDecorationAt(event->pos()) == -1)

    There are using of the expandOrCollapseItemAtPos(QPos *) and itemDecorationAt(QPos *) methods. These methods located in the QTreeViewPrivate class.
    Can I use these methods? Is it legal?
    Do I need to reimplement QTreeViewPrivate class to use these methods?

    Thank you.

  • Moderators

    Can I use these methods? Is it legal?

    Yes you can. You need to include widgets-private in QT variable in qmake to do so.

    Remember, though, that these methods are called private for a reason - normally users do not need to use them. Also, Qt does not offer any source / binary compatibility promises for private classes.

    Are you sure you can't achieve your goals without private classes?

  • @sierdzio , thank you! I will try to solve my task by another way.

  • Lifetime Qt Champion

    There is signals for when it's expanded / collapsed.

    If you tell what task is, maybe someone knows good way to solve it. :)

  • @mrjj
    My task is to drag items from QTreeView to another widget.
    But when I try to drag item this item makes selected. I need to drag items without selection them.
    So I want to reimplement mousePressEvent.
    But I still need to collapse/expand items, so if I reimplement mousePressEvent I need to see which area of the item (string or sign) user pressed.

Log in to reply