开发者

QComboBox fused to QSpinBox

So I am writing a Qt GUI for som开发者_高级运维e 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