QDBusConnection 類

The QDBusConnection 類錶示到 D-Bus 總綫守護程序的連接。 更多...

頭: #include <QDBusConnection>
qmake: QT += dbus
Since: Qt 4.2

公共類型

enum BusType { SessionBus, SystemBus, ActivationBus }
flags ConnectionCapabilities
enum ConnectionCapability { UnixFileDescriptorPassing }
enum RegisterOption { ExportAdaptors, ExportScriptableSlots, ExportScriptableSignals, ExportScriptableProperties, ..., ExportChildObjects }
flags RegisterOptions
enum UnregisterMode { UnregisterNode, UnregisterTree }

公共函數

QDBusConnection (const QString & name )
QDBusConnection (const QDBusConnection & other )
QDBusConnection (QDBusConnection && other )
~QDBusConnection ()
QDBusPendingCall asyncCall (const QDBusMessage & message , int timeout = -1) const
QString baseService () const
QDBusMessage call (const QDBusMessage & message , QDBus::CallMode mode = QDBus::Block, int timeout = -1) const
bool callWithCallback (const QDBusMessage & message , QObject * receiver , const char * returnMethod , const char * errorMethod , int timeout = -1) const
bool connect (const QString & service , const QString & path , const QString & interface , const QString & name , QObject * receiver , const char * slot )
bool connect (const QString & service , const QString & path , const QString & interface , const QString & name , const QString & signature , QObject * receiver , const char * slot )
bool connect (const QString & service , const QString & path , const QString & interface , const QString & name , const QStringList & argumentMatch , const QString & signature , QObject * receiver , const char * slot )
QDBusConnection::ConnectionCapabilities connectionCapabilities () const
bool disconnect (const QString & service , const QString & path , const QString & interface , const QString & name , QObject * receiver , const char * slot )
bool disconnect (const QString & service , const QString & path , const QString & interface , const QString & name , const QString & signature , QObject * receiver , const char * slot )
bool disconnect (const QString & service , const QString & path , const QString & interface , const QString & name , const QStringList & argumentMatch , const QString & signature , QObject * receiver , const char * slot )
QDBusConnectionInterface * interface () const
bool isConnected () const
QDBusError lastError () const
QString name () const
QObject * objectRegisteredAt (const QString & path ) const
bool registerObject (const QString & path , QObject * object , QDBusConnection::RegisterOptions options = ExportAdaptors)
bool registerObject (const QString & path , const QString & interface , QObject * object , QDBusConnection::RegisterOptions options = ExportAdaptors)
bool registerService (const QString & serviceName )
bool send (const QDBusMessage & message ) const
void swap (QDBusConnection & other )
void unregisterObject (const QString & path , QDBusConnection::UnregisterMode mode = UnregisterNode)
bool unregisterService (const QString & serviceName )
QDBusConnection & operator= (QDBusConnection && other )
QDBusConnection & operator= (const QDBusConnection & other )

靜態公共成員

QDBusConnection connectToBus (QDBusConnection::BusType type , const QString & name )
QDBusConnection connectToBus (const QString & address , const QString & name )
QDBusConnection connectToPeer (const QString & address , const QString & name )
void disconnectFromBus (const QString & name )
void disconnectFromPeer (const QString & name )
QByteArray localMachineId ()
QDBusConnection sessionBus ()
const QMetaObject staticMetaObject
QDBusConnection systemBus ()

詳細描述

The QDBusConnection 類錶示到 D-Bus 總綫守護程序的連接。

此類是 D-Bus 會話的初始點。使用它,可以獲得對遠程對象、接口的訪問;將遠程信號連接到對象槽;注冊對象、等。

D-Bus 連接的創建是使用 connectToBus () 函數,打開到服務器守護程序的連接和做初始握手,關聯該連接與名稱。進一步,試圖使用相同的名稱進行連接,將返迴相同連接。

然後斷開連接使用 disconnectFromBus () 函數。

一旦斷開連接,調用 connectToBus () 不會重新建立連接,必須創建新的 QDBusConnection 實例。

為瞭方便,對於 2 種最常見連接類型, sessionBus () 和 systemBus () 函數分彆返迴到會話服務器守護程序和係統服務器守護程序的打開連接。這些連接在首次使用時打開,和被關閉當 QCoreApplication 析構函數運行。

