[SOLVED] How tu use Actions on popup contextmenu previously created in Action Editor (QT Creator - QT 4.7) ??
-
Hello,
I'm newbie to Qt and C++.
I created and Action (actionConnection) in QT Creator with Action Editor, as show here:
!http://s14.postimage.org/7iri8oa9t/action_Connection.png(ActionEditor )!
It works well and I can insert it in toolbar.
But I would like to insert this action in a popup contextMenu when we right click on tree elements. This is my code for the ContextMenu:
mainwindow.cpp
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_MainWindow_customContextMenuRequested(const QPoint &pos)
{
QMenu *menu = new QMenu();QAction *setupAction = new QAction("Setup", this); QAction *monitorAction = new QAction("Monitor", this); //*How can I refer here to actionConnection created on Action Editor?* menu->addAction(setupAction); menu->addAction(monitorAction); menu->exec(QCursor::pos());
}@
This is the result:
!http://s7.postimage.org/bctn2qkp7/result.png(Result)!
How can I insert the previously created MainWindow Action (actionConnect) in contextMenu (as in toolbar)?
Thank you in advance for your help!!
-
The actions created in Designer are member variables of the ui object. Access them like this:
ui->actionConnection... -
Thank you miroslav. Now It works.
This is the final working code:
@void MainWindow::on_MainWindow_customContextMenuRequested(const QPoint &pos)
{
QMenu *menu = new QMenu();
QAction *setupAction = new QAction("Setup", this);
QAction *monitorAction = new QAction("Monitor", this);menu->addAction(setupAction); menu->addAction(monitorAction); menu->addAction(ui->actionConnection); menu->exec(QCursor::pos());
}@