QComboBox 与 QSpinBox 融合

发布于 2024-12-06 20:41:45 字数 251 浏览 1 评论 0原文

因此,我正在为一些科学软件编写 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 技术交流群。

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

发布评论

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

评论(1

风吹短裙飘 2024-12-13 20:41:45

如果融合意味着旋转框和组合框之间的距离应该为零,那么您应该创建 QProxyStyle 后代,重新实现 styleHint 以在要求 QStyle::PM_LayoutHorizo​​ntalSpacing 时返回 -1,并声明一个名为 的槽>layoutSpacingImplementation(阅读QStyle 文档)正在接收两种小部件类型(代表它们枚举)。

MyClass::layoutSpacingImplementation( QSizePolicy::ControlType control1, 
                                      SizePolicy::ControlType control2, 
                                      Qt::Orientation orientation,
                                      const QStyleOption * option = 0,
                                      const QWidget * widget = 0 ) const
{
    if (orientation == Qt::Horizontal &&
        ((control1 == QSizePolicy::SpinBox && control2 == QSizePolicy::ComboBox) || 
         (control2 == QSizePolicy::SpinBox && control1 == QSizePolicy::ComboBox)))
               return 0;

    int spacing = baseStyle()->styleHint(QStyle::PM_LayoutHorizontalSpacing);
    if (spacing >= 0) return spacing;
    return baseStyle()->layoutSpacing(control1, control2, orientation, option, widget);
}

然后,您为融合项目所在的小部件设置此样式,使用其先前的样式作为基本样式。

这会将所有这些元素融合在一起,因此最好在不应该融合的元素之间添加一些额外的空格。这是一个小问题,但不幸的是,文档没有说明哪个控件描述左控件或右控件(我猜这意味着空格应该与顺序无关),因此没有其他方法可以实现它。

编辑:

刚刚看到发布的图片。标准 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 called layoutSpacingImplementation (read QStyle documentation) that is recieving both widget types (as representing them enums).

MyClass::layoutSpacingImplementation( QSizePolicy::ControlType control1, 
                                      SizePolicy::ControlType control2, 
                                      Qt::Orientation orientation,
                                      const QStyleOption * option = 0,
                                      const QWidget * widget = 0 ) const
{
    if (orientation == Qt::Horizontal &&
        ((control1 == QSizePolicy::SpinBox && control2 == QSizePolicy::ComboBox) || 
         (control2 == QSizePolicy::SpinBox && control1 == QSizePolicy::ComboBox)))
               return 0;

    int spacing = baseStyle()->styleHint(QStyle::PM_LayoutHorizontalSpacing);
    if (spacing >= 0) return spacing;
    return baseStyle()->layoutSpacing(control1, control2, orientation, option, widget);
}

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.

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