Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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

  • Lifetime Qt Champion

    Hi,

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



  • @SGaist

    Thanks for the reply, you mean

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

    right ?/

    sorry , im new to this


  • Lifetime Qt Champion

    Yes, but in a subclass.

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


Log in to reply