Results 1 to 3 of 3

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

  1. #1
    Join Date
    Feb 2014
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default 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
    Qt Code:
    1. #include <QWebElement>
    2.  
    3. #include <QCoreApplication>
    4. #include <QWebElement>
    5.  
    6. class MyObject : QObject {
    7. Q_OBJECT
    8.  
    9. public slots:
    10. void doSomethingWithWebElement(const QWebElement&);
    11. };
    12.  
    13. /* ... */
    14.  
    15.  
    16. int main(int argc, char *argv[])
    17. {
    18. QCoreApplication a(argc, argv);
    19.  
    20. MyObject myObject;
    21. myWebPage.mainFrame()->addToJavaScriptWindowObject("myObject", &myObject);
    22.  
    23.  
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 
    Here is the pro file.
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2014-02-08T17:31:01
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core
    8.  
    9. QT -= gui
    10.  
    11. TARGET = Demo_JS
    12. CONFIG += console
    13. CONFIG -= app_bundle
    14. QT += webkit
    15.  
    16. TEMPLATE = app
    17.  
    18.  
    19. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt Code:
    1. <html>
    2. <head>
    3. <script>
    4. function runExample() {
    5. myObject.doSomethingWithWebElement(document.getElementById("someElement"));
    6. }
    7. </script>
    8. </head>
    9. <body onload="runExample()">
    10. <span id="someElement">Text</span>
    11. </body>
    12. </html>
    To copy to clipboard, switch view to plain text mode 
    Please help me here with an elaborate explanation. I am literally frustrated with all my trials.
    Last edited by anda_skoa; 9th February 2014 at 13:36. Reason: added code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to write a webkit application where a javascript will invoke a QT C++ methods

    Quote Originally Posted by joshis1 View Post
    /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 Code:
    1. QT -= gui
    To copy to clipboard, switch view to plain text mode 
    I would assume that this has no consequence due to QT += webkit impying gui, but just in case.

    Quote Originally Posted by joshis1 View Post
    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
    _

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,594
    Thanks
    13
    Thanked 1,588 Times in 1,517 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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:
    Qt Code:
    1. QT += webkit webkitwidgets
    To copy to clipboard, switch view to plain text mode 
    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.

Similar Threads

  1. ActiveX-javascript invoke activex interface exception
    By zhy282289 in forum Qt Programming
    Replies: 0
    Last Post: 29th October 2012, 02:58
  2. Webkit open new javascript Page
    By ShapeShiftme in forum Qt Programming
    Replies: 3
    Last Post: 25th October 2012, 08:46
  3. Webkit and Javascript Debugger
    By ideru in forum Installation and Deployment
    Replies: 1
    Last Post: 7th October 2011, 04:14
  4. Replies: 10
    Last Post: 10th February 2011, 19:45
  5. WebKit - accessing javascript results + googleMap
    By giusepped in forum Qt Programming
    Replies: 10
    Last Post: 30th May 2009, 00:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.