0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.