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

QTextDocument::find() does not work well with QRegularExpressions

    XMLWordPrintable

Details

    • All
    • 3ec9331c3 (dev), ecaf35b4c (6.7), 891fd9cbb (6.6)

    Description

      When I use `QTextDocument::find()` with regular expression I get some very unexpected results.
       

      #include <QTextDocument>
      #include <QTextCursor>
      #include <QRegularExpression>
      #include <QDebug>
      int main() {
        QTextDocument doc;
        doc.setPlainText("A bug?");
        QRegularExpression regExp("BUG"); // regexp is case sensitive by default, unless I set on QRegularExpression::CaseInsensitiveOption
        qInfo() << doc.find(regExp).selectedText(); // Shockingly it writes "bug"!!!
      }
      

       
      The expected result would be that the result would return invalid text cursor because the regexp is case sensitive. But it returns "bug" despite the fact I was searching for "BUG", case sensitively. I took me quite a lot of time to find why this happens. Now I know the root cause of this: there is a default FindFlags parameter in QTextDocument::find() which by default specifies case insensitivity and this has curiously priority over the options in regular expression. Which I think is wrong because it gives totally unexpected results.

      When the overload with regular expression is used, I think the default case insensitivity FindFlag in find() overload with regexp should be ignored. Or some other solution if backward compatibility is an issue, which probably is. Ideally the current overload should be deprecated and replaced with something like find(QRegularExpression regexp, QTextCursor cursor, bool forward=true) but this could also be a problem with backward compatibility because implicit conversions of enum to bool, I guess. Introducing strongly typed enum for the last argument should solve it. So solution like find(QRegularExpression regexp, QTextCursor cursor, QTextDocument::SearchDirection direction=QTextDocument::SearchForward) should work well, I think. Of course this overload should respect the case sensitivity specified in the regular expression.

      Attachments

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

        Activity

          People

            esabraha Eskil Abrahamsen Blomfeldt
            vladimir.kraus Vladimir Kraus
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There is 1 open Gerrit change