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. Contextmenu in QTreeView
Qt 6.11 is out! See what's new in the release blog

Contextmenu in QTreeView

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.2k Views 1 Watching
  • 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.
  • D Offline
    D Offline
    developer_61
    wrote on last edited by developer_61
    #1

    Hello !
    I want use a context menu in the QTreeView. So if i'm going to rightlick an element in the Tree i want to chose between actions.
    I tried following:

    ui->tree->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->tree, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(showContextMenu(const QPoint&))); 
    
    void MainWindow::showContextMenu(const QPoint& pos)
    {
    
      auto contextMenu = new QMenu(ui->tree);
      contextMenu->addAction("Uninstall TA", this, SLOT(uninstallAppletClickedSlot()));
    
      QModelIndex index = ui->tree->indexAt(pos);
      if (index.isValid() && index.row() % 2 == 0) {
        contextMenu->exec(ui->tree->viewport()->mapToGlobal(pos));
      }
    } 
    

    But i get nothing.
    Ideas?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      well its either index that is not valid or (index.row() % 2 == 0) is not true

      rest of code looks good.

      try

       if ( true /*index.isValid() && index.row() % 2 == 0*/) {
          contextMenu->exec(ui->tree->viewport()->mapToGlobal(pos));
        }
      

      and see if it pops.

      also. you create a new menu each time. You could move it as a member to avoid this.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        developer_61
        wrote on last edited by developer_61
        #3

        @mrjj i tried the debugger, i don't even get into the function.

        mrjjM 1 Reply Last reply
        0
        • D developer_61

          @mrjj i tried the debugger, i don't even get into the function.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @developer_61

          ok?
          Did you check that connect returns true ?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            developer_61
            wrote on last edited by
            #5

            @mrjj right problem solved. I got true now. Problem was with the syntax. Thank You.

            mrjjM 1 Reply Last reply
            0
            • D developer_61

              @mrjj right problem solved. I got true now. Problem was with the syntax. Thank You.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @developer_61

              Super.
              Can you tell what it was for me as i didnt spot it :(

              1 Reply Last reply
              0
              • M Offline
                M Offline
                ManjunathXI
                wrote on last edited by
                #7

                Hi,
                First enable custom context menu by using below code and connect the slot

                ui->treeView->setContextMenuPolicy( Qt::CustomContextMenu );
                connect(ui->treeView, SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(ShowContextMenuMessage(QPoint)));

                In slot create menu and pop up the menu in mouse position like this

                void Mainwindow::ShowContextMenuMessage(QPoint p)
                {
                QMenu *menu = new QMenu("menu",ui->treeView_Messages);
                menu->addAction(QIcon(":/new/prefix1/Icons/add.ico"),"Add Tag",this,SLOT(addNewTag()));
                menu->popup(QCursor::pos());
                menu->exec();
                }

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  developer_61
                  wrote on last edited by developer_61
                  #8

                  @mrjj sure :). I spot that there is another syntax for the connect function with &.
                  i use:

                  connect(ui->tree,&QWidget::customContextMenuRequested ,this, &MainWindow::showContextMenu);
                  

                  and it works fine.
                  I think you would spot it easy if you would see the whole code.

                  @ManjunathXI thank you already solved!

                  1 Reply Last reply
                  1

                  • Login

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