qt addaction
时间: 2023-10-14 08:06:38 浏览: 79
QAction is a class in Qt that represents an action that can be performed in a graphical user interface (GUI). The addAction() function is a method of QMainWindow, QMenu, QToolBar, and other classes that allows you to add an action to the GUI.
Here's an example of how to use addAction() to add a "Save" action to a QMenu:
```
QMenu *fileMenu = new QMenu("File");
QAction *saveAction = new QAction("Save", this);
fileMenu->addAction(saveAction);
```
This creates a new QMenu called "File", creates a new QAction called "Save", and adds the action to the menu using addAction(). The "this" parameter is used to specify the parent object of the QAction (in this case, the current object).
You can also use addAction() to add actions to a QToolBar:
```
QToolBar *toolbar = new QToolBar("My Toolbar");
toolbar->addAction(saveAction);
```
This adds the same "Save" action to a new QToolBar called "My Toolbar".
Overall, addAction() is a useful function for adding actions to GUI elements in Qt.
阅读全文