QAction

QAction类提供了一个抽象的用户界面操作,可以被当作部件。QAction通过信号槽connect连接到用户所要完成的动作。随后,QAction可以被添加到菜单和工具栏,用户单机相应的菜单或者工具栏按钮则可以完成之前通过信号槽连接的动作。

下面给出官方帮助文档的一个使用例子,这个例子中对于fileMenu和fileToolBar没有事先定义:

1 const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/images/open.png"));
2 QAction *openAct = new QAction(openIcon, tr("&Open..."), this);
3 openAct->setShortcuts(QKeySequence::Open);
4 openAct->setStatusTip(tr("Open an existing file"));
5 connect(openAct, &QAction::triggered, this, &MainWindow::open);
6 fileMenu->addAction(openAct);
7 fileToolBar->addAction(openAct);

在给出一个例子,这个例子写的较为详细,也好理解:

 1     openAction = new QAction(QIcon(":/doc-open"), tr("&Open..."), this);
 2     openAction->setShortcuts(QKeySequence::Open);
 3     openAction->setStatusTip(tr("Open an existing file"));
 4     connect(openAction, &QAction::triggered, this, &MainWindow::open);
 5     QMenu *file = menuBar()->addMenu(tr("&File"));
 6     file->addAction(openAction);
 7 
 8     QToolBar *toolBar = addToolBar(tr("&File"));
 9     toolBar->addAction(openAction);
10     statusBar() ;
1 MainWindow::~MainWindow()
2 {
3 }
4 void MainWindow::open()
5 {
6     QMessageBox::information(this, tr("Information"), tr("Open"));
7 }

 

 

posted @ 2016-07-25 23:42  shaoxiaodong92  阅读(437)  评论(0编辑  收藏  举报