QComboBox 与 QSpinBox 融合
因此,我正在为一些科学软件编写 Qt GUI,并且我有 QSpinBoxes 和 QDoubleSpinBoxes 用于用户应设置单位的数量。因为我连续有几个这样的 spinbox-combobox 对,所以我认为将右侧的 ComboBox 与左侧的 SpinBox 融合起来最不会造成混乱。
我已经尝试使用样式表进行此操作,但当我这样做时,我失去了塑料风格。
什么是[最好是非笨拙的]方法来做到这一点?
谢谢,
安德烈亚斯
So I am writing a Qt GUI for some scientific software and I have QSpinBoxes and QDoubleSpinBoxes for a quantities that user should set the units of. Because I have several of these spinbox-combobox pairs in a row I think it would be least confusing to have the ComboBox on the right fused to the SpinBox on the left.
I have attempted this with stylesheets but I am losing the plastique style when I do this.
What would be a [preferably non-kludgy] way to do this?
Thanks,
Andreas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果融合意味着旋转框和组合框之间的距离应该为零,那么您应该创建 QProxyStyle 后代,重新实现 styleHint 以在要求
QStyle::PM_LayoutHorizontalSpacing
时返回 -1,并声明一个名为的槽>layoutSpacingImplementation
(阅读QStyle 文档)正在接收两种小部件类型(代表它们枚举)。然后,您为融合项目所在的小部件设置此样式,使用其先前的样式作为基本样式。
这会将所有这些元素融合在一起,因此最好在不应该融合的元素之间添加一些额外的空格。这是一个小问题,但不幸的是,文档没有说明哪个控件描述左控件或右控件(我猜这意味着空格应该与顺序无关),因此没有其他方法可以实现它。
编辑:
刚刚看到发布的图片。标准 qt 中没有这样的控制,并且通过典型方法实现它是不太可能的。您可能会尝试创建自己的布局类或您自己的 QWidget 后代,这将使您的旋转框部分覆盖组合框(准确地说是覆盖左圆边部分),这样会产生融合的错觉。然而,如果用户使用组合框边缘较宽的样式,这可能看起来很难看,这会在 sping 框的侧面“看起来”。这是一个快速且非常丑陋的解决方法。
除此之外,我认为您唯一的选择是创建自己的小部件 - 但即使使用 QStyle 的绘画功能(我猜主要是drawControl)和实现单独的 QSpingBox 和 QCombobox 的代码片段,这也意味着大量的工作。考虑到你想要实现的只是 UI 中的简单图形效果,我想说这是不值得的。
If by fused you mean that distance between spinbox and combobox should be zero, then you should create QProxyStyle descendant, reimplement styleHint to return -1 if asked for
QStyle::PM_LayoutHorizontalSpacing
, and declarie a slot calledlayoutSpacingImplementation
(read QStyle documentation) that is recieving both widget types (as representing them enums).Then you set this style for widget those fused items lay on, using it's previous style as base style.
This will fuse all those elements together, so it would be best if you added some additional spaces between elements that shouldn't be fused. That is a small problem, but unfortunately documentation does not state which control describes left or right control (which i guess means that spaces are supposed to be ordering-independent), so there is no other way to achieve it.
EDIT:
Just seen posted image. There is no such control in standard qt, and achieving it by typical means is rather unlikely. You might try to create your own layout class or your own QWidget descendant, which will have your spinbox partially covering combobox (covering left round edges part to be exact), this way creating illusion of fusing. This however might look ugly if user uses style with wider edges of combobox, which will "look out" on the side of a sping box. This is quick and veeeeeery ugly workaround.
Apart from that I think the only option you have is to create your own widget - but even using painting functions from QStyle (mostly drawControl I guess) and scraps of code implementing separate QSpingBox and QCombobox this will mean lots and lots of work. Considering that what you want to achieve is simple graphical effect in UI, i would say that this is not worth the effort.