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 to use right click instead of left click for the on_listView_clicked slot from QListView?

How to use right click instead of left click for the on_listView_clicked slot from QListView?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.1k 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.
  • D Offline
    D Offline
    DoubleC122
    wrote on last edited by
    #1

    Hi. I have a QListView widget in which, when I click on one item from it, would show a context menu. I managed to do so, except it works for the left click only. I tried to find a way to make it work for the right click, but I'm a bit confused. I found out information regarding using the mousePressEvent method, except I'm not sure if it's done correctly. As I've seen in many examples, overriding the mousePressEvent method should work, but in my case it doesn't do anything.

    Here is the mousePressEvent as I have it in mainwindow.cpp:

    void MainWindow::mousePressEvent(QMouseEvent *e)
    {
        if(e->button() == Qt::RightButton)
        {
            close();
        }
    }
    

    I also read about using a subclass, but it seemed a bit cumbersome to me, given the fact that I've seen other ways to do it, like the one I'm trying right now (not very successfully though).

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

      Hi
      if you mean a normal context menu ?
      then something like

       ui->theview->setContextMenuPolicy(Qt::CustomContextMenu);
        connect(ui->theview, &QListView::customContextMenuRequested, [this](QPoint pos){
                QModelIndex index=ui->lwShapes->indexAt(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->theview->viewport()->mapToGlobal(pos));
          });
      

      Hmm. maybe i misread what you try..

      D 1 Reply Last reply
      3
      • D DoubleC122

        Hi. I have a QListView widget in which, when I click on one item from it, would show a context menu. I managed to do so, except it works for the left click only. I tried to find a way to make it work for the right click, but I'm a bit confused. I found out information regarding using the mousePressEvent method, except I'm not sure if it's done correctly. As I've seen in many examples, overriding the mousePressEvent method should work, but in my case it doesn't do anything.

        Here is the mousePressEvent as I have it in mainwindow.cpp:

        void MainWindow::mousePressEvent(QMouseEvent *e)
        {
            if(e->button() == Qt::RightButton)
            {
                close();
            }
        }
        

        I also read about using a subclass, but it seemed a bit cumbersome to me, given the fact that I've seen other ways to do it, like the one I'm trying right now (not very successfully though).

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        @DoubleC122

        Did you checkout the mousePressEvents of QListView?

        You would need to inhgerit from QListView and overwrite one of these events.

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        1
        • mrjjM mrjj

          Hi
          if you mean a normal context menu ?
          then something like

           ui->theview->setContextMenuPolicy(Qt::CustomContextMenu);
            connect(ui->theview, &QListView::customContextMenuRequested, [this](QPoint pos){
                    QModelIndex index=ui->lwShapes->indexAt(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->theview->viewport()->mapToGlobal(pos));
              });
          

          Hmm. maybe i misread what you try..

          D Offline
          D Offline
          DoubleC122
          wrote on last edited by
          #4

          @mrjj Oh, I already have the context menu working, my issue is that I want it to pop when I use right click, not left click as it is by default. That is the reason why I was trying to use that mousePressEvent.

          mrjjM 1 Reply Last reply
          0
          • D DoubleC122

            @mrjj Oh, I already have the context menu working, my issue is that I want it to pop when I use right click, not left click as it is by default. That is the reason why I was trying to use that mousePressEvent.

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

            @DoubleC122
            ok, but customContextMenuRequested pops on right click pr default so
            i was wondering if that was easier ?

            D 2 Replies Last reply
            1
            • mrjjM mrjj

              @DoubleC122
              ok, but customContextMenuRequested pops on right click pr default so
              i was wondering if that was easier ?

              D Offline
              D Offline
              DoubleC122
              wrote on last edited by
              #6

              @mrjj I see. I'll try with that.

              1 Reply Last reply
              0
              • mrjjM mrjj

                @DoubleC122
                ok, but customContextMenuRequested pops on right click pr default so
                i was wondering if that was easier ?

                D Offline
                D Offline
                DoubleC122
                wrote on last edited by
                #7

                @mrjj Alright that seems to do the job quite fine. Thank you very much for your time.

                1 Reply Last reply
                1

                • Login

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