How to drag QActions from Custom QMenu
-
I have created a custom QMenu class and in that i have added few actions to it. I have to drag the QActions and drop it in a custom widget. I have to override the MousePressEvent, MouseMoveEvent, DragEnterEvent, DragLeaveEvent etc. and i have done it. My Problem is, I can able to drag the menu items, but i cannot drag the actions. from this image shown below, i can able to drag the "Nokia Developer" but i cannot able to drag the "Nokia India" and "Nokia Europe" actions. can anyone please explain me with an example how the drag and drop of menu and actions works and from that how to drag an action from a custom QMenu.
!http://i.imgur.com/608y0r3.png(Drag Actions)!
SubMenu.h
@#ifndef SUBMENU_H
#define SUBMENU_H#include <QMainWindow>
#include "ui_SubMenu.h"
#include "DerivedSubMenu.h"class SubMenu : public QMainWindow
{
Q_OBJECTpublic:
SubMenu(QWidget *parent = 0);
~SubMenu();private slots:
void wikiForumNokia();
void DiBoForumNokia();
void nokiaIndia();
void nokiaEurope();
void help();
void about();private:
void createMyMenus();private:
Ui::SubMenu ui;- DerivedSubMenu *nokiaMenu; //DerivedSubMenu is the Sub class of QMenu
DerivedSubMenu forumNokiaMenu; //DerivedSubMenu is the Sub class of QMenu
QAction *menu_nokiaIndiaAction; QAction *menu_nokiaEuropeAction; QAction *menu_wikiAction; QAction *menu_DiBoAction; QAction *menu_aboutAction; QAction *menu_helpAction; QAction *menu_exitAction;
};
#endif // SUBMENU_H@
SubMenu.cpp
@#include "SubMenu.h"
#include <QMenu>
#include <QMessageBox>
#include <QDesktopServices>
#include <QUrl>SubMenu::SubMenu(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setWindowTitle(tr("Sub Menus"));
//Create memu and sub menu.
createMyMenus();
}void SubMenu::createMyMenus()
{//add action for help menu. menu_helpAction = new QAction(tr("Help"), this); menuBar()->addAction(menu_helpAction); connect(menu_helpAction, SIGNAL(triggered()), this, SLOT(help())); //add action for about menu. menu_aboutAction = new QAction(tr("About"), this); menuBar()->addAction(menu_aboutAction); connect(menu_aboutAction, SIGNAL(triggered()), this, SLOT(about())); /* for nokia menu and its sub menus. */ //create action Nokia India sub menu item. menu_nokiaIndiaAction = new QAction(tr("Nokia India"), this); connect(menu_nokiaIndiaAction, SIGNAL(triggered()), this, SLOT(nokiaIndia())); //create action Nokia Europe sum menu item menu_nokiaEuropeAction = new QAction(tr("Nokia Europe"), this); connect(menu_nokiaEuropeAction, SIGNAL(triggered()), this, SLOT(nokiaIndia())); //add menu item nokia to main menu *nokiaMenu = new DerivedSubMenu(this);* // creating menu with the Sub classed QMenu nokiaMenu->addMenu("Nokia"); //add action for nokia india sub-menu nokiaMenu->addAction(menu_nokiaIndiaAction); //add action for nokia india sub-menu nokiaMenu->addAction(menu_nokiaEuropeAction); //create action for Discussion Boards sub menu item menu_DiBoAction = new QAction(tr("Discussion Boards"), this); connect(menu_DiBoAction, SIGNAL(triggered()), this, SLOT(DiBoForumNokia())); //create action for Wiki sub menu item menu_wikiAction = new QAction(tr("Wiki"), this); connect(menu_wikiAction, SIGNAL(triggered()), this, SLOT(wikiForumNokia())); //add menu item Nokia Developer to menu item nokia
- forumNokiaMenu = new DerivedSubMenu(this);* // creating menu with the Sub classed QMenu
forumNokiaMenu->addMenu("Nokia Developer");
//forumNokiaMenu = nokiaMenu->addMenu(tr("Nokia Developer"));
//add action for Discussion Boards sub-menu
forumNokiaMenu->addAction(menu_DiBoAction);
//add action for Wiki sub-menu
forumNokiaMenu->addAction(menu_wikiAction);
//Add action for exit menu. menu_exitAction = new QAction(tr("Exit"), this); menuBar()->addAction(menu_exitAction); connect(menu_exitAction, SIGNAL(triggered()), this, SLOT(close()));
}
/* handle action for wiki sub-menu item. */
void SubMenu::wikiForumNokia()
{
QDesktopServices::openUrl(QUrl(tr("http://www.developer.nokia.com/Community/Wiki/Wiki_Home")));
}/* handle action for Discussion Boards sub-menu item. */
void SubMenu::DiBoForumNokia()
{
QDesktopServices::openUrl(QUrl(tr("http://www.developer.nokia.com/Community/Discussion/")));
}/* handle action for nokia india sub-menu item. */
void SubMenu::nokiaIndia()
{
QDesktopServices::openUrl(QUrl(tr("http://www.nokia.co.in/")));
}/* handle action for nokia europe sub-menu item. */
void SubMenu::nokiaEurope()
{
QDesktopServices::openUrl(QUrl(tr("http://europe.nokia.com/home")));
}/* handle action for help menu item. */
void SubMenu::help()
{
QMessageBox::information(this, tr("Help Menu"),tr("This example shows how to create submenu."));
}/* handle action for about menu item. */
void SubMenu::about()
{
QMessageBox::about(this, tr("About Menu"),tr("Testing Sub-Menus."));
}SubMenu::~SubMenu()
{}@
- DerivedSubMenu *nokiaMenu; //DerivedSubMenu is the Sub class of QMenu