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. Create context menu when right-click on header of QTreeWidget
Forum Updated to NodeBB v4.3 + New Features

Create context menu when right-click on header of QTreeWidget

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 7.5k 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.
  • P Offline
    P Offline
    Please_Help_me_D
    wrote on 15 Feb 2020, 16:10 last edited by
    #1

    Hi,

    I'm trying to write a code that shows me context menu when I press right-click on header of QTreeWidget but I only could do that for non-header area of that widget.

    I use the code in mainwindow.cpp (here of course part of this file):

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->treeWidget_seismic->setColumnCount(1);
    
        /* Context menu*/
        connect(ui->treeWidget_seismic, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotCustomMenuRequested(QPoint)));
    }
    
    /* Context menu*/
    void MainWindow::slotCustomMenuRequested(QPoint pos)
    {
        QModelIndex index = ui->treeWidget_seismic->indexAt(pos);
        QTreeWidgetItem* item = ui->treeWidget_seismic->currentItem();
    
        menu = new QMenu(this);
        QAction *myAction = menu->addAction("Action 1");
        menu->popup(ui->treeWidget_seismic->viewport()->mapToGlobal(pos));
    
        connect(myAction, SIGNAL(triggered()), this, SLOT(slotAddH5File()));
    }
    
    void MainWindow::slotAddH5File()
    {
        QString fileName = QFileDialog::getOpenFileName(this, tr("Open Project"),
                                                        QDir::homePath(),
                                                        tr("Project file (*.prj)"));
    }
    

    forum.png

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 15 Feb 2020, 17:22 last edited by mrjj
      #2

      Hi
      Its not Treewidget but a QHeaderView (up there)

      So its

          auto widget = ui->treeWidget->header();
          widget->setContextMenuPolicy(Qt::CustomContextMenu);
          connect(widget,&QTreeWidget::customContextMenuRequested,this,&MainWindow::slotCustomMenuRequested); // use new syntax for more joy
      
      void MainWindow::slotCustomMenuRequested(QPoint pos)
      {
      
          QMenu *menu = new QMenu(this);
          menu->addAction(new QAction("Action 1", this));
          menu->addAction(new QAction("Action 2", this));
          menu->addAction(new QAction("Action 3", this));
          menu->popup(ui->treeWidget->header()->mapToGlobal(pos));
      }
      
      

      alt text

      P 1 Reply Last reply 15 Feb 2020, 18:41
      5
      • M mrjj
        15 Feb 2020, 17:22

        Hi
        Its not Treewidget but a QHeaderView (up there)

        So its

            auto widget = ui->treeWidget->header();
            widget->setContextMenuPolicy(Qt::CustomContextMenu);
            connect(widget,&QTreeWidget::customContextMenuRequested,this,&MainWindow::slotCustomMenuRequested); // use new syntax for more joy
        
        void MainWindow::slotCustomMenuRequested(QPoint pos)
        {
        
            QMenu *menu = new QMenu(this);
            menu->addAction(new QAction("Action 1", this));
            menu->addAction(new QAction("Action 2", this));
            menu->addAction(new QAction("Action 3", this));
            menu->popup(ui->treeWidget->header()->mapToGlobal(pos));
        }
        
        

        alt text

        P Offline
        P Offline
        Please_Help_me_D
        wrote on 15 Feb 2020, 18:41 last edited by
        #3

        @mrjj Thank you!
        Is there a way of control wether to invoke context menu after right-click or double click (or left click)?

        M A 2 Replies Last reply 15 Feb 2020, 18:44
        0
        • P Please_Help_me_D
          15 Feb 2020, 18:41

          @mrjj Thank you!
          Is there a way of control wether to invoke context menu after right-click or double click (or left click)?

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 15 Feb 2020, 18:44 last edited by mrjj
          #4

          @Please_Help_me_D

          Hi
          The default context menu is only for right-click, but its quite easy to make your own subclass of QHeaderView
          and overwrite mousePress/dbclick and use show menu as you want.
          Or emit a new signal so you can respond in another widget and show different menus easy/pr use case.

          P 1 Reply Last reply 15 Feb 2020, 19:48
          2
          • M mrjj
            15 Feb 2020, 18:44

            @Please_Help_me_D

            Hi
            The default context menu is only for right-click, but its quite easy to make your own subclass of QHeaderView
            and overwrite mousePress/dbclick and use show menu as you want.
            Or emit a new signal so you can respond in another widget and show different menus easy/pr use case.

            P Offline
            P Offline
            Please_Help_me_D
            wrote on 15 Feb 2020, 19:48 last edited by
            #5

            @mrjj ok!
            Good to know it
            Thank you

            1 Reply Last reply
            0
            • P Please_Help_me_D
              15 Feb 2020, 18:41

              @mrjj Thank you!
              Is there a way of control wether to invoke context menu after right-click or double click (or left click)?

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 16 Feb 2020, 10:24 last edited by
              #6

              @Please_Help_me_D said in Create context menu when right-click on header of QTreeWidget:

              @mrjj Thank you!
              Is there a way of control wether to invoke context menu after right-click or double click (or left click)?

              Context menu should only be displayed on right click. Otherwise your app will confuse your users ;)

              Right click is simply the convention.

              Regards

              Qt has to stay free or it will die.

              1 Reply Last reply
              3

              3/6

              15 Feb 2020, 18:41

              • Login

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