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 access Model index via contextMenuEvent?
Forum Update on Monday, May 27th 2025

How can I access Model index via contextMenuEvent?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 498 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.
  • L Offline
    L Offline
    Loquat
    wrote on 31 Aug 2021, 14:39 last edited by
    #1

    Hi,
    I have a custom Model that inherits from QAbstractListModel and I use QListView to access the data in it.

    I want to right click on a single item from the list and know which item from the Model that is.
    Why in the below code, the returned "item" index is wrong and later not recognised by

    myListView->model()->data(item).toString();

    Please see the below code snippet:

    void MyClass::contextMenuEvent(QContextMenuEvent* event)
    {
        if (event->reason() == QContextMenuEvent::Mouse)
        {
            QModelIndex item = myListView->indexAt(event->pos());
       
            //blah, blah, blah
        }
    }
    
    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 31 Aug 2021, 14:44 last edited by VRonin
      #2

      Probaby a matter of relative coordinates, try event->globalPos()

      Edit: worng answer

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Loquat
        wrote on 31 Aug 2021, 14:50 last edited by
        #3

        Nope, the index remains the same and looks like this in Watch:

        index {r=0xffffffff c=0xffffffff p=0x00000000 ...} const QModelIndex &

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 31 Aug 2021, 15:16 last edited by Christian Ehrlicher
          #4

          indexAt() expects local coordinates of the view, not of some other widget where you catch the contextMenuEvent. You have to translate it.

          btw: https://doc.qt.io/qt-5/qwidget.html#customContextMenuRequested is the normal way to go here.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3
          • V Offline
            V Offline
            VRonin
            wrote on 31 Aug 2021, 15:17 last edited by VRonin
            #5

            Fully working minimal example:

            #include <QApplication>
            #include <QStandardItemModel>
            #include <QListView>
            #include <QDebug>
            int main(int argc, char *argv[])
            {
                QApplication app(argc,argv);
                QStandardItemModel model(5,1);
                for(int i=0;i<model.rowCount();++i)
                    model.setData(model.index(i,0),i+1);
            
                QListView view;
                view.setModel(&model);
                view.setContextMenuPolicy(Qt::CustomContextMenu);
                QObject::connect(&view,&QListView::customContextMenuRequested,[&view](const QPoint &pos){
                    qDebug() << view.indexAt(pos);
                });
                view.show();
                return app.exec();
            }
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1
            • L Offline
              L Offline
              Loquat
              wrote on 31 Aug 2021, 15:33 last edited by
              #6

              Thanks guys, that's looking well-promising now! The "Item" looks like a valid number now. Much appreciate your help!

              However, what I'm trying to do next, fails. Would you mind having a look, please?

              void MyClass::showContextMenu(const QPoint& pos)
              {
                  /* Handle global position */
                  QPoint globalPos = deviceView->mapToGlobal(pos);
                  QModelIndex item = deviceView->indexAt(pos);
              
                  /* Create menu and insert some actions */
                  QMenu rightClickMenu;
                  rightClickMenu.addAction("Some Action");
              
                  QAction* selectedMenuItem = rightClickMenu.exec(globalPos);  // does not work with "pos" either
                  if (!selectedMenuItem)
                  {
                      return; // always end up here!
                  }
              ...
              
              1 Reply Last reply
              0
              • L Offline
                L Offline
                Loquat
                wrote on 31 Aug 2021, 16:23 last edited by
                #7

                What I'm trying to do is:

                • open a QListView in my MainWindow (in fact, I open it in a Dock, that is in MainWindow, which then sets its widget as my QListView)
                • display data from the Model that my QListView is using
                • right-click on a particular row and select an item which will create another dedicated dock

                How can I make sure that the right-click only works for the QListView? Because at the moment, it seems that I can click anywhere and it will show the right-click menu. Doing the below doesn't seem to work:

                    MyListModel* myModel = new MyListModel();
                    myView = new QListView;
                    myView->setModel(myModel);
                
                    /* Custom right-click menu */
                    myView ->setContextMenuPolicy(Qt::CustomContextMenu);
                
                    connect(myView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
                

                As you can see in the connect, signal-to-slot is from myView to "this" because showContextMenu is defined in "this", i.e. my class that instantiates QListView... is that all correct?

                V 1 Reply Last reply 1 Sept 2021, 09:34
                0
                • L Loquat
                  31 Aug 2021, 16:23

                  What I'm trying to do is:

                  • open a QListView in my MainWindow (in fact, I open it in a Dock, that is in MainWindow, which then sets its widget as my QListView)
                  • display data from the Model that my QListView is using
                  • right-click on a particular row and select an item which will create another dedicated dock

                  How can I make sure that the right-click only works for the QListView? Because at the moment, it seems that I can click anywhere and it will show the right-click menu. Doing the below doesn't seem to work:

                      MyListModel* myModel = new MyListModel();
                      myView = new QListView;
                      myView->setModel(myModel);
                  
                      /* Custom right-click menu */
                      myView ->setContextMenuPolicy(Qt::CustomContextMenu);
                  
                      connect(myView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
                  

                  As you can see in the connect, signal-to-slot is from myView to "this" because showContextMenu is defined in "this", i.e. my class that instantiates QListView... is that all correct?

                  V Offline
                  V Offline
                  VRonin
                  wrote on 1 Sept 2021, 09:34 last edited by
                  #8

                  @Loquat said in How can I access Model index via contextMenuEvent?:

                  is that all correct?

                  Yes

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  0

                  1/8

                  31 Aug 2021, 14:39

                  • Login

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