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)
    {
    setModel(&model);

    addFileAction = new QAction(tr("Add file"), this);
    projectMenu = new QMenu(this);
    projectMenu->addAction(addFileAction);
    
    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenu(const QPoint&)));
    

    }

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

    QPoint globalPos = mapToGlobal(pos);
    projectMenu->exec(globalPos);
    

    }@

    All the answers are welcome!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.