Skip to content

Prefix and Suffix properties for MLDoubleSpinBox #1

Open

Description

@romainreignier

Your implementation of the DoubleSpinBox for Qt Quick Controls 2 is very interesting. Thanks!
But in order to port code using Qt Quick Controls 1, one would need the support for the prefix and suffix properties.
As mentioned in the doc: prefix and suffix.

I have added it to the floating point SpinBox example from Qt Quick Controls 2 doc like this:

    property string prefix: ''
    property string suffix: ''
    
    [...]

    textFromValue: function(value, locale) {
        return prefix + Number(value / 100).toLocaleString(locale, 'f', decimals) + suffix
    }

    valueFromText: function(text, locale) {
        var numberStr = text.replace(suffix, '').replace(prefix, '');
        return Number.fromLocaleString(locale, numberStr) * 100
    }

Activity

  1. mpaperno commented on Aug 6, 2018

    @mpaperno
    Owner

    Hi, thanks for chiming in. Yes, this is on the list/in the works... the "tricksy" part is not having the prefix/suffix editable, and likewise for the cursor to go in the right place when item is focused (eg. you don't want to have entry after the suffix). I believe this means using an input mask, or at least that would be the simplest method I can think of. Of course you can specify an input mask already, but it will definitely be nice to add pre-baked prefix and suffix properties.

  2. added a commit that references this issue on Aug 7, 2018
  3. mpaperno commented on Aug 7, 2018

    @mpaperno
    Owner

    Well, my "tricksy" part turned out to be more difficult than expected. The input mask technique does work but it is somewhat awkward to use.

    I did add basic prefix/suffix support, pretty much as you described in the first post (just append and strip), but in editable spin boxes it has the issue I mentioned with the cursor/editing. A user can start typing before/after/in the middle of a prefix/suffix. Works nicely on non-editable ones.

    I also added a testing page (which helped find/fix other bugs), and in there I implemented an MLDoubleSpinBox subclass with prefix/suffix and an input mask. This works, but I'm not sure it makes sense to implement in the main class because the input mask has to be very specific, and I'm just not sure it's possible to cover all the possible use cases elegantly.

    The real solution is to use a custom QValidator that is aware of the prefix/suffix, which is exactly what the Controls 1 SpinBox does (QQuickSpinBoxValidator1). It would be tempting to just import that validator and use it, but Controls 1 are deprecated plus I don't want to get them mixed in here anyway. But one could use that if one wanted, I think it would just "plug into" the MLDoubleSpinBox as a custom validator (but I haven't tried).

    So eventually I'd like to add a custom C++-based validator (perhaps make it optional to use). Although at some point it will probably make sense to back the whole MLDoubleSpinBox with a C++ base, probably abstracted just like QAbstractSpinBox. Why the Qt folks didn't do this with Controls 2 in the first place I don't know (the whole issue wouldn't really exists if they had used a real base type for SpinBox2 (like SpinBox1 did) instead of int).

    I'll keep this issue open as a reference until a better solution is found.

  4. romainreignier commented on Aug 8, 2018

    @romainreignier
    Author

    Wow! Very nice analysis!
    It is true that my solution is far from optimal, yours is a lot better!
    Thank you for taking the time to implement it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions