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. ContextMenu not working when using Qt::ActionsContextMenu and static memory allocation
Forum Updated to NodeBB v4.3 + New Features

ContextMenu not working when using Qt::ActionsContextMenu and static memory allocation

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 654 Views 2 Watching
  • 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.
  • P Offline
    P Offline
    partyBoy
    wrote on last edited by
    #1

    Hello,

    I hope someone could explain me this behaviour, when using setContextMenuPolicy(Qt::ActionsContextMenu).
    I can see my QActions in the contextMenu but only when I create them using dynamic memory allocation, like the 3 first. If i use static memory allocation (like the last one) the QAction is not showed in the contextMenu.

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QAction *searchTodayAction = new QAction("Today's Flight", this);
        QAction *searchYesterdayAction = new QAction("Yesterday's Flight", this);
        QAction *randomAction = new QAction("Random Flight", this);
        QAction notWorkingAction("Stuck Flight", this);
    
        this->addAction(searchTodayAction);
        this->addAction(searchYesterdayAction);
        this->addAction(randomAction);
        this->addAction(&notWorkingAction);
        this->setContextMenuPolicy(Qt::ActionsContextMenu);
    }
    

    Thanks !

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

      Hi and welcome to devnet,

      Because notWorkingAction is a variable local to the constructor, so it will be destroyed at the end of it.

      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
      4
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Its not a static allocation, its stack allocation and its
        deleted when scope ends. in this case the constructor.

        1 Reply Last reply
        4

        • Login

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