Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTreeView and context menu
Forum Update on Monday, May 27th 2025

QTreeView and context menu

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrahamL
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GrahamL
        wrote on last edited by
        #3

        Cheers!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved