在Qt中使用QAction来表示窗口的动作,就是窗口上发生的事件。 QAction包含了图标、菜单文字、快捷键、状态栏文字、浮动帮助等信息。 当把一个QAction对象添加到程序中时,Qt自己选择使用哪个属性来显示,无需我们关心。同时,Qt 能够保证把QAction对象添加到不同的菜单、工具栏时,显示内容是同步的。 也就是说,如果我们在菜单中修改了QAction的图标,那么在工具栏上面这个QAction所对应的按钮的图标也会同步修改。
PyQt使用各种动作(QAction)来为主窗口应用程序用户提供各种功能,这些动作会有文本(用在菜单,含菜单栏菜单和快捷菜单)、图标(用在菜单和工具栏上)、工具栏提示信息、状态栏提示信息,以及连接的触发时供调用的槽 值得注意的是,需要给QAction一个父类对象,例如,主窗口,以便声明动作的作用域,还可以确保在适当的时候删除它们。 例如,若要创建一个“打开文件”的动作,可以这样写(这里会假设已有适当的图标和fileOpen方法): self.fileOpenAction=QAction(QIcon("open.png"), 再例如,若要创建文本加粗动作(可切换状态),可以这样写: self.actionTextBold = QAction(QIcon("textbold.png"),"&Bold",self QIcon('textbold.png')), "&Bold", self, priority=QAction.LowPriority,
(tr(“File”), this); fileOpenAction = new QAction(“&Open…”, this); fileSaveAction = new QAction(“&Save 在某些情况下将QAction分组是十分有用的。 = new QAction(“Right”, this); centerAction = new QAction(“Center”, this); justifyAction = new QAction *action1 = new QAction(tr(“&New1”), this); QAction *action2 = new QAction(tr(“&New2”), this); QAction *action3 = new QAction(tr(“&New3”), this); QAction *action4 = new QAction(QIcon(“.
*newFile; QAction *openFile; QAction *save; QAction *saveAs; QAction *printText; QAction *exit; QAction *undoAction;//撤销 QAction *redoAction;//恢复 QAction *copy; QAction *cut; QAction *paste; QAction *insertImg; QAction *insertTable; QAction *find; QAction *about; QAction *aboutQt; QAction *bold; QAction *italic; QAction *underLine ; QAction *color; QAction *left; QAction *center; QAction *right; QAction *justify
我们可以为每个上下文菜单项创建一个 QAction 对象,并使用信号映射器将每个 QAction 的 triggered() 信号与一个槽函数关联。 在槽函数中,我们可以通过信号映射器来获取触发信号的 QAction 对象,从而确定哪个菜单项被点击。 = QAction("Remove from Playlist", self) # 将每个 QAction 的 triggered() 信号与信号映射器关联 self.signal_mapper.setMapping () 方法**QAction 提供了一个 setData() 方法,可以为 QAction 对象设置数据。 __init__(parent) # 为每个上下文菜单项创建一个 QAction 对象 self.add_to_playlist_action = QAction("Add
*text = new QAction(EditMenu); text->setText("编辑文件"); // 设置文本内容 text (":/image/3.ico")); childMenu->addAction(set_file); QAction *read_file = new QAction(childMenu *pNewTask = new QAction(tr("新建"), this); QAction *pEditTask = new QAction(tr("编辑"), this); QAction void MainWindow::onTaskBoxContextMenuEvent(){ // this->sender()就是信号发送者 QAction QAction *pEven = *NewAction = fileMenu->addAction("新建文件"); QAction *OpenAction = fileMenu->addAction("打开文件"); QAction
")); // 设置可用图标 fileMenu->addSeparator(); // 添加分割线 QAction (":/image/3.ico")); childMenu->addAction(set_file); QAction *read_file = new QAction( *pNewTask = new QAction(tr("新建"), this); QAction *pEditTask = new QAction(tr("编辑"), this); QAction *pDeleteTask = new QAction(tr("删除"), this); // 设置属性值编号: 1=>新建 2=>设置 3=>删除 pNewTask->setData QAction *pEven = qobject_cast<QAction *>(this->sender()); // 获取编号: 1=>新建 2=>设置 3=>删除 int iType
为了增加菜单,我们首先需要在程序全局增加QAction其中每一个QAction则代表一个菜单选项指针。 // 全局下设置增加菜单 QAction *NewAction; QAction *InsertAction; QAction *DeleteAction; 其次则是通过代码的方式在程序中动态创建一个基础的右键菜单 *NewAction; QAction *InsertAction; QAction *DeleteAction; // By: LyShark // https://www.cnblogs.com endl; ui->plainTextEdit->appendPlainText(QString("新建触发")); }); connect(InsertAction,&QAction *NewAction; QAction *InsertAction; QAction *DeleteAction; MainWindow::MainWindow(QWidget *parent):
widgetForAction(QAction *action) const 返回与给定动作相关联的小部件。 setActiveAction(QAction *action) 设置活动动作,该动作将在菜单栏上显示为活动状态。 *pNewTask = new QAction(tr("新建菜单"), this); QAction *pEditTask = new QAction(tr("编辑菜单"), this); QAction *pDeleteTask = new QAction(tr("删除菜单"), this); // 设置属性值编号: 1=>新建 2=>设置 3=>删除 pNewTask QAction *pEven = qobject_cast<QAction *>(this->sender()); // 获取编号: 1=>新建 2=>设置 3=>删除 int
Qt 中通过 QAction 类实现菜单项,一个 QAction 对象可以同时被菜单和工具栏使用,实现功能复用。 4.1 QAction 的核心特性与 API QAction 不仅是菜单项的载体,还支持设置图标、快捷键、提示信息等,核心 API 如下: 构造函数:QAction(const QString 创建文件菜单的菜单项 QAction *actNew = new QAction(QIcon(":/icons/new.png"), "新建(&N)", menuFile); QAction 创建编辑菜单的菜单项 QAction *actUndo = new QAction("撤销(&U)", menuEdit); QAction *actRedo = new QAction ("重做(&R)", menuEdit); QAction *actCopy = new QAction("复制(&C)", menuEdit); QAction *actPaste =
("&New") self.newAction.setIcon(QIcon(":file-new.svg")) self.openAction = QAction(QIcon ", self) self.saveAction = QAction(QIcon(":file-save.svg"), "&Save", self) self.exitAction = QAction("&Exit", self) # Edit actions self.copyAction = QAction(QIcon(":edit-copy.svg "), "&Copy", self) self.pasteAction = QAction(QIcon(":edit-paste.svg"), "&Paste", self) self.cutAction = QAction(QIcon(":edit-cut.svg"), "C&ut", self) # Snip...
); //设置菜单名,将菜单设置到菜单栏中 QMenu* m = new QMenu("文件"); mb->addMenu(m); //设置菜单项,将菜单项设置到菜单当中 QAction * a1 = new QAction("打开"); QAction* a2 = new QAction("保存"); QAction* a3 = new QAction("关闭"); ::triggered,this,&MainWindow::load); connect(a2,&QAction::triggered,this,&MainWindow::save); * openAction = new QAction("open",this); QAction* newAction = new QAction("new",this); tb-> * a = new QAction("打开"); tb->addAction(a); connect(a,&QAction::triggered,[=](){ QDialog
); //设置菜单名,将菜单设置到菜单栏中 QMenu* m = new QMenu("文件"); mb->addMenu(m); //设置菜单项,将菜单项设置到菜单当中 QAction * a1 = new QAction("打开"); QAction* a2 = new QAction("保存"); QAction* a3 = new QAction("关闭"); this->setCentralWidget(te); te->setPlaceholderText("请输入"); } menubar 让我们加上对应的功能 connect(a1,&QAction ::triggered,this,&MainWindow::load); connect(a2,&QAction::triggered,this,&MainWindow::save); * openAction = new QAction("open",this); QAction* newAction = new QAction("new",this); tb->
addAction(QAction *action) 向工具栏中添加一个动作。 *pNewTask = new QAction(tr("新建菜单"), this); QAction *pEditTask = new QAction(tr("编辑菜单"), this); QAction *pDeleteTask = new QAction(tr("删除菜单"), this); // 设置属性值编号: 1=>新建 2=>设置 3=>删除 pNewTask->setData QAction *pEven = qobject_cast<QAction *>(this->sender()); // 获取编号: 1=>新建 2=>设置 3=>删除 int iType *NewAction = fileMenu->addAction("新建文件"); QAction *OpenAction = fileMenu->addAction("打开文件"); QAction
需要用到两个类QMenu和QAction,例如我的Demo程序界面如下: ? QtGuiApplication4::QtGuiApplication4(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); myAc1 = new QAction myAc1); //工具条 connect(myAc1, SIGNAL(triggered()), this, SLOT(pop1())); myAc2 = new QAction setStatusTip("This is ac2"); connect(myAc2, SIGNAL(triggered()), this, SLOT(pop2())); myAc3 = new QAction *myAc1; QAction *myAc2; QAction *myAc3; private slots: void pop1(); void pop2(); void pop3
每个QMenu对象都可以包含一个或多个QAction对象或级联的QMenu对象。 菜单中的操作按钮可以是字符串或QAction对象。菜单发射triggered信号,将该信号连接到槽函数proecesstrigger(),该函数接收信号的QAction对象。 单击任何QAction按钮时,QMenu对象都会发射triggered信号。 另外,这个信号将关联的QAction对象的引用发送到连接的槽函数上。 然后,添加具有文本标题的工具按钮,工具栏通常包含图形按钮,具有图标和名称的QAction对象将被添加到工具栏中。
("添加", this)); popMenu->addAction(new QAction("删除", this)); popMenu->addAction(new QAction("修改 ("添加", this)); popMenu->addAction(new QAction("删除", this)); if(currentMousePosHasAItem()) { popMenu->addAction(new QAction("修改", this)); } popMenu->exec(QCursor::pos()); // ("添加", this)); popMenu->addAction(new QAction("删除", this)); if(this->itemAt(QCursor::pos()) ! 记得在自己的代码总要把QAction连接到处理的slot上。上面的代码菜单是没有功能的。
为了增加菜单,我们首先需要在程序全局增加QAction其中每一个QAction则代表一个菜单选项指针。 // 全局下设置增加菜单QAction *NewAction;QAction *InsertAction;QAction *DeleteAction;其次则是通过代码的方式在程序中动态创建一个基础的右键菜单 ui_mainwindow.h"#include <QMenuBar>#include <QMenu>#include <QToolBar>#include <iostream>// 全局下设置增加菜单QAction *NewAction;QAction *InsertAction;QAction *DeleteAction;// By: LyShark// https://www.cnblogs.com/lysharkMainWindow *NewAction;QAction *InsertAction;QAction *DeleteAction;MainWindow::MainWindow(QWidget *parent): QMainWindow
*m_play; QAction *m_deleteSelect; QAction *m_openFolder; setEditTriggers(QAbstractItemView ::triggered,this,&MediaListView::play); connect(addFile,&QAction::triggered,this,&MediaListView:: ,&QAction::triggered,this,&MediaListView::loadPlaylist); connect(save,&QAction::triggered,this,&MediaListView *m_play; QAction *m_deleteSelect; QAction *m_openFolder; MediaListModel *m_model; FileFilter ,&QAction::triggered,this,&MediaListView::loadPlaylist); connect(save,&QAction::triggered,this,&MediaListView
//MainWindow.h文件 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QAction> *new_action = new QAction("新建(&N)"); QAction *open_action = new QAction("打开(&O)"); QAction * save_action = new QAction("保存(&S)"); //添加状态栏提示 new_action->setStatusTip("新建一个文件或项目"); open_action->setStatusTip("打开一个文件或项目"); save_action->setStatusTip("保存"); //添加动作到新建菜单,QAction 我们也看到了QAction的强大,它代表的是一个动作,它可以被添加到菜单中,也可以被添加到工具栏。还可以设置动作在状态栏的提示语句。