problem in treewidget context menu
Hi all
I'm writting an application where i'm adding elements to tree widget. if i right click on that treewidget a pop up menu shoud open. which having option to addparent or add child to parent. below is my code but its not working.i put a debug statment to capture the action but its showing the action is empty.
Code:
Headre file---
private slot:
void addparent();
void addchild();
void ContextMenuDisplayMethod
(const QPoint &pt
);
In constructor---
connect(ui
->treeWidget,
SIGNAL(customContextMenuRequested
(const QPoint &)),
this,
SLOT(ContextMenuDisplayMethod
(const QPoint &)));
in cpp file---
void ContextMenuDisplayMethod
(const QPoint &pt
) {
QAction action1
(tr
("Add parent"),ui
->treeWidget
);
QAction action2
(tr
("Add child"),ui
->treeWidget
);
QList<QAction *> actionsList;
actionsList.append(&action1);
actionsList.append(&action2);
contextMenu.addAction(&action1);
contextMenu.addAction(&action2);
contextMenu.exec(actionsList,pt);
qDebug()<<"menu slected : "<<contextMenu.menuAction()->text();// showing Action clicked: ""
connect(&action1, SIGNAL(triggered()),
this, SLOT(addparent()));
connect(&action2, SIGNAL(triggered()),
this, SLOT(addchild()));
}
plz help.. :(
Re: problem in treewidget context menu
you should allocate actions in the heap, but you create them in the stack.