这个Qt DBus信号连接代码是否正确?

时间:2012-06-25 14:00:50

标签: c++ qt ubuntu dbus

这是我第一次使用DBus,所以我不能完全确定我是否正确地使用它。我正在尝试连接Ubuntu One DBus服务并获取我的应用程序的登录凭据,但是我连接到DBus返回信号的详细信息here似乎永远不会被触发,尽管返回了肯定的结果在连接期间。

在我开始寻找与此特定服务相关的详细信息中的错误之前,有人可以告诉我这个代码是否会在第一时间起作用,或者我在这里做错了什么?

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;
}

1 个答案:

答案 0 :(得分: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