PDA

View Full Version : How to write a webkit application where a javascript will invoke a QT C++ methods?



joshis1
9th February 2014, 08:08
I am looking for a short example with a minute details covered.

I am writing a QT application - console one on the QT creator on FC19 machine. The aim here is to call a QT method from a javascript.

At the moment, I am following this link -

http://qt.developpez.com/doc/4.7/qtwebkit-bridge/

Iam facing two problems -

First compilation -


/home/joshis1/Demo_JS/main.cpp:2: error: QWebElement: No such file or directory


#include <QWebElement>

#include <QCoreApplication>
#include <QWebElement>

class MyObject : QObject {
Q_OBJECT

public slots:
void doSomethingWithWebElement(const QWebElement&);
};

/* ... */


int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

MyObject myObject;
myWebPage.mainFrame()->addToJavaScriptWindowObject("myObject", &myObject);


return a.exec();
}

Here is the pro file.


#-------------------------------------------------
#
# Project created by QtCreator 2014-02-08T17:31:01
#
#-------------------------------------------------

QT += core

QT -= gui

TARGET = Demo_JS
CONFIG += console
CONFIG -= app_bundle
QT += webkit

TEMPLATE = app


SOURCES += main.cpp


The next upcoming challenge I thinks is how to relate a java script present in any folder of my FC19 to invoke a QT function defined in the above program. How my javascript will know the path to pick the application. How it can be done? This is my java script wrapped in a html file.


<html>
<head>
<script>
function runExample() {
myObject.doSomethingWithWebElement(document.getEle mentById("someElement"));
}
</script>
</head>
<body onload="runExample()">
<span id="someElement">Text</span>
</body>
</html>

Please help me here with an elaborate explanation. I am literally frustrated with all my trials.

anda_skoa
9th February 2014, 13:43
/home/joshis1/Demo_JS/main.cpp:2: error: QWebElement: No such file or directory

Could a problem with the .pro file. Try removing the line


QT -= gui

I would assume that this has no consequence due to QT += webkit impying gui, but just in case.


The next upcoming challenge I thinks is how to relate a java script present in any folder of my FC19 to invoke a QT function defined in the above program. How my javascript will know the path to pick the application. How it can be done?

I don't quite understand the question. You program loads the HTML into its QWebPage instance, in your code snippet called "myWebPage".
Any JavaScript in that HTML file or loaded by it sees the environment you prepared, including your exported object.

Cheers
_

ChrisW67
9th February 2014, 22:59
You appear to be building using Qt5. The error is repaired with:


QT += webkit webkitwidgets

Webkit requires Gui.

Since you are using Qt 5, you should be reading the correct version of the docs:
http://qt-project.org/doc/qt-5/qtwebkit-bridge.html


The next upcoming challenge I thinks is how to relate a java script present in any folder of my FC19 to invoke a QT function defined in the above program. How my javascript will know the path to pick the application. How it can be done?
You can only use the QtWebkit bridge when the HTML/Javascript is loaded into a QWebFrame in a Qt application (i.e. using QtWebkit). The HTML loaded into an arbitrary browser is not magically going to launch a separate Qt program and connect itself somehow.