D-Bus 還支持點對點連接,無需總綫服務器守護程序。使用此設施,2 應用程序可以相互交談和交換消息。這可以達成通過將地址傳遞給 connectToBus () 函數,其被打開通過另一 D-Bus 應用程序使用 QDBusServer .

成員類型文檔編製

enum QDBusConnection:: BusType

指定總綫連接的類型。有效總綫類型:

常量 描述
QDBusConnection::SessionBus 0 會話總綫,關聯在運行的桌麵會話
QDBusConnection::SystemBus 1 係統總綫,用於與係統範圍進程通信
QDBusConnection::ActivationBus 2 激活總綫,用於啓動服務的總綫彆名

在會話總綫,可以找到由同一用戶共享的相同桌麵會話 (因此得名) 的其它應用程序。不管怎樣,在係統總綫,通常可以找到整個係統的共享進程。

enum QDBusConnection:: ConnectionCapability
flags QDBusConnection:: ConnectionCapabilities

此枚舉描述用於 D-Bus 連接的可用能力。

常量 描述
QDBusConnection::UnixFileDescriptorPassing 0x0001 使將 Unix 文件描述符能夠傳遞給其它進程 (見 QDBusUnixFileDescriptor )

該枚舉在 Qt 4.8 引入或被修改。

ConnectionCapabilities 類型是 typedef 對於 QFlags <ConnectionCapability>。它存儲 ConnectionCapability 值的 OR 組閤。

另請參閱 connectionCapabilities ().

enum QDBusConnection:: RegisterOption
flags QDBusConnection:: RegisterOptions

采用連接指定注冊對象的選項。可能的值:

常量 描述
QDBusConnection::ExportAdaptors 0x01 導齣在此對象中找到的適配器內容
QDBusConnection::ExportScriptableSlots 0x10 導齣此對象的可編寫腳本槽
QDBusConnection::ExportScriptableSignals 0x20 導齣此對象的可編寫腳本信號
QDBusConnection::ExportScriptableProperties 0x40 export this object's scriptable properties
QDBusConnection::ExportScriptableInvokables 0x80 export this object's scriptable invokables
QDBusConnection::ExportScriptableContents 0xf0 shorthand form for ExportScriptableSlots | ExportScriptableSignals | ExportScriptableProperties
QDBusConnection::ExportNonScriptableSlots 0x100 export this object's non-scriptable slots
QDBusConnection::ExportNonScriptableSignals 0x200 export this object's non-scriptable signals
QDBusConnection::ExportNonScriptableProperties 0x400 export this object's non-scriptable properties
QDBusConnection::ExportNonScriptableInvokables 0x800 export this object's non-scriptable invokables
QDBusConnection::ExportNonScriptableContents 0xf00 shorthand form for ExportNonScriptableSlots | ExportNonScriptableSignals | ExportNonScriptableProperties
QDBusConnection::ExportAllSlots ExportScriptableSlots|ExportNonScriptableSlots export all of this object's slots
QDBusConnection::ExportAllSignals ExportScriptableSignals|ExportNonScriptableSignals 導齣此對象的所有信號
QDBusConnection::ExportAllProperties ExportScriptableProperties|ExportNonScriptableProperties export all of this object's properties
QDBusConnection::ExportAllInvokables ExportScriptableInvokables|ExportNonScriptableInvokables export all of this object's invokables
QDBusConnection::ExportAllContents ExportScriptableContents|ExportNonScriptableContents export all of this object's contents
QDBusConnection::ExportChildObjects 0x1000 導齣此對象的子級對象

RegisterOptions 類型是 typedef 對於 QFlags <RegisterOption>。它存儲 RegisterOption 值的 OR 組閤。

另請參閱 registerObject (), QDBusAbstractAdaptor ,和 使用適配器 .

enum QDBusConnection:: UnregisterMode

取消對象路徑注冊的方式:

常量 描述
QDBusConnection::UnregisterNode 0 僅取消此節點的注冊:不取消子級對象的注冊
QDBusConnection::UnregisterTree 1 取消注冊此節點及其所有子樹

注意,不管怎樣,若此對象被注冊采用 ExportChildObjects 選項,UnregisterNode 也會取消子級對象的注冊。

