Qt WebChannel JavaScript API.javascript调用有返回值的C++函数官方教程

Q t WebChannel JavaScript API

Setup

To communicate with a QWebChannel or WebChannel, any HTML client must use and setup the JavaScript API provided byqwebchannel.js. For HTML clients run inside Qt WebKit, you can load the file viaqrc:///qtwebchannel/qwebchannel.js. For external clients you will need to copy the file to your webserver. Then instantiate aQWebChannel object and pass it a transport object and a callback function, which will be invoked once the initialization of the channel finished and published objects become available.

The transport object implements a minimal message passing interface. It should be an object with asend() function, which takes a stringified JSON message and transmits it to the server-sideQWebChannelAbstractTransport object. Furthermore, itsonmessage property should be called when a message from the server was received. This interface is implemented internally by the Qt WebKit navigator.qtWebChannelTransport object. Alternatively, you can also use a WebSocket, which also implements this interface.

Note that the JavaScript QWebChannel object should be constructed once the transport object is fully operational. In case of a WebSocket, that means you should create the QWebChannel in the socket'sonopen handler. Take a look at theQt WebChannel Standalone Example to see how this is done.

Interacting with QObjects

Once the callback passed to the QWebChannel object is invoked, the channel has finished initialization and all published objects are accessible to the HTML client via thechannel.objects property. Thus, assuming an object was published with the identifier "foo", then we can interact with it as shown in the example below. Note that all communication between the HTML client and the QML/C++ server is asynchronous. Properties are cached on the HTML side. Furthermore keep in mind that only QML/C++ data types which can be converted to JSON will be (de-)serialized properly and thus accessible to HTML clients.

 new QWebChannel(yourTransport, function(channel) {

      // Connect to a signal:
      channel.objects.foo.mySignal.connect(function() {
          // This callback will be invoked whenever the signal is emitted on the C++/QML side.
          console.log(arguments);
      });

      // To make the object known globally, assign it to the window object, i.e.:
      window.foo = channel.objects.foo;

      // Invoke a method:
      foo.myMethod(arg1, arg2, function(returnValue) {
          // This callback will be invoked when myMethod has a return value. Keep in mind that
          // the communication is asynchronous, hence the need for this callback.
          console.log(returnValue);
      });

      // Read a property value, which is cached on the client side:
      console.log(foo.myProperty);

      // Writing a property will instantly update the client side cache.
      // The remote end will be notified about the change asynchronously
      foo.myProperty = "Hello World!";

      // To get notified about remote property changes,
      // simply connect to the corresponding notify signal:
      foo.onMyPropertyChanged.connect(function(newValue) {
          console.log(newValue);
      });

      // One can also access enums that are marked with Q_ENUM:
      console.log(foo.MyEnum.MyEnumerator);
  });



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值