高水平,可以選擇使用
QObject
類型直接作為 Source (源);或者可以定義期望 API 在
.rep
模闆為用於
repc
編譯器。
若已經擁有完全定義 QObject ,可以把它用作源通過將其傳遞給 QRemoteObjectHostBase::enableRemoting ()。這樣,其它進程 (或設備) 則可以創建 動態復本 對於要交互對象。
更多信息,見 遠程對象交互 ).
讓 RepC (復本編譯器) 為工程生成 Source (源) 頭文件,使用 REPC_SOURCE 變量,為實現要求 API 有提供 3 種不同選項。
假設類名為 Foo,那麼擁有下列選項:
更多細節關於如何創建
.rep
文件,見
*.REP 文件格式
.
在頭中定義的 <Type>SimpleSource 類,有為各特性提供基本 getter/setter 方法,並在頭中實現瞭正確特性類型的數據成員。這裏的 <Type> 錶示類名來自
.rep
文件,因此,若類是 MyType 類型在
.rep
文件,那麼,生成的頭文件將擁有 MyTypeSimpleSource 類聲明。這是使用 API 的快速入門方式。要使用此類,需要編寫繼承自它的類,並實現在生成的頭文件中被標記為純虛擬的任何槽定義。還需要嚮類添加必要邏輯,以管理暴露特性和發射信號。
若需要隱藏實現細節,使用在頭文件中聲明的第 2 <Type>Source 類。此類定義不提供數據成員,並將 getter/setter 函數製作成純虛擬。雖然可能需要編寫更多代碼,但是在實現中使用此類可賦予更多靈活性。
The third class generated in the header is <Type>SourceAPI. 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 required API as the source. Use this class to hide or convert properties or signal/slot parameters. If your implementation does not provide the correct API, there will be compile-time warnings.
注意:
Replicas and sources both derive from
QObject
; but their
QObject
API 是
never
exposed. For instance, while a replica has a
destroyed
信號,源的
destroyed
signal is not propagated. The source and each of its replica are unique QObjects with their own connections and lifetimes. Instead, the API that is exposed is defined by the
.rep
template used by repc. In the case of raw QObjects, all API elements are defined in the inheritance chain from a specific ancestor. The
QObject
's parent is always used, unless you define
Q_CLASSINFO("RemoteObject Type")
in an ancestor. If you use
Q_CLASSINFO("RemoteObject Type")
, that class's API is the lowest level of API used.
由於主機 Node (節點) 可以共享多個 Source (源),所以每個源都要求名稱。所有 RepC (復本編譯器) 生成頭,都包括節點確定類名的方式:
Q_CLASSINFO
用於 Replica (復本)、SimpleSource (簡單源)、或 Source (源) 類型;或靜態
name()
函數用於 SourceAPI 類型。若傳遞自己的
QObject
類型到
QRemoteObjectHostBase::enableRemoting
(),使用下列邏輯確定名稱: