QWebChannel 类

把 QObject 暴露给远程 HTML 客户端。 更多...

头: #include <QWebChannel>
qmake: QT += webchannel
继承: QObject

特性

公共函数

QWebChannel (QObject * parent = nullptr)
virtual ~QWebChannel ()
QBindable<bool> bindableBlockUpdates ()
QBindable<int> bindablePropertyUpdateInterval ()
bool blockUpdates () const
void deregisterObject (QObject * object )
int propertyUpdateInterval () const
void registerObject (const QString & id , QObject * object )
void registerObjects (const QHash<QString, QObject *> & 对象 )
QHash<QString, QObject *> registeredObjects () const
void setBlockUpdates (bool block )
void setPropertyUpdateInterval (int ms )

公共槽

void connectTo (QWebChannelAbstractTransport * transport )
void disconnectFrom (QWebChannelAbstractTransport * transport )

信号

void blockUpdatesChanged (bool block )

详细描述

QWebChannel 填充 C++ 应用程序和 HTML/JavaScript 应用程序之间的间隙。通过发布 QObject 派生对象给 QWebChannel 并使用 qwebchannel.js on the HTML side, one can transparently access properties and public slots and methods of the QObject 。不需要手动传递消息和序列化数据,在 C++ 侧的特性更新和信号发出,会被自动传输到潜在的远程运行 HTML 客户端。在客户端侧,JavaScript 对象将被创建,为任何已发布 C++ QObject 。它镜像 C++ 对象的 API,因此,是直观可用的。

QWebChannel transparently supports QFuture . When a client calls a method that returns a QFuture , QWebChannel will send a response with the QFuture result only after the QFuture has finished.

Custom conversion of types to and from JSON is supported by defining converters with QMetaType::registerConverter () to and from QJsonValue . Note that custom converters from QJsonValue to a concrete type must fail if the QJsonValue does not match the expected format. Otherwise QWebChannel cannot fall back to its default conversion mechanisms. Custom converters are also available on the JavaScript side .

C++ QWebChannel API 使与任何 HTML 客户端对话成为可能,这些客户端可以运行在本地 (甚至远程机器上)。唯一的限制是 HTML 客户端要支持 JavaScript 特征,要求使用 qwebchannel.js 。因此,基本上可以与任何现代 HTML 浏览器或独立 JavaScript 运行时 (如 node.js) 交互。

还存在声明式 WebChannel API .

另请参阅 Qt WebChannel 独立范例 , JavaScript API ,和 QMetaType::registerConverter ().

特性文档编制

[bindable] blockUpdates : bool

注意: 此特性支持 QProperty 绑定。

当设为 true 时,更新被阻塞,且不会通知远程客户端有关特性的更改。

改变被记录并被发送给客户端,一旦更新再次变为不阻塞,通过把此特性设为 false。默认情况下,更新不被阻塞。

[bindable] propertyUpdateInterval : int

注意: 此特性支持 QProperty 绑定。

This property holds the property update interval.

This interval can be changed to a different interval in milliseconds by setting it to a positive value. Property updates are batched and sent out after the interval expires. If set to zero, the updates occurring within a single event loop run are batched and sent out on the next run. If negative, updates will be sent immediately. Default value is 50 milliseconds.

成员函数文档编制

[explicit] QWebChannel:: QWebChannel ( QObject * parent = nullptr)

构造 QWebChannel 对象采用给定 parent .

注意:QWebChannel 才完整可操作,一旦把它连接到 QWebChannelAbstractTransport 。HTML 客户端还需进行适当设置使用 qwebchannel.js .

[virtual noexcept] QWebChannel:: ~QWebChannel ()

销毁 QWebChannel .

[slot] void QWebChannel:: connectTo ( QWebChannelAbstractTransport * transport )

连接 QWebChannel 到给定 transport 对象。

然后,传输对象处理 C++ 应用程序和远程 HTML 客户端之间的通信。

另请参阅 QWebChannelAbstractTransport and QWebChannel::disconnectFrom ().

[invokable] void QWebChannel:: deregisterObject ( QObject * object )

撤销注册给定 object QWebChannel .

远程客户端会接收 destroyed 信号对于给定对象。

注意: 此函数可以被援引,通过元对象系统和从 QML。见 Q_INVOKABLE .

另请参阅 QWebChannel::registerObjects (), QWebChannel::registerObject (),和 QWebChannel::registeredObjects ().

[slot] void QWebChannel:: disconnectFrom ( QWebChannelAbstractTransport * transport )

断开连接 QWebChannel transport 对象。

另请参阅 QWebChannel::connectTo ().

[invokable] void QWebChannel:: registerObject (const QString & id , QObject * object )

把单个对象注册到 QWebChannel .

The properties, signals and public methods of the object are published to the remote clients. There, an object with the identifier id 然后被构造。

注意: A property that is BINDABLE but does not have a NOTIFY signal will have working property updates on the client side, but no mechanism to register a callback for the change notifications.

注意: 当前的限制是对象被注册,在任何客户端被初始化之前。

注意: 此函数可以被援引,通过元对象系统和从 QML。见 Q_INVOKABLE .

另请参阅 QWebChannel::registerObjects (), QWebChannel::deregisterObject (),和 QWebChannel::registeredObjects ().

void QWebChannel:: registerObjects (const QHash < QString , QObject *> & 对象 )

把对象组注册到 QWebChannel .

对象的特性、信号及公共可援引方法被发布到远程客户端。在那里,对象采用标识符用作为键在 对象 映射然后被构造。

注意: 当前的限制是对象被注册,在任何客户端被初始化之前。

另请参阅 QWebChannel::registerObject (), QWebChannel::deregisterObject (),和 QWebChannel::registeredObjects ().

QHash < QString , QObject *> QWebChannel:: registeredObjects () const

返回被发布到远程客户端的注册对象的映射。

另请参阅 QWebChannel::registerObjects (), QWebChannel::registerObject (),和 QWebChannel::deregisterObject ().