Results 1 to 7 of 7

Thread: QDBus : Send an array of objects (C++)

  1. #1
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QDBus : Send an array of objects (C++)

    Hello,

    I have to send on the dbus an array of objects but I don't know how to do it.
    I have a class that contains four variables and I need to create an array of objects of this class and to send it to another application on dbus.
    I read the tutorial about the custom type but I didn't understand well how to modify operator << and >> , and how to modify the xml file in order to produce the interface and the adaptor.
    In the xml file I used type="(iiii)" and I added the annotation row but in this one I don't know what I have to write if the value is an array of objects...
    Does anyone can help me please?

    Thanks you in advance
    Last edited by lordjoseph; 24th January 2012 at 17:19.

  2. #2
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QDBus : Send an array of objects (C++)

    In the xml file what type do I have to write in the arg if the arg is a QList<MyClass> ?

  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDBus : Send an array of objects (C++)

    http://dbus.freedesktop.org/doc/dbus...ml#type-system
    in your case it will be:
    a(iiii)

    Added after 14 minutes:


    for operators it is quite simple.
    You don't have to do it for QList (template will handle that).
    Remember to declare meta type for struct and register dbus type
    Last edited by MarekR22; 25th January 2012 at 14:16.

  4. #4
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QDBus : Send an array of objects (C++)

    I'm already using it but I have some problem....

    Qt Code:
    1. <!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
    2. <node>
    3. <interface name="org.my.test">
    4. <signal name="signalSent">
    5. <arg name="asignals" type="a(iiii)" direction="out"/>
    6. <annotation name="com.trolltech.QtDBus.QtTypeName.In0" value="SignalsArray"/>
    7. </signal>
    8. <method name="SetSignal">
    9. <arg name="asignals" type="a(iiii)" direction="out"/>
    10. <annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="SignalsArray"/>
    11. </method>
    12. </interface>
    13. </node>
    14.  
    15. /****************************** TxSignal.h *************/
    16. #include <QtDBus>
    17. #include <QtCore/QList>
    18. #include <QtCore/QMetaType>
    19.  
    20.  
    21. class TxSignal {
    22.  
    23.  
    24. public:
    25. TxSignal();
    26. virtual ~TxSignal();
    27.  
    28.  
    29. //Getter
    30. QString getSurname();
    31. QString getName();
    32. float getValue();
    33.  
    34. //Setter
    35. void setSurname(QString Surname);
    36. void setName(QString Name);
    37. void setValue(float Value);
    38.  
    39.  
    40. //register Message with the Qt type system
    41. //static void registerMetaType();
    42. friend const QDBusArgument &operator>>(const QDBusArgument &argument, TxSignal &msignals);
    43. friend QDBusArgument &operator<<(QDBusArgument &argument, const TxSignal &msignals);
    44.  
    45.  
    46.  
    47. public:
    48. QString m_Surname;
    49. QString m_Name;
    50. double m_Value;
    51.  
    52. };
    53.  
    54. typedef QList<TxSignal> SignalsArray;
    55.  
    56. Q_DECLARE_METATYPE(TxSignal);
    57. Q_DECLARE_METATYPE(SignalsArray);
    58.  
    59. inline void registerCommType(){
    60. qDBusRegisterMetaType<TxSignal>();
    61. qDBusRegisterMetaType< SignalsArray >();
    62. }
    63.  
    64. /****************************************** TxSignal.cpp *************************************************/
    65.  
    66.  
    67. #include "TxSignal.h"
    68.  
    69. TxSignal::TxSignal() {
    70. // TODO Auto-generated constructor stub
    71.  
    72. }
    73.  
    74. TxSignal::~TxSignal() {
    75. // TODO Auto-generated destructor stub
    76. }
    77.  
    78. QDBusArgument &operator <<(QDBusArgument &argument, const TxSignal &msignals)
    79. {
    80. argument.beginStructure();
    81. argument << msignals.m_Surname;
    82. argument << msignals.m_Name;
    83. argument << msignals.m_Value;
    84. argument.endStructure();
    85.  
    86. return argument;
    87. }
    88.  
    89. const QDBusArgument &operator >>(const QDBusArgument &argument, TxSignal &msignals)
    90. {
    91. argument.beginStructure();
    92. argument >> msignals.m_Surname;
    93. argument >> msignals.m_Name;
    94. argument >> msignals.m_Value;
    95. argument.endStructure();
    96. return argument;
    97. }
    98.  
    99. QString TxSignal::getName()
    100. {
    101. return this->m_Name ;
    102. }
    103.  
    104. QString TxSignal::getSurname()
    105. {
    106. return this->m_Surname ;
    107. }
    108.  
    109. float TxSignal::getValue()
    110. {
    111. return this->m_Value ;
    112. }
    113.  
    114. void TxSignal::setName(QString Name)
    115. {
    116. this->m_Name = Name;
    117.  
    118. }
    119.  
    120. void TxSignal::setSurname(QString Surname)
    121. {
    122. this->m_Surname = Surname;
    123. }
    124.  
    125.  
    126. void TxSignal::setValue(float Value)
    127. {
    128. this->m_Value = Value;
    129. }
    130. /*************************************** TestSignal.h *********************************************/
    131.  
    132. #include <QObject>
    133. #include <QStringList>
    134. #include "TxSignal.h"
    135.  
    136.  
    137. class TestSignal : public QObject{
    138.  
    139. Q_OBJECT
    140.  
    141. //Local variables
    142. QString m_Surname;
    143. QList<TxSignal> m_asignals;
    144.  
    145. Q_CLASSINFO("D-Bus Interface", "org.my.test")
    146.  
    147. public:
    148. TestSignal(QObject* parent = 0);
    149. virtual ~TestSignal();
    150.  
    151. signals:
    152.  
    153. void signalSent(const SignalsArray &asignals);
    154.  
    155. public slots:
    156.  
    157. void SetSignal(const SignalsArray &asignals);
    158.  
    159.  
    160. };
    161.  
    162. /*************************************** TestSignal.cpp *******************************************/
    163.  
    164. #include <iostream>
    165. using namespace std;
    166. #include "TestSignal.h"
    167. #include "TestSignalAdaptor.h"
    168. #include "TestSignalInterface.h"
    169.  
    170. TestSignal::TestSignal(QObject *parent) {
    171. // add our D-Bus interface and connect to D-Bus
    172. new TestsignalAdaptor(this);
    173. QDBusConnection::sessionBus().registerService("org.my.test");
    174. QDBusConnection::sessionBus().registerObject("/org/my/test", this);
    175.  
    176. org::my::test *iface;
    177. iface = new org::my::test(QString(), QString(), QDBusConnection::sessionBus(), this);
    178. bool ret;
    179. ret = connect(iface, SIGNAL(signalSent(SignalsArray)),this, SLOT(SetSignal(SignalsArray)));
    180. if (ret==TRUE) {
    181. cout<<"\nTRUEEEEE";
    182. fflush(stdout);
    183. }
    184. SignalsArray prova;
    185. TxSignal nuovo;
    186. nuovo.m_Surname = "111222333";
    187. nuovo.m_Name = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA****";
    188. prova.append(nuovo);
    189.  
    190. int i=0;
    191. for(i=0;i<20;i++);
    192. emit signalSent(prova);
    193.  
    194. cout<<"\nSent";
    195.  
    196. }
    197.  
    198.  
    199. void TestSignal::TestSignal(const SignalsArray &asignals)
    200. {
    201. cout<<asignals.at(0).m_Surname.toAscii().data();
    202. cout<<"\nMetodo richiamato";
    203. fflush(stdout);
    204.  
    205. }
    206.  
    207. TestSignal::~TestSignal() {
    208. // TODO Auto-generated destructor stub
    209. }
    To copy to clipboard, switch view to plain text mode 

    In this way I can't read the QList sent, if I try to send a QString it works fine(after modifying the xml and the code) , so where is the problem?
    Can you help me?

    Thanks

  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDBus : Send an array of objects (C++)

    problem is that type in xml is wrong.
    i - means 32 bit integer value and you don't have ANY int values in you structure.
    your type description in xml should be: "a(ssd)"
    a - stands for array
    s - stands for string
    d - stands for double

    also contents of xml should be in class declaration as a meta data in Q_CLASSINFO().

    See qt documentation and examples of d-bus. Apparently you didn't read that carefully.

  6. #6
    Join Date
    Jan 2012
    Posts
    4
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QDBus : Send an array of objects (C++)

    I read that (iiii) can be used for every type... am I wrong?
    xml should be in the class?
    it is in the Adaptor file generated by qdbusxml2cpp tool, anywhere else right?

  7. #7
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QDBus : Send an array of objects (C++)

    You had to misunderstand something.
    Check table how to describe d-bus types.
    See some example.

Similar Threads

  1. Multi-dimensional objects array
    By matulik in forum Newbie
    Replies: 3
    Last Post: 23rd October 2011, 02:20
  2. How to send a array to server?
    By Niamita in forum Qt Programming
    Replies: 6
    Last Post: 25th July 2011, 07:12
  3. qml send a list/array from signal
    By goli in forum Qt Programming
    Replies: 0
    Last Post: 9th May 2011, 09:46
  4. Qt Designer How to create an array of objects??
    By dominate in forum Qt Tools
    Replies: 2
    Last Post: 22nd January 2011, 16:34
  5. QtScript: Passing an array of objects to C++
    By Plow in forum Qt Programming
    Replies: 0
    Last Post: 16th April 2010, 12:53

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.