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

Inconsistent QString::isNull behavior in QJSEngine::evaluate result and QJSValue constructed from QString

XMLWordPrintable

    • macOS, Windows
    • df684931c (dev), 14bb25f69 (6.8)

      We encountered an issue where a value returned from QJSEngine::evaluate returns a null QString. This caused problems when writing the result to a SQL database where we get NULL instead of an empty string (Previously we used QScriptEngine where it worked as expected).

      This behavior is inconsistent with a QJSValue constructed with either an empty or null QString. In that case value.toString().isNull() is false.

      I would expect the same for the evaluate result.

      Here's a small example:

      #include <QCoreApplication>
      #include <QJSEngine>
      #include <QJSValue>
      #include <QString>
      #include <QVariant>
      
      int main(int argc, char *argv[]) {
        QCoreApplication app(argc, argv);
        QJSEngine engine;
        const auto evalResult = engine.evaluate(R"(
      function f() {
        return "";
      }
      f();
      )");
      
      	const auto evalResultStr = evalResult.toString();
      	qDebug() << "QJSEngine::evaluate  : " << (evalResultStr.isNull() ? "null" : "not null");
      
      	QJSValue jsValueNullStr = QString{};
      	qDebug() << "QJSValue(QString())  : " << (jsValueNullStr.isNull() ? "null" : "not null");
      
      	QJSValue jsValueEmptyStr = QStringLiteral("");
      	qDebug() << "QJSValue(QString(\"\")): " << (jsValueEmptyStr.isNull() ? "null" : "not null");
      }
      

      This is the output:

      QJSEngine::evaluate  :  null
      QJSValue(QString())  :  not null
      QJSValue(QString("")):  not null
      

        For Gerrit Dashboard: QTBUG-125611
        # Subject Branch Project Status CR V

            qtqmlteam Qt Qml Team User
            lbuckel Leon Buckel
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved:

                There are no open Gerrit changes