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?

6
  • Here's a whole bunch of Qt Script examples: doc.qt.nokia.com/4.7/examples-script.html. Commented Nov 24, 2010 at 8:20
  • This is ongoing work tracked by this bug. If you want to try this experimental feature, checkout the answer in this question or this maillist thread. Commented Nov 24, 2010 at 8:25
  • but, is Qt Script able to handle html+css+javascript? I want a whole web page to be displayed with my customized javascript API Commented Nov 24, 2010 at 9: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. Commented Nov 24, 2010 at 9:08
  • 11
    @MSalters remind me never to buy a TomTom device. Commented Nov 27, 2010 at 21:55

2 Answers 2

6

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

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

Comments

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.

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.