成員函數文檔編製

QDBusConnection:: QDBusConnection (const QString & name )

創建 QDBusConnection object attached to the connection with name name .

這不會打開連接。必須調用 connectToBus () 以打開它。

QDBusConnection:: QDBusConnection (const QDBusConnection & other )

創建副本為 other 連接。

QDBusConnection:: QDBusConnection ( QDBusConnection && other )

移動拷貝構造函數。

QDBusConnection:: ~QDBusConnection ()

處置此對象。這不會關閉連接:必須調用 disconnectFromBus () 以做到這點。

QDBusPendingCall QDBusConnection:: asyncCall (const QDBusMessage & message , int timeout = -1) const

發送 message over this connection and returns immediately. This function is suitable for method calls only. It returns an object of type QDBusPendingCall which can be used to track the status of the reply.

If no reply is received within timeout milliseconds, an automatic error will be delivered indicating the expiration of the call. The default timeout is -1, which will be replaced with an implementation-defined value that is suitable for inter-process communications (generally, 25 seconds). This timeout is also the upper limit for waiting in QDBusPendingCall::waitForFinished().

QDBusInterface::asyncCall () 函數瞭解放置調用的更友好方式。

該函數在 Qt 4.5 引入。

QString QDBusConnection:: baseService () const

Returns the unique connection name for this connection, if this QDBusConnection object is connected, or an empty QString 否則。

A Unique Connection Name is a string in the form ":x.xxx" (where x are decimal digits) that is assigned by the D-Bus server daemon upon connection. It uniquely identifies this client in the bus.

此函數返迴空 QString for peer-to-peer connections.

QDBusMessage QDBusConnection:: call (const QDBusMessage & message , QDBus::CallMode mode = QDBus::Block, int timeout = -1) const

發送 message over this connection and blocks, waiting for a reply, for at most timeout milliseconds. This function is suitable for method calls only. It returns the reply message as its return value, which will be either of type QDBusMessage::ReplyMessage or QDBusMessage::ErrorMessage .

If no reply is received within timeout milliseconds, an automatic error will be delivered indicating the expiration of the call. The default timeout is -1, which will be replaced with an implementation-defined value that is suitable for inter-process communications (generally, 25 seconds).

QDBusInterface::call () 函數瞭解放置調用的更友好方式。

警告: mode is QDBus::BlockWithGui , this function will reenter the Qt event loop in order to wait for the reply. During the wait, it may deliver signals and other method calls to your application. Therefore, it must be prepared to handle a reentrancy whenever a call is placed with call().

bool QDBusConnection:: callWithCallback (const QDBusMessage & message , QObject * receiver , const char * returnMethod , const char * errorMethod , int timeout = -1) const

發送 message over this connection and returns immediately. When the reply is received, the method returnMethod is called in the receiver object. If an error occurs, the method errorMethod will be called instead.

If no reply is received within timeout milliseconds, an automatic error will be delivered indicating the expiration of the call. The default timeout is -1, which will be replaced with an implementation-defined value that is suitable for inter-process communications (generally, 25 seconds).

This function is suitable for method calls only. It is guaranteed that the slot will be called exactly once with the reply, as long as the parameter types match and no error occurs.

返迴 true if the message was sent, or false if the message could not be sent.

bool QDBusConnection:: connect (const QString & service , const QString & path , const QString & interface , const QString & name , QObject * receiver , const char * slot )

連接信號指定通過 service , path , interface and name 參數用於槽 slot 在對象 receiver 。自變量 service and path 可以為空,錶示連接到任何信號為 ( interface , name ) 對,從任何遠程應用程序。

返迴 true 若連接成功。

警告: The signal will only be delivered to the slot if the parameters match. This verification can be done only when the signal is received, not at connection time.

bool QDBusConnection:: connect (const QString & service , const QString & path , const QString & interface , const QString & name , const QString & signature , QObject * receiver , const char * slot )

這是重載函數。

將信號連接到槽 slot 在對象 receiver . Unlike the previous connect () overload, this function allows one to specify the parameter signature to be connected using the signature variable. The function will then verify that this signature can be delivered to the slot specified by slot and return false otherwise.

返迴 true 若連接成功。

