C++ 中的动态 QT4 菜单

发布于 2024-10-07 01:19:19 字数 483 浏览 0 评论 0原文

我在 Linux 上使用 C++ 和 QT4。我希望在运行时将项目添加到菜单栏。我的包含菜单栏的主 GUI 是使用 QT Designer 设计的,并在下面的第一行代码中进行设置。

在我的主窗口构造函数中,我有以下测试代码,它可以正常编译。但是,执行程序时不会出现新的子菜单栏及其项目。我猜想 Qt 需要被告知以某种方式更新菜单栏。有什么想法吗?

 // Setup the user interface
 m_ui.setupUi(this);

 QMenu * iObjectsMenu = new QMenu(tr("Objects"), m_ui.menubar); //m_ui.menubar.menu_View->addMenu(tr("Objectz"));
 QAction * menu_testAction = new QAction(tr("Test"), m_ui.menubar);
 iObjectsMenu->addAction(menu_testAction);

I am using C++ and QT4 on Linux. I wish to add items to the menubar at runtime. My main GUI containing the menubar was designed with QT Designer and setup in the first line of code below.

In my main window constructor I have the following test code and it compiles fine. However, the new submenu bar and its item do not appear when the program is executed. I'm guessing Qt needs to be told to update the menubar somehow. Any ideas?

 // Setup the user interface
 m_ui.setupUi(this);

 QMenu * iObjectsMenu = new QMenu(tr("Objects"), m_ui.menubar); //m_ui.menubar.menu_View->addMenu(tr("Objectz"));
 QAction * menu_testAction = new QAction(tr("Test"), m_ui.menubar);
 iObjectsMenu->addAction(menu_testAction);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽梦忆笙歌 2024-10-14 01:19:19

按照你的做法,你需要将菜单放在某个地方,但你还没有这样做。

我的做法有点不同。当您在设计器中制作菜单时,它们在 ui 成员中具有指针。然后您可以非常轻松地操作该菜单。

例如,我想要一个包含所有可以显示/隐藏的工具栏和停靠窗口的菜单。我查看了 QMainWindow::createPopup() 的代码,并制作了自己的外部函数 construct_view_menu(QMainWindow *parent, QMenu * view_menu)。它是在主窗口初始化期间使用 construct_view_menu(this, ui.menu_View) 调用的。函数只是将更多菜单和操作附加到该菜单。 menu_View 只是我与设计器创建的一个空菜单。

我发现这是最简单的方法,但您也可以创建新菜单,然后将它们附加或插入到菜单栏中;这是你忽略的最后一步。

The way you're doing it, you need to put the menu somewhere and you haven't done so.

I do it a little differently. When you make menus in the designer they have pointers in the ui member. You can then manipulate that menu quite easily.

For example, I wanted a menu that contains all the toolbars and dock windows that can be shown/hidden. I looked at the code that makes QMainWindow::createPopup() and made my own external function construct_view_menu(QMainWindow * parent, QMenu * view_menu). It's called during the main window initialization with construct_view_menu(this, ui.menu_View). Function just appends more menus and actions to that menu. The menu_View is just an empty menu I created with the designer.

I found this the easiest way to go about it, but you can also create new menus and then append or insert them into the menubar; it's this latter step you've neglected.

儭儭莪哋寶赑 2024-10-14 01:19:19

我只是在你的设置代码中生成菜单而不是用户界面,保留一个指向 QMenu 的成员变量(用 new 分配)

I would just generate the menu in your setup code rather than the ui, keep a member variable that points to the QMenu (allocated with new)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文