Important: Please read the Qt Code of Conduct -

Context menu breaks TreeView structure and selection

  • We have faced recently the problem with context menu for the QTreeView. Context menu appears normally, but at the same time TreeView updates itself to the previous state. Consider folders. For example, if there are two folders in the TreeView. You open one of them and makes right click on the item inside. Then QTreeView shows the previous state, i.e. two collapsed folders. The second problem is that selection also jumpes to the one in the previous state.

    Here is the example of the code (Qt 4.7.4):

    @Viewer::Viewer(QWidget *parent) : QTreeView(parent)

    addFileAction = new QAction(tr("Add file"), this);
    projectMenu = new QMenu(this);
    connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&)));


    void ProjectViewer::showContextMenu(const QPoint& pos){

    QPoint globalPos = mapToGlobal(pos);


    All the answers are welcome!

Log in to reply