Labels

Thursday, September 29, 2011

QAction class

Qt simplifies the programming of menus and toolbars through its action concept. An action is an item that can be added to any number of menus and toolbars. Creating menus and toolbars in Qt involves these steps:

• Create and set up the actions.

• Create menus and populate them with the actions.

• Create toolbars and populate them with the actions.



In the Spreadsheet application, actions are created in createActions():

void MainWindow::createActions()

{

newAction = new QAction(tr("&New"), this);

newAction->setIcon(QIcon(":/images/new.png"));

newAction->setShortcut(tr("Ctrl+N"));

newAction->setStatusTip(tr("Create a new spreadsheet file"));

connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));

}

The New action has an accelerator (New), a parent (the main window), an

icon (new.png), a shortcut key (Ctrl+N), and a status tip.We connect the action’s

triggered() signal to the main window’s private newFile() slot, which we will

implement in the next section. This connection ensures that when the user

chooses the File|New menu item, clicks the New toolbar button, or presses Ctrl+N,

the newFile() slot is called.

No comments:

Post a Comment