2

I am new to Qt and what I am trying to do is:

  1. Create a Linux app using the Qt framework.
  2. This app displays some web pages from the Internet.
  3. I want to extend the JavaScript API to access some device and device based data, which means some devices can be controlled using JavaScript in Webkit.

But how do I add some customized functions/classes to Webkit in Qt?

user unknown
  • 35,537
  • 11
  • 75
  • 121
Mickey Shine
  • 12,187
  • 25
  • 96
  • 148
  • Here's a whole bunch of Qt Script examples: http://doc.qt.nokia.com/4.7/examples-script.html. – Daniel Lidström Nov 24 '10 at 08:20
  • but, is Qt Script able to handle html+css+javascript? I want a whole web page to be displayed with my customized javascript API – Mickey Shine Nov 24 '10 at 09:06
  • a number of your past questions suggest that you are working on devices, web-based updates and maps. This is an area in which my company (TomTom) owns multiple patents. After selecting a solution, you might want to check with an IP attorney whether such a solution would be infringing. – MSalters Nov 24 '10 at 09:08
  • 11
    @MSalters remind me never to buy a TomTom device. –  Nov 27 '10 at 21:55
  • @Will: You should probably stick to paper maps, as that's the only map technology that's out of patents. Our competitors have their own patents. But I'm under no legal obligation to point out that Garmin, Nokia, Google etc are in the map business. Again, don't take my word here, and check with a properly licensed IP attorney. – MSalters Nov 29 '10 at 15:38
  • This is ongoing work tracked by [this bug](https://bugs.webkit.org/show_bug.cgi?id=31863). If you want to try this experimental feature, checkout the answer in [this question](http://stackoverflow.com/questions/943554/how-to-get-javascript-in-a-qwebview-to-create-new-instances-of-c-based-classes) or this [maillist thread](http://old.nabble.com/Adding-Javascript-object-to-Webkit-td16471407.html). – J-16 SDiZ Nov 24 '10 at 08:25

2 Answers2

6

I've done a QWebKit project in which I stablished a bridge between Javascript and my C++ code.

To achieve this I used the method:

this->page()->mainFrame()->addToJavaScriptWindowObject( "god", this );

This allows you to execute methods of the object you pass to addToJavaScriptWindowObject as second parameter from Javascript, using the object specified as first parameter.

Here an example:

class Browser : public QWebView
{
     Q_OBJECT
     public:
         Browser( QWidget* parent=0 )
         {
            connect( this->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),    this,   SLOT(onJavaScriptWindowObjectCleared()) );
         }

     public slots:
         void onJavaScriptWindowObjectCleared()
         {
          //   QString script = "console.log('init!');";
          //   this->page()->mainFrame()->evaluateJavaScript( script );
         }

         void onChange()
         {
             qDebug() << "Browser::onChange()";
         }
 }

Then, from Javascript I can do:

$('input:text').keydown( god.onChange );

So every time I press a key in an input box, god.onChange() is executed which executes Browser::onChange() slot.

This way you avoid extending the JS api.

Pherrymason
  • 7,835
  • 8
  • 39
  • 57
6

Fortunately, there exists some documentation on this, finally: http://doc.qt.io/qt-4.8/qtwebkit-bridge.html

Ringding
  • 2,856
  • 17
  • 10