Qt 工具栏QToolBar添加带有弹出菜单的QAction

void QAction::setMenu(QMenu *menu)

函数解释:Sets the menu contained by this action to the specified menu. Actions that contain menus can be used to create menu items with submenus, or inserted into toolbars to create buttons with popup menus.

翻译:设置action所包含的菜单。 包含菜单的action可用于创建带有子菜单的菜单项,或插入到工具栏中以创建带有弹出菜单的按钮。

示例:

    //创建QAction
    QAction *file_newAct = new QAction(tr("&New"), this);
    QAction *file_openAct = new QAction(tr("&Open"), this);
    QAction *file_saveAct = new QAction(tr("&Save"), this);
    QAction *file_saveAsAct = new QAction(tr("Save &As"), this);

    //创建菜单栏
    QMenu *fileMenu = menuBar()->addMenu(tr("&FileM"));
    fileMenu->addAction(file_newAct);
    fileMenu->addAction(file_openAct);
    fileMenu->addAction(file_saveAct);
    fileMenu->addAction(file_saveAsAct);

    //创建工具栏
    QToolBar *toolbar = new QToolBar(this);
    this->addToolBar(Qt::TopToolBarArea, toolbar);
    QAction *fileAct = new QAction(tr("FileA"), this);
    fileAct->setMenu(fileMenu);
    toolbar->addAction(fileAct);

效果:

               

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值