QT and QwebKit can I load a javascript file as a resource file and use it?

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

+3
source share
1 answer

Yes, you can. I do this in my application. For instance:

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

Where readFile is a function that reads the contents of a file into a string. For instance:

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

Since the file name begins with:, it is read from the resource file. The resource file should contain /scripts/foo.js, obviously.

+4
source

Source: https://habr.com/ru/post/1748838/


All Articles