Archived:How to use QAction
Archived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Qt Quick should be used for all UI development on mobile devices. The approach described in this article (based on QWidget) is deprecated.
Article Metadata
Tested with
Devices(s): Symbian emulator
Compatibility
Platform(s): Qt
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: QAction
Created: james1980
(17 Jan 2009)
Last edited: hamishwillee
(11 Oct 2012)
Contents |
Overview
This code snippet demonstrates how to use QAction, the class which represents an action in Qt menus and toolbars.
Qt provides the QAction class for an abstract user interface action that can be inserted into widgets. Normally user performs different task in a different way. Whether a task is performed from the menu or from the toolbar or using shortcut key it must be perform in the same manner. So in Qt each command or task is represented as a Action. Than we can add this action to the menu or toolbar. Hence command is executed in the same manner regardless of the user interface used.
QAction *newAct=new QAction();
Download
- Download and install the Qt SDK
Various Function
- Used to set the Icon text
newAct->setIconText("Nokia");
- To add an action to a Action group.
newAct->setActiongroup(QActionGroup *object);
- To auto repeat any of the Action.
newAct->setAutoRepeat(1);
- This property Holds the action escriptive text.
QAction::setText("Helllo");
Code Snippet
#include "menuwindow.h"
menuwindow::menuwindow(QWidget *parent)
: QMainWindow(parent)
{
//...some code
newAct = new QAction(QIcon(":/images/new.png"), tr("&New"), this);
newAct->setShortcut(tr("Ctrl+N"));
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcut(tr("Ctrl+Q"));
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
// .. some code
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
fileMenu->addSeparator();
fileMenu->addAction(exitAct);
// ... some code
}
menuwindow::~menuwindow
{
if(newAct)
{
delete newAct;
}
if(exitAct)
{
delete exitAct;
}
if(fileMenu)
{
delete fileMenu;
}
}
Related Example
Archived:How to create a menu in Qt shows the use of QAction.


14 Sep
2009
22 Sep
2009