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 make context menu to work only on a specific widget?

How to make context menu to work only on a specific widget?

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

    Hi there,
    I am making a simple file manager for educational purposes. And it is interesting to know how can I make context menu to appear only when the user right clicks on QListView widget? Here is the picture for better visualization:
    Image
    As you can see the menu appears even when I right click on menuBar (or on statusBar, or on toolBar)

    I am adding action items to the context menu like this

    void MainWindow::contextMenuEvent(QContextMenuEvent *event) {
        QMenu menu(this);
        menu.addAction(ui->actionOpen);
        menu.addAction(ui->actionNew_Directory);
        menu.addSeparator();
        menu.addAction(ui->actionCompress);
        menu.addAction(ui->actionDecompress);
        menu.addSeparator();
        menu.addAction(ui->actionRemove_Object);
    
        menu.exec(event->globalPos());
    }
    

    Thanks :)

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Aram said:

      void MainWindow::contextMenuEvent(...

      That's the problem. You want the context menu on list view but you're handling it on the main window. Subclass the QListView and handle the event there:

      void YourQListViewSubclass::contextMenuEvent(QContextMenuEvent *event) {
      ...
      
      A 1 Reply Last reply
      4
      • Chris KawaC Chris Kawa

        @Aram said:

        void MainWindow::contextMenuEvent(...

        That's the problem. You want the context menu on list view but you're handling it on the main window. Subclass the QListView and handle the event there:

        void YourQListViewSubclass::contextMenuEvent(QContextMenuEvent *event) {
        ...
        
        A Offline
        A Offline
        Aram
        wrote on last edited by
        #3

        @Chris-Kawa thanks for your answer!

        1 Reply Last reply
        0
        • giupignaG Offline
          giupignaG Offline
          giupigna
          wrote on last edited by
          #4

          you don't have to subclass the ListView. You can add Actions on the widgets.
          See addAction(QAction *action)

          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