QTreeView and context menu



  • Hi
    I have developed a class derived from QAbstractItemModel and am using it to display data in a QTreeView.
    I want to display a context menu when the user right clicks on an item in the tree. The contents of this menu are dependent on which item in the tree has bee clicked and the result of the click will result in an additional row being inserted into the model.
    I would be grateful of some guidance of how to achieve this.

    Thanks



  • I assume, you already know how call menu and how detect your item's type.
    One of solutions is manipulating visibility of QAction:
    @void on_trvCustomTree_customContextMenuRequested(const QPoint &pos)
    {
    //get position where to display menu
    QPoint globePos = ui->trvCustomTree->mapToGlobal(pos);

    //get item
    QModelIndex currentItem = ui->trvCustomTree->currentIndex();
    //or more correct is
    QModelIndex currentItem = ui->trvCustomTree->indexAt(pos);
    
    //here you recognize type of your item
    //....
    
    //and finally manage actions visibility
    switch(itemType)
    {
    case type_1:
        action_1->setVisible(true);
        action_2->setVisible(false);
        break;
    //....
    }  
    

    }@
    something like that



  • Cheers!


Log in to reply
 

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