Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix and Suffix properties for MLDoubleSpinBox #1

Open
romainreignier opened this issue Aug 6, 2018 · 3 comments
Open

Prefix and Suffix properties for MLDoubleSpinBox #1

romainreignier opened this issue Aug 6, 2018 · 3 comments

Comments

@romainreignier
Copy link

romainreignier commented Aug 6, 2018

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
    }
@mpaperno
Copy link
Owner

mpaperno commented Aug 6, 2018

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.

mpaperno added a commit that referenced this issue Aug 7, 2018
 + Add prefix and suffix properties (see notes on limitations, see issue #1);
 + Add cleanText property and getCleanText();
 + Add new RegExp-based validator as an option;
 + Add util functions for escaping RegEx and input mask strings.
@mpaperno
Copy link
Owner

mpaperno commented Aug 7, 2018

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.

@romainreignier
Copy link
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
Projects
None yet
Development

No branches or pull requests

2 participants