Qt 动作(QAction)

   Qt 使用QAction类作为动作,QAction包含了图标、菜单文字、快捷键、状态栏文字、浮动帮助等信息,Qt自己选择使用哪个属性来显示,无需我们关心。同时,Qt 能够保证把QAction对象添加到不同的菜单、工具栏时,显示内容是同步的。也就是说,如果我们在菜单中修改了QAction的图标,那么在工具栏上面这个QAction所对应的按钮的图标也会同步修改。

#include "mainwindow.h"
#include <QIcon>
#include <QAction>
#include <QMenuBar>
#include <QMessageBox>
#include <QToolBar>
#include <QStatusBar>
#include <iterator>
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    CreateQButton();
}
MainWindow::~MainWindow()
{
    if(openAction) delete openAction;
}
void MainWindow::CreateQButton()
{
    setWindowTitle(tr("Main Window"));
 
    openAction = new QAction(QIcon(), "打开...", this);//new 了之后要释放内存(delete),不然会造成内存泄漏
    openAction->setShortcuts(QKeySequence::Open);//说明这个QAction的快捷键Ctrl+O,也可以自定义
    openAction->setStatusTip(tr("Open an existing file"));//etStatusTip()实现了当用户鼠标滑过这个 action 时,会在主窗口下方的状态栏显示相应的提示
    connect(openAction, &QAction::triggered, this, &MainWindow::open);
 
    QMenu *file = menuBar()->addMenu("文件");
    file->addAction(openAction);
 
    QToolBar *toolBar = addToolBar("SSS");
    toolBar->addAction(openAction);
 
    QStatusBar *statBar = statusBar();
    statBar->addAction(openAction);
}
void MainWindow::open()
{
    QMessageBox::information(this, tr("信息"), tr("功能暂未实现"));
}
 
 
 
 
 
 
 
 

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值