QtAction

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(QKeySequence::New);
    newAction->setStatusTip(tr("Create a new spreadsheet file"));
    connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
}


QAction 相当于一个对象,这个对象可以和任意多的Menu以及ToolBar想绑定,相当于建议伙伴关系,当菜单或者ToolBar单击的时候,就会触发一个事件,发射一个信号,然后调用对应的槽函数来相应对应的动作。



THE END!
2012年12月28日
原文地址:https://www.cnblogs.com/xingchen/p/2837616.html