注意: This function verifies that the signal signature matches the slot's parameters, but it does not verify that the actual signal exists with the given signature in the remote service.

bool QDBusConnection:: connect (const QString & service , const QString & path , const QString & interface , const QString & name , const QStringList & argumentMatch , const QString & signature , QObject * receiver , const char * slot )

這是重載函數。

將信號連接到槽 slot 在對象 receiver . Unlike the previous connect () overload, this function allows one to specify the parameter signature to be connected using the signature variable. The function will then verify that this signature can be delivered to the slot specified by slot and return false otherwise.

The argumentMatch parameter lists the string parameters to be matched, in sequential order. Note that, to match an empty string, you need to pass a QString that is empty but not null (i.e., QString ("")). A null QString skips matching at that position.

返迴 true 若連接成功。

注意: This function verifies that the signal signature matches the slot's parameters, but it does not verify that the actual signal exists with the given signature in the remote service.

該函數在 Qt 4.6 引入。

[static] QDBusConnection QDBusConnection:: connectToBus ( QDBusConnection::BusType type , const QString & name )

打開連接為類型 type 到某一已知總綫並關聯它采用連接名稱 name 。返迴 QDBusConnection 對象關聯該連接。

[static] QDBusConnection QDBusConnection:: connectToBus (const QString & address , const QString & name )

打開到私有總綫的連接按地址 address 並關聯它采用連接名稱 name 。返迴 QDBusConnection 對象關聯該連接。

[static] QDBusConnection QDBusConnection:: connectToPeer (const QString & address , const QString & name )

Opens a peer-to-peer connection on address address 並關聯它采用連接名稱 name 。返迴 QDBusConnection 對象關聯該連接。

該函數在 Qt 4.8 引入。

QDBusConnection::ConnectionCapabilities QDBusConnection:: connectionCapabilities () const

Returns the capabilities of this connection as negotiated with the bus server or peer. If this QDBusConnection is not connected, this function returns no capabilities.

該函數在 Qt 4.8 引入。

bool QDBusConnection:: disconnect (const QString & service , const QString & path , const QString & interface , const QString & name , QObject * receiver , const char * slot )

Disconnects the signal specified by the service , path , interface and name parameters from the slot slot 在對象 receiver . The arguments must be the same as passed to the connect () 函數。

返迴 true 若斷開連接成功。

bool QDBusConnection:: disconnect (const QString & service , const QString & path , const QString & interface , const QString & name , const QString & signature , QObject * receiver , const char * slot )

這是重載函數。

Disconnects the signal specified by the service , path , interface , name ,和 signature parameters from the slot slot 在對象 receiver . The arguments must be the same as passed to the connect () 函數。

返迴 true 若斷開連接成功。

bool QDBusConnection:: disconnect (const QString & service , const QString & path , const QString & interface , const QString & name , const QStringList & argumentMatch , const QString & signature , QObject * receiver , const char * slot )

這是重載函數。

Disconnects the signal specified by the service , path , interface , name , argumentMatch ,和 signature parameters from the slot slot 在對象 receiver . The arguments must be the same as passed to the connect () 函數。

返迴 true 若斷開連接成功。

該函數在 Qt 4.6 引入。

[static] void QDBusConnection:: disconnectFromBus (const QString & name )

關閉總綫連接對於名稱 name .

Note that if there are still QDBusConnection objects associated with the same connection, the connection will not be closed until all references are dropped. However, no further references can be created using the QDBusConnection 構造函數。

[static] void QDBusConnection:: disconnectFromPeer (const QString & name )

關閉對等連接對於名稱 name .

Note that if there are still QDBusConnection objects associated with the same connection, the connection will not be closed until all references are dropped. However, no further references can be created using the QDBusConnection 構造函數。

該函數在 Qt 4.8 引入。

QDBusConnectionInterface *QDBusConnection:: interface () const

返迴 QDBusConnectionInterface object that represents the D-Bus server interface on this connection.

bool QDBusConnection:: isConnected () const

返迴 true 若此 QDBusConnection 對象被連接。

QDBusError QDBusConnection:: lastError () const

返迴在此連接發生的最後錯誤。

This function is provided for low-level code. If you're using QDBusInterface::call (), error codes are reported by its return value.

另請參閱 QDBusInterface and QDBusMessage .

