Reputation: 4208
I am trying to call a Javascript function in my HTML file from QT using the addToJavaScriptWindowObject
mwthod. I followed this post on StackOverflow and did exactly as the accepted answer.
My code is as follows
main.cpp
#include <QApplication>
#include <QDebug>
#include <QtWebKitWidgets/QWebFrame>
#include <QtWebKitWidgets/QWebPage>
#include <QtWebKitWidgets/QWebView>
class MyJavaScriptOperations : public QObject {
Q_OBJECT
public:
Q_INVOKABLE void sumOfNumbers(int a, int b) {
qDebug() << a + b;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWebView *view = new QWebView();
view->resize(400, 500);
view->page()->mainFrame()->addToJavaScriptWindowObject("myoperations", new MyJavaScriptOperations);
view->load(QUrl("file://C:/programs/index.html"));
view->show();
return a.exec();
}
#include "main.moc"
.pro file
QT += core
QT += gui
QT += webkit
QT += webkit webkitwidgets
TARGET = QtJsonPostExample
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
OTHER_FILES += \
../../../../programs/index.html
index.html
<html>
<body>
<script type="text/javascript">
myoperations.sumOfNumbers(12, 23);
</script>
</body>
When I run the above QT project in Qt creator, it compiles correctly and even runs. I see the new window open up but the output , in this case the sum of 12+23= 35 isn't displayed in the Application Output
window. Why is that so?
Why am I not able to view the output even though the program is running fine?
Upvotes: 0
Views: 747
Reputation: 441
It sounds like you forgot to enable debug output. Are you sure you built in debug mode? Try adding a qDebug() << "Test"; to your main function to check.
Upvotes: 1