2

I wonder if I can load Javascript as a resource file to use in QwebKit? Well, it doesn't have to be resource file, I am just looking for a method to embed JS files into my application.

JTFouquier
  • 389
  • 1
  • 4
  • 17
user63898
  • 29,839
  • 85
  • 272
  • 514

1 Answers1

4

Yes, you can. I do this in my app. For example:

QWebFrame* frame = ui->webView->page()->mainFrame();
frame->evaluateJavaScript(readFile(":/scripts/foo.js"));

Where readFile is a function which reads the contents of a file to a string. For example:

QString readFile (const QString& filename)
{
    QFile file(filename);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QTextStream stream(&file);
        return stream.readAll();
    }
    return "";
}

Since the filename starts with :, it is read from the resource file. The resource file must have /scripts/foo.js defined, obviously.