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. How can I show context menu (right-click) only for a specific View?
Forum Updated to NodeBB v4.3 + New Features

How can I show context menu (right-click) only for a specific View?

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 3.4k Views 2 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.
  • L Offline
    L Offline
    Loquat
    wrote on 31 Aug 2021, 19:46 last edited by Loquat
    #1

    Hi,

    I instantiate my list View (QListView) in my QMainWindow and I want to show the context menu only for that List.
    Below is signal-slot I have to call to get the custom menu, but because the slot object is "this" (i.e. Main Window), the menu shows wherever I click in the window...

    connect(myView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
    

    Does it mean I have to subclass the View (e.g. myViewClass: public QListView) and then instantiate it, so that I can do:

    connect(myView, SIGNAL(customContextMenuRequested(QPoint)), myViewClass, SLOT(showContextMenu(QPoint)));
    

    ?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 31 Aug 2021, 20:31 last edited by
      #2

      Hi,

      QMainWindow has its own handling of contextual menu event so you might be seeing this. Are you sure it's showing only your custom menu ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 31 Aug 2021, 20:32
      2
      • S SGaist
        31 Aug 2021, 20:31

        Hi,

        QMainWindow has its own handling of contextual menu event so you might be seeing this. Are you sure it's showing only your custom menu ?

        L Offline
        L Offline
        Loquat
        wrote on 31 Aug 2021, 20:32 last edited by Loquat
        #3

        @SGaist yes, it's showing my custom context menu all over the Main Window.. how can I make it only valid for the QListView?
        What's more, how can I show context menu only for certain rows in QListView?

        Even when I set QMenu's parent in:

        QMenu myRightClickMenu(myView);
        

        it doesn't work.

        E 1 Reply Last reply 31 Aug 2021, 20:43
        0
        • L Loquat
          31 Aug 2021, 20:32

          @SGaist yes, it's showing my custom context menu all over the Main Window.. how can I make it only valid for the QListView?
          What's more, how can I show context menu only for certain rows in QListView?

          Even when I set QMenu's parent in:

          QMenu myRightClickMenu(myView);
          

          it doesn't work.

          E Offline
          E Offline
          eyllanesc
          wrote on 31 Aug 2021, 20:43 last edited by
          #4

          @Loquat please provide a minimal and reproducible example.

          If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

          1 Reply Last reply
          1
          • L Offline
            L Offline
            Loquat
            wrote on 31 Aug 2021, 20:54 last edited by Loquat
            #5

            Example:

            void MyMainWindowClass::createDockWindows()
            {
                QDockWidget* dock = new QDockWidget(tr("My View Dock"), this);
                dock->setObjectName("My List");
                MyCustomListModel* myModel = new MyCustomListModel();
                myView = new QListView;
                myView->setModel(myModel );
            
                /* Custom right-click menu */
                myView->setContextMenuPolicy(Qt::CustomContextMenu);
                connect(myView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
            
                dock->setWidget(myView);
            }
            
            void MyMainWindowClass::showContextMenu(const QPoint& pos)
            {
                /* Handle global position */
                QPoint globalPos = deviceView->mapToGlobal(pos);
            
                /* Create menu and insert some actions */
                QMenu myRightClickMenu(myView);
                QAction* action1= rightClickMenu.addAction("Some Action");
            
                selectedMenuItem = rightClickMenu.exec(globalPos);
                if (!selectedMenuItem)
                {
                    return;
                }
                if (selectedMenuItem == action1)
                {
                    //do something
                }
            }
            
            E 1 Reply Last reply 31 Aug 2021, 20:56
            0
            • L Loquat
              31 Aug 2021, 20:54

              Example:

              void MyMainWindowClass::createDockWindows()
              {
                  QDockWidget* dock = new QDockWidget(tr("My View Dock"), this);
                  dock->setObjectName("My List");
                  MyCustomListModel* myModel = new MyCustomListModel();
                  myView = new QListView;
                  myView->setModel(myModel );
              
                  /* Custom right-click menu */
                  myView->setContextMenuPolicy(Qt::CustomContextMenu);
                  connect(myView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
              
                  dock->setWidget(myView);
              }
              
              void MyMainWindowClass::showContextMenu(const QPoint& pos)
              {
                  /* Handle global position */
                  QPoint globalPos = deviceView->mapToGlobal(pos);
              
                  /* Create menu and insert some actions */
                  QMenu myRightClickMenu(myView);
                  QAction* action1= rightClickMenu.addAction("Some Action");
              
                  selectedMenuItem = rightClickMenu.exec(globalPos);
                  if (!selectedMenuItem)
                  {
                      return;
                  }
                  if (selectedMenuItem == action1)
                  {
                      //do something
                  }
              }
              
              E Offline
              E Offline
              eyllanesc
              wrote on 31 Aug 2021, 20:56 last edited by
              #6

              @Loquat I'm curious, how many times do you invoke method createDockWindows?

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              L 1 Reply Last reply 31 Aug 2021, 21:45
              0
              • E eyllanesc
                31 Aug 2021, 20:56

                @Loquat I'm curious, how many times do you invoke method createDockWindows?

                L Offline
                L Offline
                Loquat
                wrote on 31 Aug 2021, 21:45 last edited by
                #7

                @eyllanesc only once for now

                E 1 Reply Last reply 31 Aug 2021, 21:49
                0
                • L Loquat
                  31 Aug 2021, 21:45

                  @eyllanesc only once for now

                  E Offline
                  E Offline
                  eyllanesc
                  wrote on 31 Aug 2021, 21:49 last edited by eyllanesc
                  #8

                  @Loquat

                  1. Could you give more detail about your sentence: The menu shows wherever I click in the window? The QListView occupies the entire QDockWidget even that empty space under the items is part of the QListView.

                  2. With your implementation you should only call it once since if you do it several times then myView will refer only to the last QListView.

                  3. The position of the contextMenuEvent is with respect to the viewport of the QListView so you must change to: QPoint globalPos = deviceView->viewport()->mapToGlobal(pos);

                  4. Also use the new-syntax connection: connect(myView, &QWidget::customContextMenuRequested, this, &MyMainWindowClass::showContextMenu);

                  If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                  L 1 Reply Last reply 31 Aug 2021, 23:11
                  0
                  • E eyllanesc
                    31 Aug 2021, 21:49

                    @Loquat

                    1. Could you give more detail about your sentence: The menu shows wherever I click in the window? The QListView occupies the entire QDockWidget even that empty space under the items is part of the QListView.

                    2. With your implementation you should only call it once since if you do it several times then myView will refer only to the last QListView.

                    3. The position of the contextMenuEvent is with respect to the viewport of the QListView so you must change to: QPoint globalPos = deviceView->viewport()->mapToGlobal(pos);

                    4. Also use the new-syntax connection: connect(myView, &QWidget::customContextMenuRequested, this, &MyMainWindowClass::showContextMenu);

                    L Offline
                    L Offline
                    Loquat
                    wrote on 31 Aug 2021, 23:11 last edited by
                    #9

                    @eyllanesc To answer 1, the menu shows wherever I mean it shows even when right-clicking outside the QListView

                    E 1 Reply Last reply 31 Aug 2021, 23:12
                    0
                    • L Loquat
                      31 Aug 2021, 23:11

                      @eyllanesc To answer 1, the menu shows wherever I mean it shows even when right-clicking outside the QListView

                      E Offline
                      E Offline
                      eyllanesc
                      wrote on 31 Aug 2021, 23:12 last edited by
                      #10

                      @Loquat Could you provide a complete example, with that piece of code I can't reproduce the problem.

                      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

                      1 Reply Last reply
                      3
                      • L Offline
                        L Offline
                        Loquat
                        wrote on 1 Sept 2021, 08:55 last edited by Loquat 9 Jan 2021, 10:25
                        #11

                        I tried to replicate with QListWidget to simplify things and the context menu still shows up when you right-click anywhere in the main window instead of showing up only for the List Widget. There's not much to it but here is an example that is hopefully reproducible for you (QListWidget* myList is a private member of MyClass)

                        Example:

                        /*Constructor*/
                        MyClass::MyClass(QWidget *parent, Qt::WindowFlags flags) :
                            QMainWindow(parent, flags)
                        {
                            QDockWidget* dock = new QDockWidget(tr("My View Dock"), this);
                        
                            myList= new QListWidget(dock);
                            myList->setContextMenuPolicy(Qt::CustomContextMenu);
                            connect(myList, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));       
                            dock->setWidget(myList);
                            dock->setAllowedAreas(Qt::AllDockWidgetAreas);
                            addDockWidget(Qt::LeftDockWidgetArea, dock);
                        }
                        
                        
                        void MyClass::showContextMenu(const QPoint& pos)
                        {
                            /* Handle global position */
                            QPoint globalPos = myList->viewport()->mapToGlobal(pos);
                        
                            /* Create menu and insert some actions */
                            QMenu rightClickMenu(myList);
                            QAction* Action1 = rightClickMenu.addAction("Action1");
                            QAction* Action2= rightClickMenu.addAction("Action2");
                        
                            selectedMenuItem = rightClickMenu.exec(globalPos);
                            if (!selectedMenuItem)
                            {
                                return;
                            }
                        
                            if (selectedMenuItem == Action1)
                            {
                                //do something
                            }
                            if (selectedMenuItem == Action2)
                            {
                                //do something else
                            }
                        }
                        
                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 1 Sept 2021, 09:18 last edited by
                          #12

                          That's why @eyllanesc asked you for a complete minimal compilable example.

                          With the small pieces you give here we can't do that as we have to guess the rest of the structure.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          L 1 Reply Last reply 1 Sept 2021, 11:07
                          1
                          • S SGaist
                            1 Sept 2021, 09:18

                            That's why @eyllanesc asked you for a complete minimal compilable example.

                            With the small pieces you give here we can't do that as we have to guess the rest of the structure.

                            L Offline
                            L Offline
                            Loquat
                            wrote on 1 Sept 2021, 11:07 last edited by Loquat 9 Jan 2021, 11:21
                            #13

                            @eyllanesc and @SGaist Okay, so below is a reproducible example for you. Please, run it and right-click on the list inside the QListWidget - no context pops up. But when you click anywhere else, even the toolbar, the context menu shows up... what am I doing wrong? I'd like the context menu to show only for the given item in the QList (be it QListView or as it's here QListWidget for simplification purposes). I have to use Qt4 in my original code, hence my usage of lambda here is maybe wrong, as I tried to squeeze everything in main().

                            #include <QApplication>
                            #include <QDebug>
                            #include <QDockWidget>
                            #include <QMenuBar>
                            #include <QToolBar>
                            #include <QMenu>
                            #include <QAction>
                            
                            
                            int main(int argc, char* argv[])
                            {
                                QApplication app(argc, argv);
                                QMainWindow myMainWindow;
                            
                                QMenuBar* menuBar = new QMenuBar();
                                QToolBar* toolBar = new QToolBar();
                                toolBar->setObjectName("ToolBar");
                                QMenu* fileMenu = new QMenu("&File", menuBar);
                                menuBar->addMenu(fileMenu);
                                myMainWindow.setMenuBar(menuBar);
                                myMainWindow.addToolBar(Qt::TopToolBarArea, toolBar);
                            
                                QDockWidget* dock = new QDockWidget("My View Dock");
                            
                                QListWidget* myList = new QListWidget(dock);
                                myList->addItem("Item 1");
                                myList->addItem("Item 2");
                                myList->setContextMenuPolicy(Qt::CustomContextMenu);
                                QObject::connect(myList, &QListWidget::customContextMenuRequested, [myList](const QPoint& pos) {
                                qDebug() << myList->indexAt(pos);
                            
                                QPoint globalPos = myList->viewport()->mapToGlobal(pos);
                                /* Create menu and insert some actions */
                                QMenu rightClickMenu(myList);
                                rightClickMenu.addAction("context1");
                                rightClickMenu.addAction("context2");
                            
                                });
                                dock->setWidget(myList);
                                dock->setAllowedAreas(Qt::AllDockWidgetAreas);
                            
                                myMainWindow.addDockWidget(Qt::LeftDockWidgetArea, dock);
                            
                                myMainWindow.show();
                                return app.exec();
                            }
                            
                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              Cobra91151
                              wrote on 1 Sept 2021, 11:23 last edited by Cobra91151 9 Jan 2021, 11:26
                              #14

                              @Loquat

                              Hello!

                              You must call rightClickMenu.exec(myList->viewport()->mapToGlobal(pos)); at end of your lambda to display your context menu. I have improved your example code, feel free to try out.

                              Code:

                              #include <QApplication>
                              #include <QStandardItemModel>
                              #include <QListView>
                              #include <QDebug>
                              #include <QDockWidget>
                              #include <QMenuBar>
                              #include <QToolBar>
                              #include <QMenu>
                              #include <QAction>
                              #include <QMainWindow>
                              #include <QListWidget>
                              
                              int main(int argc, char* argv[])
                              {
                                  QApplication app(argc, argv);
                                  QMainWindow myMainWindow;
                              
                                  QMenuBar* menuBar = new QMenuBar();
                                  QToolBar* toolBar = new QToolBar();
                                  toolBar->setObjectName("ToolBar");
                                  QMenu* fileMenu = new QMenu("&File", menuBar);
                                  menuBar->addMenu(fileMenu);
                                  myMainWindow.setMenuBar(menuBar);
                                  myMainWindow.addToolBar(Qt::TopToolBarArea, toolBar);
                              
                                  QDockWidget *dock = new QDockWidget("My View Dock");
                              
                                  QListWidget *myList = new QListWidget(dock);
                                  myList->addItem("Item 1");
                                  myList->addItem("Item 2");
                                  myList->setContextMenuPolicy(Qt::CustomContextMenu);
                                  QObject::connect(myList, &QListWidget::customContextMenuRequested, [myList](const QPoint& pos) {
                                      qDebug() << myList->indexAt(pos);
                                      /* Create menu and insert some actions */
                                      QMenu rightClickMenu(myList);
                                      rightClickMenu.addAction("context1");
                                      rightClickMenu.addAction("context2");
                                      rightClickMenu.exec(myList->viewport()->mapToGlobal(pos)); // displays the context menu
                                  });
                                  dock->setWidget(myList);
                                  dock->setAllowedAreas(Qt::AllDockWidgetAreas);
                              
                                  myMainWindow.addDockWidget(Qt::LeftDockWidgetArea, dock);
                              
                                  myMainWindow.show();
                                  return app.exec();
                              }
                              

                              Screenshot:
                              List_context_menu_example.gif

                              Happy coding!

                              L 1 Reply Last reply 1 Sept 2021, 11:27
                              0
                              • C Cobra91151
                                1 Sept 2021, 11:23

                                @Loquat

                                Hello!

                                You must call rightClickMenu.exec(myList->viewport()->mapToGlobal(pos)); at end of your lambda to display your context menu. I have improved your example code, feel free to try out.

                                Code:

                                #include <QApplication>
                                #include <QStandardItemModel>
                                #include <QListView>
                                #include <QDebug>
                                #include <QDockWidget>
                                #include <QMenuBar>
                                #include <QToolBar>
                                #include <QMenu>
                                #include <QAction>
                                #include <QMainWindow>
                                #include <QListWidget>
                                
                                int main(int argc, char* argv[])
                                {
                                    QApplication app(argc, argv);
                                    QMainWindow myMainWindow;
                                
                                    QMenuBar* menuBar = new QMenuBar();
                                    QToolBar* toolBar = new QToolBar();
                                    toolBar->setObjectName("ToolBar");
                                    QMenu* fileMenu = new QMenu("&File", menuBar);
                                    menuBar->addMenu(fileMenu);
                                    myMainWindow.setMenuBar(menuBar);
                                    myMainWindow.addToolBar(Qt::TopToolBarArea, toolBar);
                                
                                    QDockWidget *dock = new QDockWidget("My View Dock");
                                
                                    QListWidget *myList = new QListWidget(dock);
                                    myList->addItem("Item 1");
                                    myList->addItem("Item 2");
                                    myList->setContextMenuPolicy(Qt::CustomContextMenu);
                                    QObject::connect(myList, &QListWidget::customContextMenuRequested, [myList](const QPoint& pos) {
                                        qDebug() << myList->indexAt(pos);
                                        /* Create menu and insert some actions */
                                        QMenu rightClickMenu(myList);
                                        rightClickMenu.addAction("context1");
                                        rightClickMenu.addAction("context2");
                                        rightClickMenu.exec(myList->viewport()->mapToGlobal(pos)); // displays the context menu
                                    });
                                    dock->setWidget(myList);
                                    dock->setAllowedAreas(Qt::AllDockWidgetAreas);
                                
                                    myMainWindow.addDockWidget(Qt::LeftDockWidgetArea, dock);
                                
                                    myMainWindow.show();
                                    return app.exec();
                                }
                                

                                Screenshot:
                                List_context_menu_example.gif

                                Happy coding!

                                L Offline
                                L Offline
                                Loquat
                                wrote on 1 Sept 2021, 11:27 last edited by Loquat 9 Jan 2021, 11:29
                                #15

                                @Cobra91151 Fantastic, thanks, that's helped! The context menu does still appear for the ToolBar and MenuBar, though - how can I prevent that? Plus, I don't want the context menu at a blank row in the list - I want it to show up only when I right-click a specific row - is that possible?

                                C 1 Reply Last reply 1 Sept 2021, 11:48
                                0
                                • C Offline
                                  C Offline
                                  Cobra91151
                                  wrote on 1 Sept 2021, 11:33 last edited by
                                  #16

                                  @Loquat

                                  Yes, it is possible. I will do it, wait a bit.

                                  1 Reply Last reply
                                  1
                                  • L Loquat
                                    1 Sept 2021, 11:27

                                    @Cobra91151 Fantastic, thanks, that's helped! The context menu does still appear for the ToolBar and MenuBar, though - how can I prevent that? Plus, I don't want the context menu at a blank row in the list - I want it to show up only when I right-click a specific row - is that possible?

                                    C Offline
                                    C Offline
                                    Cobra91151
                                    wrote on 1 Sept 2021, 11:48 last edited by Cobra91151 9 Jan 2021, 11:50
                                    #17

                                    @Loquat

                                    So, I disabled the context menu by adding: setContextMenuPolicy(Qt::PreventContextMenu); to menuBar, toolBar and dock objects. Now, the context menu works only for your list. Check out my code below.

                                    Code:

                                    #include <QApplication>
                                    #include <QStandardItemModel>
                                    #include <QListView>
                                    #include <QDebug>
                                    #include <QDockWidget>
                                    #include <QMenuBar>
                                    #include <QToolBar>
                                    #include <QMenu>
                                    #include <QAction>
                                    #include <QMainWindow>
                                    #include <QListWidget>
                                    
                                    int main(int argc, char* argv[])
                                    {
                                        QApplication app(argc, argv);
                                        QMainWindow myMainWindow;
                                    
                                        QMenuBar *menuBar = new QMenuBar();
                                        menuBar->setContextMenuPolicy(Qt::PreventContextMenu);
                                        QToolBar *toolBar = new QToolBar();
                                        toolBar->setObjectName("ToolBar");
                                        QMenu *fileMenu = new QMenu("&File", menuBar);
                                        menuBar->addMenu(fileMenu);
                                        myMainWindow.setMenuBar(menuBar);
                                        myMainWindow.addToolBar(Qt::TopToolBarArea, toolBar);
                                        toolBar->setContextMenuPolicy(Qt::PreventContextMenu);
                                    
                                        QDockWidget *dock = new QDockWidget("My View Dock");
                                        dock->setContextMenuPolicy(Qt::PreventContextMenu);
                                    
                                        QListWidget *myList = new QListWidget(dock);
                                        myList->addItem("Item 1");
                                        myList->addItem("Item 2");
                                        myList->setContextMenuPolicy(Qt::CustomContextMenu);
                                        QObject::connect(myList, &QListWidget::customContextMenuRequested, [myList](const QPoint& pos) {
                                            QModelIndex index = myList->indexAt(pos);
                                    
                                            if (index.isValid()) {
                                                /* Create menu and insert some actions */
                                                QMenu rightClickMenu(myList);
                                                rightClickMenu.addAction("context1");
                                                rightClickMenu.addAction("context2");
                                                rightClickMenu.exec(myList->viewport()->mapToGlobal(pos));
                                            }
                                    
                                            /* Second example, you can use the itemAt method
                                            if (myList->itemAt(pos) != nullptr) {
                                                //Create menu and insert some actions
                                                QMenu rightClickMenu(myList);
                                                rightClickMenu.addAction("context1");
                                                rightClickMenu.addAction("context2");
                                                rightClickMenu.exec(myList->viewport()->mapToGlobal(pos));
                                            }
                                            */
                                        });
                                        dock->setWidget(myList);
                                        dock->setAllowedAreas(Qt::AllDockWidgetAreas);
                                    
                                        myMainWindow.addDockWidget(Qt::LeftDockWidgetArea, dock);
                                    
                                        myMainWindow.show();
                                        return app.exec();
                                    }
                                    

                                    Screenshot:
                                    List_context_menu_example2.gif

                                    There are two ways how you can display the context menu for your list items.

                                    1. You can check if the QModelIndex is valid. You can get the index from your list: myList->indexAt(pos)
                                    2. You can get the actual item by using myList->itemAt(pos) method and check for nullptr.
                                    L 1 Reply Last reply 1 Sept 2021, 12:55
                                    1
                                    • C Cobra91151
                                      1 Sept 2021, 11:48

                                      @Loquat

                                      So, I disabled the context menu by adding: setContextMenuPolicy(Qt::PreventContextMenu); to menuBar, toolBar and dock objects. Now, the context menu works only for your list. Check out my code below.

                                      Code:

                                      #include <QApplication>
                                      #include <QStandardItemModel>
                                      #include <QListView>
                                      #include <QDebug>
                                      #include <QDockWidget>
                                      #include <QMenuBar>
                                      #include <QToolBar>
                                      #include <QMenu>
                                      #include <QAction>
                                      #include <QMainWindow>
                                      #include <QListWidget>
                                      
                                      int main(int argc, char* argv[])
                                      {
                                          QApplication app(argc, argv);
                                          QMainWindow myMainWindow;
                                      
                                          QMenuBar *menuBar = new QMenuBar();
                                          menuBar->setContextMenuPolicy(Qt::PreventContextMenu);
                                          QToolBar *toolBar = new QToolBar();
                                          toolBar->setObjectName("ToolBar");
                                          QMenu *fileMenu = new QMenu("&File", menuBar);
                                          menuBar->addMenu(fileMenu);
                                          myMainWindow.setMenuBar(menuBar);
                                          myMainWindow.addToolBar(Qt::TopToolBarArea, toolBar);
                                          toolBar->setContextMenuPolicy(Qt::PreventContextMenu);
                                      
                                          QDockWidget *dock = new QDockWidget("My View Dock");
                                          dock->setContextMenuPolicy(Qt::PreventContextMenu);
                                      
                                          QListWidget *myList = new QListWidget(dock);
                                          myList->addItem("Item 1");
                                          myList->addItem("Item 2");
                                          myList->setContextMenuPolicy(Qt::CustomContextMenu);
                                          QObject::connect(myList, &QListWidget::customContextMenuRequested, [myList](const QPoint& pos) {
                                              QModelIndex index = myList->indexAt(pos);
                                      
                                              if (index.isValid()) {
                                                  /* Create menu and insert some actions */
                                                  QMenu rightClickMenu(myList);
                                                  rightClickMenu.addAction("context1");
                                                  rightClickMenu.addAction("context2");
                                                  rightClickMenu.exec(myList->viewport()->mapToGlobal(pos));
                                              }
                                      
                                              /* Second example, you can use the itemAt method
                                              if (myList->itemAt(pos) != nullptr) {
                                                  //Create menu and insert some actions
                                                  QMenu rightClickMenu(myList);
                                                  rightClickMenu.addAction("context1");
                                                  rightClickMenu.addAction("context2");
                                                  rightClickMenu.exec(myList->viewport()->mapToGlobal(pos));
                                              }
                                              */
                                          });
                                          dock->setWidget(myList);
                                          dock->setAllowedAreas(Qt::AllDockWidgetAreas);
                                      
                                          myMainWindow.addDockWidget(Qt::LeftDockWidgetArea, dock);
                                      
                                          myMainWindow.show();
                                          return app.exec();
                                      }
                                      

                                      Screenshot:
                                      List_context_menu_example2.gif

                                      There are two ways how you can display the context menu for your list items.

                                      1. You can check if the QModelIndex is valid. You can get the index from your list: myList->indexAt(pos)
                                      2. You can get the actual item by using myList->itemAt(pos) method and check for nullptr.
                                      L Offline
                                      L Offline
                                      Loquat
                                      wrote on 1 Sept 2021, 12:55 last edited by
                                      #18

                                      @Cobra91151 That's brilliant! Thanks very much!

                                      1 Reply Last reply
                                      0

                                      8/18

                                      31 Aug 2021, 21:49

                                      • Login

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