How to write a webkit application where a javascript will invoke a QT C++ methods?
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
Code:
#include <QWebElement>
#include <QCoreApplication>
#include <QWebElement>
Q_OBJECT
public slots:
void doSomethingWithWebElement(const QWebElement&);
};
/* ... */
int main(int argc, char *argv[])
{
MyObject myObject;
myWebPage.mainFrame()->addToJavaScriptWindowObject("myObject", &myObject);
return a.exec();
}
Here is the pro file.
Code:
#-------------------------------------------------
#
# 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.
Code:
<html>
<head>
<script>
function runExample() {
myObject.doSomethingWithWebElement(document.getElementById("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.
Re: How to write a webkit application where a javascript will invoke a QT C++ methods
Quote:
Originally Posted by
joshis1
/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
I would assume that this has no consequence due to QT += webkit impying gui, but just in case.
Quote:
Originally Posted by
joshis1
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
_
Re: How to write a webkit application where a javascript will invoke a QT C++ methods
You appear to be building using Qt5. The error is repaired with:
Code:
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
Quote:
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.