【问题标题】:Qt5 with MacOS : How to add entries in the application menu?Qt5 with MacOS:如何在应用程序菜单中添加条目?
【发布时间】:2015-01-08 13:05:55
【问题描述】:

我正在使用 Qt5 编写应用程序。该应用可在 Window、Linux 和现在的 MacOS 上运行。

为了确保我的应用符合苹果的要求,我想在应用菜单中添加条目(首选项、关于...)。

有人知道怎么做吗?

old doc about Qt4.8 描述了菜单由 Qt 处理,但没有描述要做什么。

【问题讨论】:

  • 使用QAction::MenuRole property 描述该操作在 Mac OS X 上的应用程序菜单中所扮演的角色。
  • @Linville:有效!谢谢。请随时发布官方答案,以便我检查。

标签: macos qt5


【解决方案1】:

我看到了这个问题,正在寻找答案。 cmets 很有帮助,但这里有一个更完整的代码示例,应该可以帮助其他人。

ma​​inwindow.h:添加这些包含语句

#include <QMenu>
#include <QAction>

...

private:
    Ui::MainWindow *ui;
    QMenu *mainMenu;
    QAction *aboutAction;

ma​​inwindow.cpp:初始化函数

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    this->aboutAction = new QAction(0);
    this->aboutAction->setMenuRole(QAction::AboutRole);
    ui->setupUi(this);
    this->mainMenuBar = new QMenuBar(0);
    this->mainMenu = new QMenu(0);
    this->mainMenuBar->addMenu(this->mainMenu);
    this->mainMenu->addAction(this->aboutAction);
    this->setMenuBar(this->mainMenuBar);
}

使用 QT 5.3.2 和 Snow Leopard OS X 10.6.8、QT Creator 3.0.1。

由于您使用的是AboutRole,因此无需指定QAction 的QString 参数。它会自动默认为您想要的。

【讨论】:

  • 如果您想要实际功能,您必须将 About QAction 连接到一个函数以显示模式窗口。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-19
  • 2017-05-15
  • 1970-01-01
  • 2020-05-05
  • 2021-02-13
  • 2016-02-14
  • 1970-01-01
相关资源
最近更新 更多