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 create a custom context menu which opens up when i right click in my QListWidget box named "syslist".

How to create a custom context menu which opens up when i right click in my QListWidget box named "syslist".

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 431 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.
  • M Offline
    M Offline
    Mr-Workalot
    wrote on last edited by
    #1

    i am new to this and have read the documentation provided by QT for context menu
    i have this in my mainwindow.cpp

    void MainWindow::contextMenuEvent(QContextMenuEvent *event)
    {
        QMenu menu(this);
        menu.addAction("cut Act");
        menu.addAction("copy Act");
        menu.addAction("paste Act");
        menu.exec(event->globalPos());
    }```
    
    and this in my mainwindow.h
    
    protected:
    #ifndef QT_NO_CONTEXTMENU
        void contextMenuEvent(QContextMenuEvent *event) override;
    #endif // QT_NO_CONTEXTMENU
    
    
    
    -------------------------
    but this opens the context menu when i right click anywhere in the window ??
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If you only want that menu for your QListWidget, then subclass that class rather than your MainWindow class.

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

      M 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        If you only want that menu for your QListWidget, then subclass that class rather than your MainWindow class.

        M Offline
        M Offline
        Mr-Workalot
        wrote on last edited by
        #3

        @SGaist

        Thanks for the reply, you mean

        void QListWidget ::contextMenuEvent(QContextMenuEvent *event) ??

        right ?/

        sorry , im new to this

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Yes, but in a subclass.

          You currently have MainWindow which is likely a subclass of QMainWindow. Apply the same concept to QListWidget.

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

          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