QMenu中的非交互式项目 [英] Non interactive items in QMenu

查看:147
本文介绍了QMenu中的非交互式项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个菜单,其中一些项目在QT中不是交互式的。我在我的MyCustomMenuClass子类QMenu。

I'm trying to build a menu with some items that are not interactive in QT. I subclass QMenu in my MyCustomMenuClass. I'm trying to add section titles to my menu so that it's clearer for the user.

例如,它应该看起来像这样:

For example, it should look like this:

My section 1 title
Action 1
Action 2
Action 3
My second section title
Action 4
Action 5

问题是章节标题总是对鼠标,但我希望他们不要对鼠标作出反应,使它更漂亮。有关如何执行此操作的任何想法吗?

The issue is that the section titles always react to the mouse, but I would like them to not react to a mouse over so that it would be prettier. Any idea on how to do it?

推荐答案

从QMenu文档:


有四种操作项:分隔符,显示子菜单的操作,窗口小部件和执行操作的操作。分隔符用addSeparator()插入,子菜单用addMenu(),所有其他项目都被视为动作项。

There are four kinds of action items: separators, actions that show a submenu, widgets, and actions that perform an action. Separators are inserted with addSeparator(), submenus with addMenu(), and all other items are considered action items.

:小部件!你可以添加一个小部件到菜单?

This rings a bell: Widgets! You can add a widget to the menu? That means you are settled, you can do whatever you want.

您需要的是一个 QWidgetAction 对象。它允许您插入自定义窗口小部件作为操作。您的标题将是自定义小部件。如果你只需要一个标题, QLabel 应该足够了:

What you need is a QWidgetAction object. It allows you to insert a custom widget as an action. Your titles will be custom widgets. If you only need a title, a QLabel should suffice:

QMenu* myMenu = new QMenu(...);
QLabel* label = new QLabel(tr("<b>Title</b>"), this);
label->setAlignment(Qt::AlignCenter);

QWidgetAction* a = new QWidgetAction(myMenu);
a->setDefaultWidget(label);

- 此代码的来源

有关更复杂的示例代码,请参阅此相关问题:是否有一种方法来添加一个小部件QtCreator中的QMenu

See this related question for more sophisticated example code: Is there a way to add a Widget to a QMenu in QtCreator

这篇关于QMenu中的非交互式项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