Qt Designer 的自定义操作插件
我计划在工具栏(如下)中的“暂停”和“设置”按钮之间使用间隔符,以使“设置”按钮在 QToolBar
内右对齐。下面的分隔符没有切割它。
以下方法显然有效,尽管我还没有实际尝试过:(
QWidget* spacer = new QWidget();
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// toolBar is a pointer to an existing toolbar
toolBar->addWidget(spacer);
toolBar->addAction("Right-aligned button");
感谢 http://www.ffuts.org/blog/right-aligning-a-button-in-a-qtoolbar/)
Qt Designer 似乎不允许向 QToolBar 添加小部件,尽管 QToolBar
确实有一个 addWidget()
方法,如上面所使用的。因此,如果我希望能够使用 Qt Designer(使用插件,而不是升级)将间隔符添加到工具栏,我应该子类化 QAction
、QWidget
或 QWidgetAction
?有没有办法为 Qt Designer 编写自定义操作插件?
I plan to use a spacer in my toolbar (below) between the 'Pause' and 'Settings' buttons to make the 'Settings' button right-aligned within the QToolBar
. The separator below isn't cutting it.
The following method apparently works, although I haven't actually tried it yet:
QWidget* spacer = new QWidget();
spacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// toolBar is a pointer to an existing toolbar
toolBar->addWidget(spacer);
toolBar->addAction("Right-aligned button");
(thanks to http://www.ffuts.org/blog/right-aligning-a-button-in-a-qtoolbar/)
Qt Designer doesn't seem to allow adding widgets to a QToolBar
, although QToolBar
does have an addWidget()
method, as used above. So if I want to be able to add the spacer to my toolbar using Qt Designer (with a plugin, not promotion), should I subclass QAction
, QWidget
, or QWidgetAction
? Is there even a way to write custom action plugins for Qt Designer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能继承 QAction 并期望在 Qt Creator/Designer 中优雅地使用它。它使用用户界面中的标签并将它们添加到菜单和工具栏。您无法像其他小部件一样指定 class="customclass" 元素以及条目。因为操作的处理方式不同,所以你甚至不能对它们进行子类化,所以这是不可能的。
至于 QWidgetAction/QWidget,如果您明白我的意思,那么您与 QActionGroup 处于同一条船上。
(我个人认为这是表单设计器设计中的一个很大的忽视!)因此在设计器中预览这一点也已经过时了。
你能做的最后一件事(不可见的选项3)是QToolbar子类化,但可惜你可以子类化它,但是,除了普通的旧QToolbar可以做的事情之外,你在设计器中看不到任何东西。这并不意味着它完全消失了,这就是我要说的:
真正的答案必须是不可见的选项 4,将 QToolbar 子类化为设计器插件(如果您还没有编写,这将需要您编写大量代码)知道那里有一个,我不知道有哪个真正有设计器插件)表单设计器插件并不常见,但您可以找到某人的子类工具栏,然后自己将其制作为插件,从而节省了一步( qt 创建者甚至有一个模板),然后在 qt 设计器和您的集合中使用它。
我知道这个问题是两年前被问到的,但是当我看到一个未回答的问题时,我想到那些可能会阅读并因死胡同而感到沮丧的搜索者,所以如果我能做出贡献,我会这样做……我希望我至少能有所帮助您或其他任何人都走在正确的道路上,祝大家好运!
You cannot subclass QAction and expect to use it gracefully in Qt Creator/Designer. It uses the tags in the ui and to add them to menus and toolbars. You can't specify a class="customclass" element along with a entry like other widgets. Because actions are treated differently, you cant even subclass them so that's out.
As for QWidgetAction/QWidget, you are in the same boat as QActionGroup, if you know what I mean.
(I personally think this is a big overlook in the design of the form designer!) So previewing that in the designer is also out.
The last thing you could do (invisible option 3) is QToolbar subclassing, but alas you can subclass it, but still, you wont be seeing anything in the desinger other than what a plain old QToolbar can do. That doesnt mean that it is completely out which is the point I'm coming to:
The real answer would have to be invisible option 4, subclassing QToolbar as a designer plugin (which would require a lot of coding on your part if you dont already know of one that is out there, I dont know of any that actually have designer plug-ins) Form Designer plugins are more not than often, but you could find someone's subclassed toolbar, and make it into a plugin yourself saving you a step (qt creator even has a template for that), then use it in qt designer and your set.
I know this question got asked 2 years ago but when I see an unanswered question, I think of those searchers that might read and get frustrated by a dead-end so if I can contribute I do... I hope I did at least help you or anyone else get on the right track, and good luck to you all!