[static] QByteArray QDBusConnection:: localMachineId ()

Returns the local machine ID as known to the D-Bus system. Each node or host that runs D-Bus has a unique identifier that can be used to distinguish it from other hosts if they are sharing resources like the filesystem.

Note that the local machine ID is not guaranteed to be persistent across boots of the system, so this identifier should not be stored in persistent storage (like the filesystem). It is guaranteed to remain constant only during the lifetime of this boot session.

該函數在 Qt 4.8 引入。

QString QDBusConnection:: name () const

Returns the connection name for this connection, as given as the name parameter to connectToBus ().

The connection name can be used to uniquely identify actual underlying connections to buses. Copies made from a single connection will always implicitly share the underlying connection, and hence will have the same connection name.

Inversely, two connections having different connection names will always either be connected to different buses, or have a different unique name (as returned by baseService ()) on that bus.

該函數在 Qt 4.5 引入。

另請參閱 connectToBus () 和 disconnectFromBus ().

QObject *QDBusConnection:: objectRegisteredAt (const QString & path ) const

Return the object that was registered with the registerObject () at the object path given by path .

bool QDBusConnection:: registerObject (const QString & path , QObject * object , QDBusConnection::RegisterOptions options = ExportAdaptors)

注冊對象 object 在路徑 path 並返迴 true 若注冊成功。 options 參數指定多少對象 object 將透過 D-Bus 被暴露。

This function does not replace existing objects: if there is already an object registered at path path , this function will return false. Use unregisterObject () to unregister it first.

You cannot register an object as a child object of an object that was registered with QDBusConnection::ExportChildObjects .

bool QDBusConnection:: registerObject (const QString & path , const QString & interface , QObject * object , QDBusConnection::RegisterOptions options = ExportAdaptors)

這是重載函數。

注冊對象 object 在路徑 path 采用接口名稱 interface 並返迴 true 若注冊成功。 options 參數指定多少對象 object 將透過 D-Bus 被暴露。

This function does not replace existing objects: if there is already an object registered at path path , this function will return false. Use unregisterObject () to unregister it first.

You cannot register an object as a child object of an object that was registered with QDBusConnection::ExportChildObjects .

該函數在 Qt 5.5 引入。

bool QDBusConnection:: registerService (const QString & serviceName )

試圖注冊 serviceName 在 D-Bus 服務器並返迴 true 若注冊成功。注冊將失敗,若名稱已被另一應用程序注冊。

另請參閱 unregisterService () 和 QDBusConnectionInterface::registerService ().

bool QDBusConnection:: send (const QDBusMessage & message ) const

發送 message over this connection, without waiting for a reply. This is suitable for errors, signals, and return values as well as calls whose return values are not necessary.

返迴 true 若消息隊列成功,否則 false。

[static] QDBusConnection QDBusConnection:: sessionBus ()

返迴 QDBusConnection object opened with the session bus. The object reference returned by this function is valid until the application terminates, at which point the connection will be closed and the object deleted.

void QDBusConnection:: swap ( QDBusConnection & other )

交換此 QDBusConnection 實例與 other .

[static] QDBusConnection QDBusConnection:: systemBus ()

返迴 QDBusConnection object opened with the system bus. The object reference returned by this function is valid until the QCoreApplication 's destructor is run, when the connection will be closed and the object, deleted.

void QDBusConnection:: unregisterObject (const QString & path , QDBusConnection::UnregisterMode mode = UnregisterNode)

Unregisters an object that was registered with the registerObject () at the object path given by path and, if mode is QDBusConnection::UnregisterTree , all of its sub-objects too.

Note that you cannot unregister objects that were not registered with registerObject ().

bool QDBusConnection:: unregisterService (const QString & serviceName )

Unregisters the service serviceName that was previously registered with registerService () 並返迴 true 若成功。

另請參閱 registerService () 和 QDBusConnectionInterface::unregisterService ().

QDBusConnection &QDBusConnection:: operator= ( QDBusConnection && other )

移動賦值運算符。

QDBusConnection &QDBusConnection:: operator= (const QDBusConnection & other )

創建副本為連接 other 在此對象。注意,此對象引用的連接在拷貝之前,不會自發斷開連接。

另請參閱 disconnectFromBus ().