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

QWebEngineScript::setSourceUrl doesn't load scripts from qrc

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P1: Critical
    • Resolution: Done
    • Affects Version/s: 6.2.0 Beta4
    • Fix Version/s: 6.2.0, 6.2.1
    • Component/s: WebEngine
    • Labels:
      None
    • Platform/s:
      All
    • Commits:
      ce2d6a94ce99f8d6f82fbcbf45603f8eb7a92957 (qt/qtwebengine/6.2) 5838af270052c8befe31f9ddcf29d02bb0a5084a (qt/qtwebengine/6.2.0)

      Description

      QWebEngineScript::setSourceUrl doesn't load scripts from qrc, i.e it can't be used such way:

      QWebEngineScript script;
      script.setSourceUrl(QUrl("qrc:///qtwebchannel/qwebchannel.js"));
      ...
      

      or in QML:

      let script = WebEngine.script()
      script.sourceUrl = "qrc:///qtwebchannel/qwebchannel.js"
      ...
      

      There is wrong code introduced by https://codereview.qt-project.org/c/qt/qtwebengine/+/311566

      void QWebEngineScript::setSourceUrl(const QUrl &url)
      {
          if (url == sourceUrl())
              return;
      
          d->setSourceUrl(url);
      
          QFile file;
          if (url.scheme().compare(QLatin1String("qrc"), Qt::CaseInsensitive) == 0) {
              if (url.authority().isEmpty()) // <---- wrong?
                  file.setFileName(QLatin1Char(':') + url.path()); 
              return;  //  <---- wrong!                        
          } else {
              file.setFileName(url.toLocalFile());
          }
      
          if (!file.open(QIODevice::ReadOnly)) {
              qWarning() << "Can't open user script " << url;
              return;
          }
      
          QString source = QString::fromUtf8(file.readAll());
          setSourceCode(source);
      }
      

        Attachments

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

          Activity

            People

            Assignee:
            allan.jensen Allan Sandfeld Jensen
            Reporter:
            studiosus Vladimir Belyavsky
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There is 1 open Gerrit change