开发者

QWebFrame::evaluateJavaScript vs. script-tag in HTML

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

What I need to know is, is there any difference between reading JQuery from a file and evaluateJavaScript it, or embedding it as a script tag within the "page" that is displayed within the widget?

EDIT: It seems that I have this figured out at least partially. evaluateJavaScript will apparently 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 even read from disk (checked with inotify). this also affects baseurl being initialized with either

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

or

QUrl("file://")

And I have also tried to change 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 it right (aside from the Qt Resource System) to get the script tag to work with 开发者_StackOverflowlocal js files? Is this just poorly documented, or am I missing something?


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

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

and don't forget the call macro Q_INIT_RESOURCE in main

or using the evaluateJavaScript

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 guess using the resource systems is better.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