Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-116808

QML does not handle regular expression as expected

XMLWordPrintable

      The following regular expression matches everything that does not contain Chinese characters:

      [^\u4e00-\u9fa5]* //Unicode 4e00 ~9fa5 is the range for most of Chinese characters. ^ to negate the whole range. * to match everything else.
      

      It worked perfectly on C++ side, e.g. as regexp of a validator for QLineEdit. The QLineEdit then allows input of everything but Chinese. But not so on QML side.

      The following QML code actually allows Chinese characters for TextInput but strangely not numbers:

      TextInput {
              id: input
              anchors.fill: parent
              anchors.margins: 1
              //Extra backslash is used to escape otherwise QML complains about invalid format
              validator: RegularExpressionValidator { regularExpression:  /[^\\u4e00-\\u9fa5]*/ }
      }
      

      Temporary workaround:
      1. Set validator from C++ side, e,g.

      auto r = engine.rootObjects().at(0)->findChild<QRegularExpressionValidator*>("myRegexp");
      r->setRegularExpression(QRegularExpression("[^\u4e00-\u9fa5]*"));
      

      2. On QML side, use 'new' to create regexp instead of default // syntax, e.g.

      regularExpression:  new RegExp("[^\u4e00-\u9fa5]*")
      

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            vgt Eirik Aavitsland
            luqiaochen Luqiao Chen
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes