2012-06-25 56 views
3

這是我第一次使用DBus,所以我不完全確定我是否正在以正確的方式進行操作。我試圖連接Ubuntu One DBus服務並獲取我的應用程序的登錄憑據,但是我連接到DBus返回信號的插槽詳細信息here似乎從未觸發,儘管連接期間返回了正面結果。此Qt DBus信號連接代碼是否正確?

在我開始尋找與這個特定服務有關的細節錯誤之前,有人能告訴我這個代碼是否能夠在第一時間工作,或者如果我在這裏做錯了什麼?

int main() 
{ 
    UbuntuOneDBus *u1Dbus = new UbuntuOneDBus; 
    u1Dbus->init(); 
} 

class UbuntuOneDBus : public QObject 
{ 
    Q_OBJECT 

    QString busName; 
    QString path; 
    QString interface; 
    QString method; 
    QString signature; 

    void connectReturnSignals(); 

private slots: 
    void credentialsFound(); 
    void credentialsNotFound(); 
    void credentialsError(); 

public: 
    UbuntuOneDBus(); 

    void init(); 
}; 

UbuntuOneDBus::UbuntuOneDBus() 
{ 
    busName = "com.ubuntuone.Credentials"; 
    path = "/credentials"; 
    interface = "com.ubuntuone.CredentialsManagement"; 
    method = "register"; 
    signature = "a{ss}"; 

    connectReturnSignals(); 
} 

void UbuntuOneDBus::init() 
{ 
    QDBusMessage message = QDBusMessage::createMethodCall(busName, path, interface, method); 
    QDBusConnection::sessionBus().send(message); 
} 

void UbuntuOneDBus::connectReturnSignals() 
{ 
    QDBusConnection::sessionBus().connect(busName, path, interface, "CredentialsFound", this, SLOT(credentialsFound())); 
    QDBusConnection::sessionBus().connect(busName, path, interface, "CredentialsNotFound", this, SLOT(credentialsNotFound())); 
    QDBusConnection::sessionBus().connect(busName, path, interface, "CredentialsError", this, SLOT(credentialsError())); 
} 

void UbuntuOneDBus::credentialsFound() 
{ 
    qDebug() << "Credentials found"; 
} 

void UbuntuOneDBus::credentialsNotFound() 
{ 
    std::cout << "Credentials not found" << std::endl; 
} 

void UbuntuOneDBus::credentialsError() 
{ 
    std::cout << "Credentials error" << std::endl; 
} 
+0

我真的很驚訝您在運行代碼時沒有得到有關未初始化QCoreApplication的良好警告。 – Mat

+0

這實際上是一個實際代碼的精簡版本,它有一個QApplication。 –

+0

檢查我的答案,如果有幫助請寫信 – Blood

回答

5

我認爲你忘了運行

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

,然後檢查

bool QDBusConnection::isConnected() const 

調用

void UbuntuOneDBus::connectReturnSignals() 

或運行程序與一些標誌前,但這應該更容易。

我不想你有什麼目標,但也許你也應該嘗試

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

這裏一些文檔: connectToBus

isConnected

registerObject