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 Update on Monday, May 27th 2025

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.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.
  • LoquatL Offline
    LoquatL Offline
    Loquat
    wrote on 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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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

      LoquatL 1 Reply Last reply
      2
      • SGaistS SGaist

        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 ?

        LoquatL Offline
        LoquatL Offline
        Loquat
        wrote on 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.

        eyllanescE 1 Reply Last reply
        0
        • LoquatL Loquat

          @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.

          eyllanescE Offline
          eyllanescE Offline
          eyllanesc
          wrote on 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
          • LoquatL Offline
            LoquatL Offline
            Loquat
            wrote on 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
                }
            }
            
            eyllanescE 1 Reply Last reply
            0
            • LoquatL Loquat

              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
                  }
              }
              
              eyllanescE Offline
              eyllanescE Offline
              eyllanesc
              wrote on 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.

              LoquatL 1 Reply Last reply
              0
              • eyllanescE eyllanesc

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

                LoquatL Offline
                LoquatL Offline
                Loquat
                wrote on last edited by
                #7

                @eyllanesc only once for now

                eyllanescE 1 Reply Last reply
                0
                • LoquatL Loquat

                  @eyllanesc only once for now

                  eyllanescE Offline
                  eyllanescE Offline
                  eyllanesc
                  wrote on 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.

                  LoquatL 1 Reply Last reply
                  0
                  • eyllanescE eyllanesc

                    @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);

                    LoquatL Offline
                    LoquatL Offline
                    Loquat
                    wrote on last edited by
                    #9

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

                    eyllanescE 1 Reply Last reply
                    0
                    • LoquatL Loquat

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

                      eyllanescE Offline
                      eyllanescE Offline
                      eyllanesc
                      wrote on 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
                      • LoquatL Offline
                        LoquatL Offline
                        Loquat
                        wrote on last edited by Loquat
                        #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
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 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

                          LoquatL 1 Reply Last reply
                          1
                          • SGaistS SGaist

                            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.

                            LoquatL Offline
                            LoquatL Offline
                            Loquat
                            wrote on last edited by Loquat
                            #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
                            • Cobra91151C Offline
                              Cobra91151C Offline
                              Cobra91151
                              wrote on last edited by Cobra91151
                              #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!

                              LoquatL 1 Reply Last reply
                              0
                              • Cobra91151C Cobra91151

                                @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!

                                LoquatL Offline
                                LoquatL Offline
                                Loquat
                                wrote on last edited by Loquat
                                #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?

                                Cobra91151C 1 Reply Last reply
                                0
                                • Cobra91151C Offline
                                  Cobra91151C Offline
                                  Cobra91151
                                  wrote on last edited by
                                  #16

                                  @Loquat

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

                                  1 Reply Last reply
                                  1
                                  • LoquatL Loquat

                                    @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?

                                    Cobra91151C Offline
                                    Cobra91151C Offline
                                    Cobra91151
                                    wrote on last edited by Cobra91151
                                    #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.
                                    LoquatL 1 Reply Last reply
                                    1
                                    • Cobra91151C Cobra91151

                                      @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.
                                      LoquatL Offline
                                      LoquatL Offline
                                      Loquat
                                      wrote on last edited by
                                      #18

                                      @Cobra91151 That's brilliant! Thanks very much!

                                      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