QWebFrame :: JavaScript vs. script -tag in HTML

I want to develop an application that uses QtWebKit and JQuery.

What I need to know is, is there a difference between reading JQuery from a file and evaluating it in JavaScript, or nesting it in a script tag on a page rendered in a widget?

EDITOR: I think I figured it out at least partially. Evaluate JavaScript will obviously work reliably; but if i do

baseurl = QUrl.fromLocalFile(
  QDir.current().absoluteFilePath("doesntexist.html"));
view.setHtml(
  u"""
    <html>
      <head>
        <script type="text/javascript" 
          src="jquery-1.4.2.js">
        </script>
      </head>
      <body></body>
    </html>""", baseurl);

      

The file is never read from disk (checked with inotify). it also affects baseurl initialization either with

QUrl("file:/")
QUrl(".");
QUrl();

      

or

QUrl("file://")

      

And I also tried changing the script src parameter to absolute paths on the hard drive and to a relative path with and without "./" in front.

How do I do this correctly (other than Qt's resource system) to make the script tag work with local js files? Is this just poorly documented or am I missing something?

+2


a source to share


1 answer


You can use Qt Resource System and change your html to something like this

<script type="text/javascript" 
      src=":/jquery-1.4.2.js">

      

and don't forget the macro call Q_INIT_RESOURCE in the main



or using the JavaScript evaluation method

connect(view, SIGNAL(loadFinished(bool)), this, SLOT(loadJQuery()));

...

void MainWindow::loadJQuery()
{
  QFile file("jquery-1.4.2.js");
  file.open(QFile::ReadOnly);
  view->page()->mainFrame()->evaluateJavaScript(file.readAll());
}

      

I think using resource systems is better.

0


a source







All Articles