有没有办法使用 Qt Designer 定义带有按钮和弹出菜单的 QToolbar?
我想在 Qt 中有一个工具栏,其中包含 QActions 或 QToolbarButtons,并附有弹出菜单。我已经能够在代码中完美地做到这一点,方法是创建一个 QMenu,将其添加到 QToolbarButton,然后将其添加到 QToolbar。我的问题是这应该能够完全在设计器中完成。
这是我通过代码完成的,我想在 qt 设计器中定义按钮和菜单:
我们想要用 qt 设计器做什么就是将代码与界面分离。例如,这意味着一个人可以设计表单的外观和组件,然后程序员可以接受它并对其背后的功能进行编码。如果工具栏和菜单必须由程序员设计,我们就无法非常有效地实现这一点。
对于许多应用程序来说,这似乎是一个相当常见的要求,而且我看不出 Qt 如何强制在代码中而不是设计器中完成此操作。
如果有人对如何做到这一点有任何想法,也许我在 Qt 中遗漏了一些东西?
I am wanting to have a toolbar in Qt that contains either QActions or QToolbarButtons that have popup menus attached to them. I have been able to do this perfectly in the code by creating a QMenu, adding it to a QToolbarButton and then adding that to the QToolbar. My issue is that this should be able to be done completely in designer.
This is what I have done via code, I want to define the buttons and menus in qt designer:
What we are wanting to do with qt designer is to separate the code from the interface. For example this means that one person can design the form's look and components and then a programmer can take this and code the functionality behind it. We cannot accomplish this very effectively if the toolbars and menus must be designed by the programmer.
It seems like this would be a fairly common requirement for many applications, and I can't see how Qt could have forced this to be done in code instead of designer.
If anyone has any ideas as to how this is done, maybe I'm missing something in Qt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Qt Designer 中的工具栏支持似乎有点不直观且有限。我不知道有什么方法可以将带有下拉操作的弹出菜单直接添加到 Qt Designer 中的工具栏。
您可以按如下方式添加按钮。首先,右键单击主窗口并选择“添加工具栏”(如果您还没有)。这应该在主窗口顶部添加一个非常细长的工具栏(细长是因为它是空的)。
接下来,在动作编辑器中添加一个动作。它是 Qt Designer 的窗格之一,如果没有看到它,请选择“视图”->“动作编辑器”。您可以在此处创建新操作。
接下来,将操作拖动到工具栏,以使用触发操作的按钮填充它。
我知道您想要一个弹出菜单,但据我所知,这就是您使用 Qt Designer 所能做的范围。似乎几个 其他人也得出了同样的结论。
Toolbar support in Qt Designer seems to be a little unintuitive and limited. I don't know of any way to add popup menus with dropdown actions directly to a toolbar in Qt Designer.
You can add pushbuttons as follows. First, right-click on your main window and select "Add Toolbar", if you don't have one already. This should add a VERY SLIM toolbar at the top of your main window (slim because it's empty).
Next, add an action in the Action Editor. It's one of the panes of Qt Designer, select View->Action Editor if you don't see it. You can create new actions here.
Next, drag actions to the toolbar to populate it with pushbuttons that trigger the actions.
I know you wanted a popup menu but as far as I know, that's the extent of what you can do with Qt Designer. It seems several others have come to the same conclusion.
对此有一个尴尬的技巧。您可以将所需的小部件放置在 Qt Designer 中的特殊
QWidget
(或者说QFrame
)上,并在代码中将该小部件添加到工具栏。添加到项目一个新的 Qt 类,该类名为
QMyToolbarItem
,派生自QWidget
(或任何合适的名称)。然后在 Qt 设计器中打开QMyToolbarItem.ui
。向其中添加任何复杂的小部件。您可以使用 QComboBox 或弹出按钮作为弹出菜单。 (我已经提到这很尴尬)。保存起来。在代码中,您只需将自定义小部件添加到QMainWindow
的工具栏即可:不要忘记正确设置
QMyToolbarItem
的大小策略(例如设置将
更改为所需的值)。否则工具栏上不会显示任何内容。QMyToolbarItem
的minimumSize当然,只有当您不希望将普通项目添加到工具栏时,这才可以。请注意,这样做会失去 QAction 模型的强大功能。
不管怎样,它足够合理地将 UI 设计内容与代码分开,并为工具栏上复杂的小部件设计提供了良好的能力。
最后,不要忘记考虑适当的 模型视图模式可有效地将 UI 逻辑与底层业务逻辑分开(如果您还没有这样做)。
There's an awkward trick for this. You can place the desired widgets on a special
QWidget
(or, sayQFrame
) in Qt Designer and add that widget to toolbar in code.Add to project a new Qt class named, for example
QMyToolbarItem
, derived fromQWidget
(or whatever suits). Then openQMyToolbarItem.ui
in Qt designer. Add whatever complex widgets to it. You can useQComboBox
or popup push buttons for the popup menu. (I already mentioned that this is awkward). Save it. In code, you just need to add the custom widget toQMainWindow
's toolbar:Don't forget to set size policies of
QMyToolbarItem
properly (for example set theminimumSize
ofQMyToolbarItem
to a desired value). Otherwise nothing is displayed on toolbar.Of course this is OK only when you want not normal items be added to toolbar. Note that by doing so, you lose the great power of the QAction model.
Anyway, it reasonably enough separates the UI designing stuff from code, and gives good power over complex widget design on toolbar.
At last, don't forget to consider appropriate Model View patterns to efficiently separate UI logic from the underlying business logic, if you didn't already.
2021 v6.2 在学习此框架和 qtDesigner 的 mac 上遇到了同样的事情。添加了菜单栏,但在运行时未显示。我尝试了上面的几种想法,它们都有效,但想添加启用菜单的 qtDesigner 方式。选择 QMenuBar 并向下滚动,确保选中 nativeMenuBar。这与在 mainwindow.cpp 中使用 setNativeMenuBar(false) 手动设置此属性值是一样的; (但当然在两个地方都这样做是多余的)
2021 v6.2 ran into the same thing on a mac learning this framework and qtDesigner. Added the menu bar but didn't show up on run. I tried several above ideas and they all worked, but would like to add the qtDesigner way of enabling the menu. Select the QMenuBar and scroll down, make sure nativeMenuBar is checked. This is the same thing as manually setting this property value in mainwindow.cpp with setNativeMenuBar(false); (but of course would be redundant to do it in both places)
在 Qt Creator 的左上角,当您设计 GUI 时,应该有一个框,其中包含“mainToolBar”和“menuBar”等内容。确保有类似的东西。然后,在 Qt Creator 窗口的底部,有一个由 Signals & 占据的空间。 “插槽编辑器”选项卡和“动作编辑器”选项卡。转到操作编辑器,然后选择“新建操作”选项。这将让您创建一个动作。
这是您要找的吗?
On the top left in Qt Creator, when you are designing a GUI, there should be a box which includes stuff like "mainToolBar" and "menuBar". Make sure something similar to this is there. Then, at the bottom of the Qt Creator window, there is a space occupied by a Signals & Slots Editor tab and an Action Editor tab. Go to the action editor, and select the New Action option. This will let you create an action.
Is this what you were looking for?