Solved QMenu subclass not showing items
-
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::DefaultContextMenuN.B. if i add action in parentWidget, it works..but adding them in the constructor doesn't work..
-
@abdullahzubair109 said in QMenu subclass not showing items:
QAction action1("Rename File", this);
How long does this object live?
-
@Christian-Ehrlicher until parent widget destroyed
-
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.
-
@Christian-Ehrlicher ah I missed that.. Thanks to remind me