XChinux's Home

过往资料可到百度空间http://hi.baidu.com/xchinux

http://www.qtcn.org/bbs/u/2  [收藏] [复制]

XChinux

爱Qt,爱N9,qtcn.org

  • 602

    关注

  • 2024

    粉丝

  • 4828

    访客

  • 等级:光明使者
  • 身份:总版主
  • 总积分:10857
  • 男,1979-01-01
  • 山西

最后登录:2019-03-24

更多资料

日志

使用QtDBus实现运行一个实例

2011-07-16 09:24
自己将其进行了简单的封装成一个QtDBusObject类,适用于支持dbus的系统中,构造函数的第一个参数为service名称,必须确保在系统中是唯一的。内容如下,不再详细解释,感兴趣的,请细读:
QtDBusObject.hpp
  1. #ifndef CHINUXTEAM_QTDBUS_OBJECT_HPP
    #define CHINUXTEAM_QTDBUS_OBJECT_HPP

    #include <QtCore>
    #include <QtGui>

    namespace ChinuxTeam
    {
    class QtDBusObject : public QObject
    {
        Q_OBJECT
    public:
        enum Status
        {
            NotRunning,
            RaiseSuccess,
            RaiseFailure
        };
        QtDBusObject(const QString &service, QObject *parent = 0);
        ~QtDBusObject();
        bool isConnected();
        Status isRunningAndRaiseStatus();
        bool registerService();
        bool registerObject();
        void setActivationWindow(QWidget *widget);
    public slots:
        void raise();
    private:
        QString _service;
        QWidget *_activationWindow;
    };
    }

    #endif


QtDBusObject.cpp
  1. #include <QtDBus>
    #include "QtDBusObject.hpp"

    namespace ChinuxTeam
    {
    QtDBusObject::QtDBusObject(const QString &service, QObject *parent)
        : QObject(parent), _service(service), _activationWindow(NULL)
    {
    }

    QtDBusObject::~QtDBusObject()
    {
    }

    bool QtDBusObject::isConnected()
    {
        return QDBusConnection::sessionBus().isConnected();
    }


    QtDBusObject::Status QtDBusObject::isRunningAndRaiseStatus()
    {
        QDBusInterface iface(_service, "/", "",
                        QDBusConnection::sessionBus());
        if (iface.isValid())
        {
            QDBusReply<void> reply = iface.call("raise");
            if (reply.isValid())
            {
                return RaiseSuccess;
            }

            qDebug() << "call singleton instance raise failed"
                << reply.error().message();
            return RaiseFailure;
        }
        return NotRunning;
    }

    bool QtDBusObject::registerService()
    {
        return QDBusConnection::sessionBus().registerService(_service);
    }

    bool QtDBusObject::registerObject()
    {
        return QDBusConnection::sessionBus().registerObject("/", this,
                        QDBusConnection::ExportAllSlots);
    }

    void QtDBusObject::setActivationWindow(QWidget *widget)
    {
        _activationWindow = widget;
    }

    void QtDBusObject::raise()
    {
        if (_activationWindow == NULL)
        {
            return;
        }
        _activationWindow->setWindowState(_activationWindow->windowState()
                        & ~Qt::WindowMinimized);
        _activationWindow->raise();
        _activationWindow->activateWindow();
    }
    }



使用示例:
  1. #include <QtGui/QApplication>
    #include <QtGui/QMainWindow>
    #include "QtDBusObject.hpp"

    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);

        ChinuxTeam::QtDBusObject dbusobj("org.qtcn.sicompass");
        if (!dbusobj.isConnected())
        {
            qDebug() << "cannot connect to dbus session bus";
            app.quit();
            return EXIT_FAILURE;
        }

        switch (dbusobj.isRunningAndRaiseStatus())
        {
            case ChinuxTeam::QtDBusObject::NotRunning:
                break;
            case ChinuxTeam::QtDBusObject::RaiseSuccess:
                app.quit();
                return EXIT_SUCCESS;
            case ChinuxTeam::QtDBusObject::RaiseFailure:
                app.quit();
                return EXIT_FAILURE;
        }

        if (!dbusobj.registerService())
        {
            qDebug() << "register service failed";
            app.quit();
            return EXIT_FAILURE;
        }

        QMainWindow viewer;
        dbusobj.setActivationWindow(&viewer);
        if (!dbusobj.registerObject())
        {
            qDebug() << "register object failed";
            app.quit();
            return EXIT_FAILURE;
        }
        viewer.show();
        return app.exec();
    }


分类:编程技术|回复:0|浏览:3767|全站可见|转载
 

Powered by phpwind v8.7 Certificate Copyright Time now is:03-25 14:16
©2005-2016 QTCN开发网 版权所有 Gzip disabled