Results 1 to 15 of 15

Thread: how can i pass objects using DBus?

  1. #1
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Question how can i pass objects using DBus?

    hello everybody,
    i want to pass objects between processes using DBus i have read the documentation but i did not understand it very well.
    i understood that i can not pass objects except struct , arrays, and Qmap .
    i must declare the object with the macro Q_DECLARE_METATYPE() ,and implement the operators << >> to serialize QDBusArgument .
    how then can i pass the object QDBusArgument between two processes?

  2. #2
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Default Re: how can i pass objects using DBus?

    AnyBody?
    plz help

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: how can i pass objects using DBus?

    What did you already try? Do you have an adaptor in one application and an interface to it in the other one? Please post the service object (not the data object) you want to share between applications.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Question Re: how can i pass objects using DBus?

    the data and service object are
    Qt Code:
    1. class City{
    2. int cityId;
    3. QString cityName;
    4. double latitude;
    5. double longtude;
    6. };
    7. class ClientServiceProvider : public QObject
    8. {
    9. Q_OBJECT
    10. Q_CLASSINFO("D-Bus Interface", "com.mycompany.ptimes")
    11. public:
    12. ClientServiceProvider(QObject *parent = 0);
    13. public slots:
    14. QString getSettingsValue(QString );
    15. void setSettingsValue(QString);
    16. City getCountries();
    17. signals:
    18.  
    19. private:
    20. void loadSettings();
    21.  
    22.  
    23. private:
    24. DBManager* m_pDBMan;
    25. QHash<QString,Settings*> m_settingsHash;
    26. };
    To copy to clipboard, switch view to plain text mode 

    i want to return the city object from the function getCountries().
    i have already interface and adaptor and i am already using dbus to pass QString and QStringList.
    but the problem is to pass custom objects
    is it possible to pass custom objects and how?
    if not , is there any other solution to do some thing like this?
    Last edited by wysota; 13th January 2010 at 13:16.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: how can i pass objects using DBus?

    Well, you need to implement streaming operators (for QDataStream) for the object and declare a metatype for the class.

    Something like:
    Qt Code:
    1. struct City {
    2. int id;
    3. QString name;
    4. double lat;
    5. double lon;
    6. };
    7.  
    8. Q_DECLARE_METATYPE(City);
    9.  
    10. QDataStream& operator<<(QDataStream &str, const City &city){
    11. str <<"City"<< id << name << lat << lon;
    12. return str;
    13. }
    14.  
    15. QDataStream& operator>>(QDataStream &str, City &city){
    16. QString magic;
    17. str >> magic;
    18. if(magic!="City") return str; // error
    19. magic >> city.id >> city.name >> city.lat >> city.lon;
    20. return str;
    21. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Default Re: how can i pass objects using DBus?

    thank you ,
    but after that how can i transfer the QDataStream between the two process .
    should i define the function as follows:

    QDataStream getCountries();

    instead of
    City getCountries();

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: how can i pass objects using DBus?

    No, why? You just need to be able to serialize your structure so that Qt can forward it to dbus.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Question Re: how can i pass objects using DBus?

    i put the code above in my application as follows
    Qt Code:
    1. QDBusArgument &operator<<(QDBusArgument &argument, const QList<Country> &countries)
    2. {
    3. argument.beginArray();
    4. for(int i = 0 ; i < countries.count(); i++)
    5. {
    6. argument<<countries.at(i).countryId << countries.at(i).countryName;
    7. }
    8. argument.endArray();
    9. return argument;
    10. }
    11. const QDBusArgument &operator>>(const QDBusArgument &argument, QList<Country> &countries)
    12. {
    13.  
    14. return argument;
    15. }
    To copy to clipboard, switch view to plain text mode 

    and every thing working fine but when i implement the operator>> i get a compilation error :
    country.cpp: In function ‘const QDBusArgument& operator>>(const QDBusArgument&, QList<Country>&)’:
    country.cpp:18: error: no match for ‘operator>>’ in ‘argument >> ((QList<Country>*)countries)->QList<T>::at [with T = Country](i)->Country::countryId’
    what is the problem?
    Last edited by wysota; 14th January 2010 at 17:09.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: how can i pass objects using DBus?

    Are you sure the signature is correct? Should QDbusArgument be a const reference and not a non-const reference?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Default Re: how can i pass objects using DBus?

    it is constant and here are the declaration of the tow operators
    Qt Code:
    1. QDBusArgument &operator<<(QDBusArgument &argument, const QList<Country> &countries);
    2. const QDBusArgument &operator>>(const QDBusArgument &argument, QList<Country> &countries);
    To copy to clipboard, switch view to plain text mode 
    note when i ran the application without writing any code inside operator>> except return statement, the application compiled
    but when it call a dbus function the server and the client were crashed.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: how can i pass objects using DBus?

    Did you remember about qDBusRegisterMetaType?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #12
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Question Re: how can i pass objects using DBus?

    yes i used it in the main. look this my code
    country.h
    Qt Code:
    1. #ifndef MYSTRUCT_H
    2. #define MYSTRUCT_H
    3.  
    4. #include <QtCore/QList>
    5. #include <QtCore/QMetaType>
    6. #include <QtDBus/QtDBus>
    7.  
    8. class Country
    9. {
    10. public:
    11. int countryId;
    12. QString countryName;
    13. };
    14. Q_DECLARE_METATYPE(QList<Country>)
    15. inline void registerCommType()
    16. {
    17. qDBusRegisterMetaType< QList<Country> >();
    18. }
    19.  
    20. QDBusArgument &operator<<(QDBusArgument &argument, const QList<Country> &countries);
    21. const QDBusArgument &operator>>(const QDBusArgument &argument, QList<Country> &countries);
    22.  
    23. #endif
    To copy to clipboard, switch view to plain text mode 
    country.cpp
    Qt Code:
    1. #include "country.h"
    2.  
    3. QDBusArgument &operator<<(QDBusArgument &argument, const QList<Country> &countries)
    4. {
    5. argument.beginArray();
    6. for(int i = 0 ; i < countries.count(); i++)
    7. {
    8. argument<<countries.at(i).countryId << countries.at(i).countryName;
    9. }
    10. argument.endArray();
    11. return argument;
    12. }
    13. const QDBusArgument &operator>>(const QDBusArgument &argument, QList<Country> &countries)
    14. {
    15. argument.beginArray();
    16. for(int i = 0 ; i < countries.count(); i++)
    17. {
    18. argument>>countries.at(i).countryId >> countries.at(i).countryName;
    19. }
    20. argument.endArray();
    21. return argument;
    22. }
    To copy to clipboard, switch view to plain text mode 
    when i compile this code i get the following error
    country.cpp: In function ‘const QDBusArgument& operator>>(const QDBusArgument&, QList<Country>&)’:
    country.cpp:18: error: no match for ‘operator>>’ in ‘argument >> ((QList<Country>*)countries)->QList<T>::at [with T = Country](i)->Country::countryId’

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: how can i pass objects using DBus?

    The operator's contents seems incorrect to me. What will countries.count() return in the for loop? To me it seems the loop will never be entered so even if the code compiled, it wouldn't work. Also in the first function beginArray() should probably contain the meta-type id as a parameter. Next, QList::at() is not an L-value but a R-value, you can't assign data to it. The code should probably look like this:
    Qt Code:
    1. const QDBusArgument &operator>>(const QDBusArgument &argument, QList<Country> &countries) {
    2. argument.beginArray();
    3. countries.clear();
    4. while(!argument.atEnd()){
    5. Country country;
    6. argument >> country.countryId >> country.countryName;
    7. countries.append(country);
    8. }
    9. argument.endArray();
    10. return argument;
    11. }
    To copy to clipboard, switch view to plain text mode 

    But I think this won't work either as here QDBusArgument assumes you are going to read arrays and not arrays of structs. So the final code should be:
    Qt Code:
    1. const QDBusArgument &operator>>(const QDBusArgument &argument, QList<Country> &countries) {
    2. argument.beginArray();
    3. countries.clear();
    4. while(!argument.atEnd()){
    5. Country country;
    6. argument >> country;
    7. countries.append(country);
    8. }
    9. argument.endArray();
    10. return argument;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Of course you have to match operator<< to it.

    Next... The docs say that you don't have to write this code for QList. So all you need to do is to provide serialization of your Country structure.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Default Re: how can i pass objects using DBus?

    i used this code but still i get a compilation error
    country.h
    Qt Code:
    1. #ifndef MYSTRUCT_H
    2. #define MYSTRUCT_H
    3.  
    4. #include <QtCore/QList>
    5. #include <QtCore/QMetaType>
    6. #include <QtDBus/QtDBus>
    7.  
    8. class Country
    9. {
    10. public:
    11. int countryId;
    12. QString countryName;
    13. };
    14. Q_DECLARE_METATYPE(Country)
    15. inline void registerCommType()
    16. {
    17. qDBusRegisterMetaType< Country >();
    18. }
    19.  
    20. QDBusArgument &operator<<(QDBusArgument &argument, const Country &aCountry);
    21. const QDBusArgument &operator>>(const QDBusArgument &argument, Country & aCountry);
    22.  
    23. #endif
    To copy to clipboard, switch view to plain text mode 
    country.cpp
    Qt Code:
    1. #include "country.h"
    2.  
    3. QDBusArgument &operator<<(QDBusArgument &argument, const Country &aCountry)
    4. {
    5. argument.beginStructure();
    6. argument << aCountry.countryId << aCountry.countryName;
    7. argument.endStructure();
    8. return argument;
    9. }
    10. const QDBusArgument &operator>>(const QDBusArgument &argument, Country & aCountry)
    11. {
    12. argument.beginStructure();
    13. argument >> aCountry.countryId >> aCountry.countryName;
    14. argument.endStructure();
    15. return argument;
    16. }
    To copy to clipboard, switch view to plain text mode 
    i get the following error
    In file included from /usr/include/qt4/QtDBus/qdbusmacros.h:46,
    from /usr/include/qt4/QtDBus/qdbusconnection.h:45,
    from /usr/include/qt4/QtDBus/QDBusConnection:1,
    from main.cpp:4:
    /usr/include/qt4/QtCore/qmetatype.h: In static member function ‘static int QMetaTypeId2<T>::qt_metatype_id() [with T = QList<Country>]’:
    /usr/include/qt4/QtCore/qmetatype.h:199: instantiated from ‘int qMetaTypeId(T*) [with T = QList<Country>]’
    /usr/include/qt4/QtDBus/qdbuspendingreply.h:85: instantiated from ‘int QDBusPendingReplyTypes::metaTypeFor(T1*) [with T1 = QList<Country>]’
    /usr/include/qt4/QtDBus/qdbuspendingreply.h:98: instantiated from ‘static void QDBusPendingReplyTypes::ForEach<T1, T2, T3, T4, T5, T6, T7, T8>::fillMetaTypes(int*) [with T1 = QList<Country>, T2 = void, T3 = void, T4 = void, T5 = void, T6 = void, T7 = void, T8 = void]’
    /usr/include/qt4/QtDBus/qdbuspendingreply.h:192: instantiated from ‘void QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::calculateMetaTypes() [with T1 = QList<Country>, T2 = void, T3 = void, T4 = void, T5 = void, T6 = void, T7 = void, T8 = void]’
    /usr/include/qt4/QtDBus/qdbuspendingreply.h:199: instantiated from ‘void QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::assign(const QDBusPendingCall&) [with T1 = QList<Country>, T2 = void, T3 = void, T4 = void, T5 = void, T6 = void, T7 = void, T8 = void]’
    /usr/include/qt4/QtDBus/qdbuspendingreply.h:141: instantiated from ‘QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>& QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>:perator=(const QDBusPendingCall&) [with T1 = QList<Country>, T2 = void, T3 = void, T4 = void, T5 = void, T6 = void, T7 = void, T8 = void]’
    /usr/include/qt4/QtDBus/qdbuspendingreply.h:135: instantiated from ‘QDBusPendingReply<T1, T2, T3, T4, T5, T6, T7, T8>::QDBusPendingReply(const QDBusPendingCall&) [with T1 = QList<Country>, T2 = void, T3 = void, T4 = void, T5 = void, T6 = void, T7 = void, T8 = void]’
    ptimes_interface_p.h:43: instantiated from here
    /usr/include/qt4/QtCore/qmetatype.h:189: error: ‘qt_metatype_id’ is not a member of ‘QMetaTypeId<QList<Country> >’
    and here are the function i use in the interface class
    Ptimes_interface_p.h
    Qt Code:
    1. inline QDBusPendingReply<QList<Country> > getCountries()
    2. {
    3. QList<QVariant> argumentList;
    4. return asyncCallWithArgumentList(QLatin1String("getCountries"), argumentList);
    5. }
    To copy to clipboard, switch view to plain text mode 
    and here how i used this function

    Qt Code:
    1. PtimesInterface * iface = new PtimesInterface("com.mycompany.ptimes","/ClientServiceProvider", QDBusConnection::sessionBus(), this);
    2. QList<Country> countries = iface->getCountries();
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Feb 2008
    Posts
    154
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    12

    Default Re: how can i pass objects using DBus?

    i found a solution for my previous problem with the code
    Qt Code:
    1. #ifndef MYSTRUCT_H
    2. #define MYSTRUCT_H
    3.  
    4. #include <QtCore/QList>
    5. #include <QtCore/QMetaType>
    6. #include <QtDBus/QtDBus>
    7.  
    8. class Country
    9. {
    10. public:
    11. int countryId;
    12. QString countryName;
    13. };
    14. Q_DECLARE_METATYPE(QList<Country>)
    15. Q_DECLARE_METATYPE(Country)
    16. inline void registerCommType()
    17. {
    18. qDBusRegisterMetaType< QList<Country> >();
    19. qDBusRegisterMetaType< Country >();
    20.  
    21. }
    22.  
    23. QDBusArgument &operator<<(QDBusArgument &argument, const QList<Country> &countries);
    24. const QDBusArgument &operator>>(const QDBusArgument &argument, QList<Country> &countries);
    25. QDBusArgument &operator<<(QDBusArgument &argument, const Country &countries);
    26. const QDBusArgument &operator>>(const QDBusArgument &argument, Country &countries);
    27.  
    28. #endif
    To copy to clipboard, switch view to plain text mode 
    but i got another problem. the program are compiled but when i use a function calling DBus the program crashes and i got the following message
    QDBusArgument: read from a write-only object
    process 4668: arguments to dbus_message_unref() were incorrect, assertion "message->generation == _dbus_current_generation" failed in file dbus-message.c line 1392.
    This is normally a bug in some application using the D-Bus library.
    *** glibc detected *** /home/mismael/workspace/Nokia_N900_PKG_2010/Sources/PrayerTimes/PrayerTimesClient/PrayerTimesClient: double free or corruption (!prev): 0x091f6078 ***

Similar Threads

  1. Replies: 3
    Last Post: 9th January 2010, 16:47
  2. need DBus help
    By nrabara in forum Newbie
    Replies: 2
    Last Post: 2nd May 2009, 08:41
  3. Qt 4.5.0 win opensource and <dbus/dbus.h>
    By YaK in forum Installation and Deployment
    Replies: 2
    Last Post: 22nd March 2009, 12:06
  4. DBus and IPC
    By DrDonut in forum Qt Programming
    Replies: 3
    Last Post: 23rd March 2008, 00:42
  5. Replies: 7
    Last Post: 18th July 2006, 22:33

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.