Qt Remote Objects 源

源对象

远程对象源是 QObject 负责暴露 API 的实现。

At a high level, you have a choice of using a QObject type directly as a source or defining the desired API in a .rep template for use with the repc 编译器。

若已有完全定义的 QObject , it can become a Source simply by passing it to QRemoteObjectHostBase::enableRemoting (). This lets other processes/devices create a 复本 of the object to interact with (see 远程对象交互 ). You can then instantiate QRemoteObjectDynamicReplica s of your object.

Letting repc generate a header file for your project (using the REPC_SOURCE variable) provides three options for implementing the desired API. If your class name was Foo, the options would be the following (see *.REP 文件格式 for help in creating a rep file)

There is a <Type>SimpleSource class defined in the header, which provides basic getter/setter methods for each property and implements data members of the correct property type in the header. Here "<Type>" represents the class name from the .rep file, so if your class is of type "MyType" in the .rep file, there will be a MyTypeSimpleSource class declared in the generated header file. This is a fast way to get started using the API. To use this class, you need to inherit from this class and implement any defined slots (which are pure virtual in the generated header file). Whatever logic is needed to manage the exposed properties and emit signals would be added to the inheriting class as well.

If you need to hide the implementation details, you can use the <Type>Source class instead, which is the 2nd class declared in the header file. This class definition does not provide data members, and makes the getter/setter functions pure virtual as well. You have more flexibility in how you implement the class, although you need to write more code.

Finally, there is the <Type>SourceAPI class generated in the header. This is a templated class, for use specifically by the templated version of QRemoteObjectHostBase::enableRemoting (), which allows you to use any QObject that supports the desired API as the source. You will get compile-time warnings if the class does not provide the correct API, and using this class allows you to hide or convert properties or signal/slot parameters.

注意: The QObject API 是 never exposed. For instance, while a 复本 will have a destroyed signal, the destroyed signal of the source is not propagated. The and each 复本 are unique QObjects with their own connections. The API that is exposed is defined by the .rep template used by repc , or in the case of raw QObjects, all API elements defined in the inheritance chain from a specific ancestor. Unless you define Q_CLASSINFO ("RemoteObject Type") in an ancestor, the QObject 's parent is used. If Q_CLASSINFO ("RemoteObject Type") is used, that class's API is the lowest level of API used.

标识源

Since more than one can be shared by a host node, each requires a name. All repc generated headers include a way for the node to determine the class name ( Q_CLASSINFO for replica/simplesource/source types, or a static name() function for the SourceAPI type). If you pass your own QObject type to QRemoteObjectHostBase::enableRemoting (), the name will be determined using the following logic: