qt QAction详解

1、概述

QAction是Qt框架中的一个抽象类,用于表示用户界面中的一个动作(action)。这些动作可以绑定到菜单项、工具栏按钮或快捷键上,提供了一种灵活的方式来处理用户交互。QAction不仅包含了动作的名称、图标、提示信息等属性,还可以与特定的回调函数关联,当动作被触发时,执行相应的回调函数。

QAction是Qt信号与槽机制的一个典型应用,它允许开发人员将动作与用户界面元素解耦,从而更容易地管理和维护代码。通过使用QAction,开发人员可以创建一致的、可重用的用户界面元素,提升应用程序的可维护性和用户体验。

2、重要方法
  • setText(const QString &text):设置动作的文本标签。
  • setIcon(const QIcon &icon):设置动作的图标。
  • setShortcut(const QKeySequence &shortcut):设置动作的快捷键。
  • setStatusTip(const QString &statusTip):设置动作的状态提示信息,通常显示在状态栏中。
  • setToolTip(const QString &tip):设置动作的工具提示信息,当用户将鼠标悬停在动作上时显示。
  • setWhatsThis(const QString &text):设置动作的"这是什么"帮助信息,当用户按下Shift+F1并悬停在动作上时显示。
  • triggered(bool checked = false):这是一个信号,当动作被触发时发出。在子类中可以重写此方法以提供自定义行为。
  • connect():通常与triggered信号一起使用,将动作与特定的回调函数关联。
3、重要信号
  • triggered(bool checked = false):当动作被触发时发出此信号。如果动作是可检查的(checkable),则checked参数指示动作是否被选中。

    #include
    #include
    #include
    #include
    #include
    #include

    class MainWindow : public QMainWindow {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) {
    // 创建菜单
    QMenuBar *menuBar = this->menuBar();
    QMenu *fileMenu = menuBar->addMenu(tr("&File"));

    复制代码
          // 创建动作  
          QAction *newAction = new QAction(tr("&New"), this);  
          newAction->setIcon(QIcon(":/icons/new.png"));  
          newAction->setStatusTip(tr("Create a new file"));  
          connect(newAction, &QAction::triggered, this, &MainWindow::onNewFile);  
    
          QAction *openAction = new QAction(tr("&Open..."), this);  
          openAction->setIcon(QIcon(":/icons/open.png"));  
          openAction->setStatusTip(tr("Open an existing file"));  
          connect(openAction, &QAction::triggered, this, &MainWindow::onOpenFile);  
    
          // 将动作添加到菜单  
          fileMenu->addAction(newAction);  
          fileMenu->addAction(openAction);  
    
          // 创建工具栏  
          QToolBar *toolBar = this->addToolBar(tr("Main Toolbar"));  
          toolBar->addAction(newAction);  
          toolBar->addAction(openAction);  
      }  

    private slots:
    void onNewFile() {
    QMessageBox::information(this, tr("New File"), tr("Create a new file..."));
    }

    复制代码
      void onOpenFile() {  
          QMessageBox::information(this, tr("Open File"), tr("Open an existing file..."));  
      }  

    };

    int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    复制代码
      MainWindow window;  
      window.show();  
    
      return app.exec();  

    }

觉得有帮助的话,打赏一下呗。。

相关推荐
ShineWinsu2 小时前
对于Qt:信号与槽的解析
c++·qt·gui·lambda·信号·信号与槽·槽函数
努力实干12 小时前
Qt/C++ 模块化设计与工程组织
开发语言·c++·qt
Lhan.zzZ13 小时前
Qt:动态库模块框架设计
开发语言·c++·qt
qq_401700411 天前
TCP 长连接、心跳包与断线自动重连设计:工业级通信的“不死鸟”架构
网络·qt·tcp/ip
dyxal2 天前
A2A 协议 v1.0 完全指南:方法名、Agent Card、三种绑定与 FastAPI 实战
开发语言·qt·fastapi
金色熊族2 天前
在Qt中使用asan检测内存泄漏(一)
linux·qt·asan
前行中的小猪2 天前
Qt 5 踩坑实录:多显示器跨屏拖拽导致窗口异常放大与 setGeometry 报错的深度解析与根治
qt·多显示器·高分辨率·多屏幕
想做小南娘,发现自己是女生喵2 天前
QT项目实战第一周(嵌入式)
开发语言·qt
誰能久伴不乏2 天前
【无标题】
c++·qt·机器人
金色熊族2 天前
在Qt中使用asan检测内存泄漏(二)--检查依赖库的内存使用
linux·qt·asan