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. QMenu subclass not showing items
Forum Update on Monday, May 27th 2025

QMenu subclass not showing items

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 549 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
    abdullahzubair109
    wrote on last edited by abdullahzubair109
    #1

    i have a subclass of qmenu (FileListContext).. i add some actions in the constructor

    FileListContext::FileListContext(QWidget *parent)
    : QMenu(parent)
    {
    	QAction action1("Rename File", this);
    	this->addAction(&action1);
    	QAction action2("Remove File", this);
    	this->addAction(&action2);
    
    }
    

    in parent widget, i created a new instance :

    void parentWidget::contextMenuEvent(QContextMenuEvent *event)
    {
        FileListContext *contextMenu = new FileListContext(this);
        contextMenu->exec(QCursor::pos());
        qDebug() << contextMenu->isEmpty();
    }
    

    unfortunately no item is visible and contextMenu->isEmpty() returns true;
    ContextMenuPolicy is set to Qt::DefaultContextMenu

    N.B. if i add action in parentWidget, it works..but adding them in the constructor doesn't work..

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @abdullahzubair109 said in QMenu subclass not showing items:

      QAction action1("Rename File", this);

      How long does this object live?

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

      A 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        @abdullahzubair109 said in QMenu subclass not showing items:

        QAction action1("Rename File", this);

        How long does this object live?

        A Offline
        A Offline
        abdullahzubair109
        wrote on last edited by
        #3

        @Christian-Ehrlicher until parent widget destroyed

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then I would suggest you to go and get a C++ book. The object is created on the stack and destroyed just before the function ends.

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

          A 1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

            Then I would suggest you to go and get a C++ book. The object is created on the stack and destroyed just before the function ends.

            A Offline
            A Offline
            abdullahzubair109
            wrote on last edited by
            #5

            @Christian-Ehrlicher ah I missed that.. Thanks to remind me

            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