Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QML TextEdit, validator, regex with \pL — how to do it?
Forum Updated to NodeBB v4.3 + New Features

QML TextEdit, validator, regex with \pL — how to do it?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
6 Posts 3 Posters 144 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    barbicels
    wrote last edited by barbicels
    #1

    Following up on an older discussion:

    I need to use Unicode-aware (\pL-style) letter validation in a [Edit: multi-line] TextEdit item, which does not support a validator property as QML's single-line text items do, and was surprised to find that the ECMA implementation is too old to recognize Unicode character property escapes in regexes (added in 2018). Because of that, a block of script code in an onTextChanged handler can't do the same validation that a RegularExpressionValidator can.

    (Qt's documentation explains: "Note that QML parses JavaScript regular expressions, while Qt's QRegularExpression class' regular expressions are based on Perl regular expressions." The problem I have is that the JavaScript REs are feature-limited due to QML's pre-2018 ECMA implementation.)

    Any suggestions, better than "pick out the bad characters"? I need this code to be fully Unicode-aware and not specific to the letter set of any one language. I would be most grateful for a working example!

    Note: I've tried using XRegExp/regex+, but either it's not suited for use in QML or I'm not understanding it properly.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      barbicels
      wrote last edited by
      #2

      One approach that sorta-kinda works is to feed the changed text into a hidden TextField item that has a Perl-style RegularExpressionValidator, by setting its text property and then reading its acceptableInput property.

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zvoopz
        wrote last edited by
        #3

        QRegularExpression in Qt does support Unicode properties like \p{L}
        This means in C++ you can write a filter to allow only Unicode letters.

        B 1 Reply Last reply
        0
        • Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote last edited by Axel Spoerl
          #4

          Just use a TextField instead of a TextEdit. It supports validators out of the box.

          Software Engineer
          The Qt Company, Oslo

          B 1 Reply Last reply
          0
          • Axel SpoerlA Axel Spoerl

            Just use a TextField instead of a TextEdit. It supports validators out of the box.

            B Offline
            B Offline
            barbicels
            wrote last edited by
            #5

            @Axel-Spoerl That's good for single-line text handling, but in my case (as in the discussion I linked) I require a multi-line text item. I am currently using an invisible dummy TextField in the way you describe (see comment #2 above), but, apart from the code ugliness, I have difficulties with things like restoring the previous text selection bounds after rejecting an invalid character, and there is no "fixup" capability that would let me accept a paste while rejecting any included invalid characters.

            1 Reply Last reply
            0
            • Z zvoopz

              QRegularExpression in Qt does support Unicode properties like \p{L}
              This means in C++ you can write a filter to allow only Unicode letters.

              B Offline
              B Offline
              barbicels
              wrote last edited by
              #6

              @zvoopz Right, that's what I do in my non-QML widget code. For my QML code, exposing a C++ object class may be the best solution, if only because QRegularExpressionValidator can do an intelligent "fixup" that would let me accept a paste while filtering out any invalid characters, but it's odd to me that [a] TextEdit still doesn't have a validator property, and [b] QML is still stuck on ECMA 7th edition, which is now nine years old.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved