Chinaunix首页 | 论坛 | 博客
  • 博客访问: 700840
  • 博文数量: 112
  • 博客积分: 2486
  • 博客等级: 大尉
  • 技术积分: 1541
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-14 18:30
文章分类

全部博文(112)

文章存档

2012年(5)

2011年(48)

2010年(26)

2009年(33)

我的朋友

分类: LINUX

2011-05-09 22:43:47

服务端:
    1. {
    2.     if (!QDBusConnection::connectToBus(QDBusConnection::SessionBus,QString("c2box")).isConnected()) {
    3.       fprintf(stderr, "Cannot connect to the D-Bus session bus.\n"
    4.       "To start it, run:\n"
    5.       "\teval `dbus-launch --auto-syntax`\n");
    6.       return;
    7.     }

    8.     if (!QDBusConnection::connectToBus(QDBusConnection::SessionBus,QString("c2box")).registerService(DBUS_SERVICE_NAME)) {
    9.       fprintf(stderr, "%s\n",
    10.       qPrintable(QDBusConnection::connectToBus(QDBusConnection::SessionBus,QString("c2box")).lastError().message()));
    11.       exit(1);
    12.     }

    13.     PlayerSetting* playerSetting = this;
    14. //把playerSetting 作为interface,即把playerSetting对象中的
    15.     QDBusConnection::connectToBus(QDBusConnection::SessionBus,QString("c2box")).registerObject(DBUS_PLAYER_SETTING_PATH, playerSetting, QDBusConnection::ExportAllSlots);
    16.   }
  1. 客户端:
  1.  QDBusInterface* dbusIterface;
  2. if(!QDBusConnection::connectToBus(QDBusConnection::SessionBus,QString("c2box")).isConnected()) {
  3.       fprintf(stderr, "Cannot connect to the D-Bus session bus.\n"
  4.               "To start it, run:\n"
  5.               "\teval `dbus-launch --auto-syntax`\n");
  6.       return;
  7.   }
  8.   dbusIterface = new QDBusInterface(DBUS_SERVICE_NAME, DBUS_PLAYER_SETTING_PATH, "", QDBusConnection::connectToBus(QDBusConnection::SessionBus,QString("c2box")), parent);
  9.   if ((dbusIterface!=NULL) && !dbusIterface->isValid()) {
  10.     delete dbusIterface;
  11.     dbusIterface = NULL;
  12.   }

//通过call 调用dbus service 提供的接口GetValue,item为参数。得到返回的结果为int类型的QDBusReply
  QDBusReply reply = dbusIterface->call("GetValue", item);
  if (reply.isValid()) {
    int value = reply.value();
//通过call 调用dbus service 提供的接口GetString,item为参数。得到返回的结果为QString类型的//QDBusReply
 QDBusReply reply = dbusIterface->call("GetString", item);
  if (reply.isValid()) {
    return reply.value();
  }


推出客户端时候:
  if (dbusIterface!=NULL)
    delete dbusIterface;


阅读(2677) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~