views:

109

answers:

1

Hello all i wander if i can load javascript as resource file to use in QwebKit well its dont have to be resource file , i just looking for a method to emmbed js files in to my application

+1  A: 

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.

Dan