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()));
}
Headre file---
private slot:
void addparent();
void addchild();
QMenu contextMenu;
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.popup(QCursor::pos());
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()));
}
To copy to clipboard, switch view to plain text mode
Bookmarks