문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top