Results 1 to 3 of 3

Thread: Namespaces in QtDBus

  1. #1
    Join Date
    Jan 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Namespaces in QtDBus

    Hello,

    I have a problem concerning the usage of my own types in QtDBus. I managed to make the program work without namespaces, but when I include the type I want to (de)marshall in a namespace, the method using this type does not show up on DBus.

    I boiled the whole probem down to the following small (compilable) example:
    Qt Code:
    1. #ifndef HEADER_HPP
    2. #define HEADER_HPP
    3.  
    4. #include <QtCore/QCoreApplication>
    5. #include <QtDBus/QtDBus>
    6. #include <QtCore/QObject>
    7. #include <QMetaType>
    8. #include <iostream>
    9.  
    10. namespace Test {
    11. struct S {
    12. int number;
    13. QString name;
    14. double time;
    15. };
    16. }
    17.  
    18. QDBusArgument &operator<<(QDBusArgument &argument, const Test::S &arg);
    19. const QDBusArgument &operator>>(const QDBusArgument &argument, Test::S &t);
    20.  
    21. Q_DECLARE_METATYPE(Test::S)
    22.  
    23.  
    24.  
    25. namespace Test {
    26. class Foo : public QObject {
    27. Q_OBJECT
    28. public:
    29. void foo1() { // some code
    30. }
    31.  
    32. void foo2(S input) { // some code
    33. }
    34. };
    35.  
    36.  
    37. class FooAdaptor : public QDBusAbstractAdaptor {
    38. Q_OBJECT
    39. Q_CLASSINFO("D-Bus Interface", "com.Test.Foo")
    40. public:
    41. FooAdaptor(Foo* srv_appl) : QDBusAbstractAdaptor(srv_appl) {
    42.  
    43. setAutoRelaySignals(true);
    44. qDBusRegisterMetaType<S>();
    45. }
    46.  
    47. public slots:
    48.  
    49. int foo1() { // some invokation code
    50. }
    51.  
    52. void foo2(S dummy) { // some invokation code
    53. }
    54. };
    55. }
    56.  
    57. #endif
    58.  
    59. // the CPP-File looks like:
    60. #include "header.hpp"
    61.  
    62. QDBusArgument &operator<<(QDBusArgument &argument, const Test::S &arg) {
    63. argument.beginStructure();
    64.  
    65. argument << arg.number;
    66. argument << arg.name;
    67. argument << arg.time;
    68.  
    69. argument.endStructure();
    70. return argument;
    71. }
    72.  
    73. const QDBusArgument &operator>>(const QDBusArgument &argument, Test::S &arg) {
    74. argument.beginStructure();
    75.  
    76. argument >> arg.number;
    77. argument >> arg.name;
    78. argument >> arg.time;
    79.  
    80. argument.endStructure();
    81. return argument;
    82. }
    83.  
    84. // The main file:
    85. #include <QtCore/QCoreApplication>
    86. #include "header.hpp"
    87. using namespace Test;
    88.  
    89. int main(int argc, char *argv[])
    90. {
    91. QCoreApplication a(argc, argv);
    92.  
    93. qDBusRegisterMetaType<S>();
    94.  
    95. Foo f;
    96. new FooAdaptor(&f);
    97.  
    98. QDBusConnection connection = QDBusConnection::sessionBus();
    99. connection.registerObject("/com/Test/Foo", &f);
    100. connection.registerService("com.Test.FooService");
    101.  
    102. return a.exec();
    103. }
    To copy to clipboard, switch view to plain text mode 

    But when I run this program and run qdbus, the method foo2 does not show up:
    Qt Code:
    1. $ qdbus com.Test.FooService /com/Test/Foo
    2. method int com.Test.Foo.foo1()
    3. method QDBusVariant org.freedesktop.DBus.Properties.Get(QString interface_name, QString property_name)
    4. method QVariantMap org.freedesktop.DBus.Properties.GetAll(QString interface_name)
    5. method void org.freedesktop.DBus.Properties.Set(QString interface_name, QString property_name, QDBusVariant value)
    6. method QString org.freedesktop.DBus.Introspectable.Introspect()
    To copy to clipboard, switch view to plain text mode 

    This is irritating to me, as the program works fine, when I leave out the namespace. I also tried to implement the op>> and op<< in the namespace Test (i.e. namespace Test { const QDBusArgument &operator>>(const QDBusArgument &argument, S &arg) ... ). This compiles as well, but the method foo2 does not show up on the DBus. Including Q_DECLARE_METATYPE in the namespace does not compile.

    I do not really understand this behavior, as I declare S as a metatype, provide the op<< and op>> and I register the type on DBus. However, the inclusion of the struct in a namespace seems to screw the whole thing up.

    What am I doing wrong and how can I fix this?

    Thank you very much in advance!

    Best regards,
    Daniel

  2. #2
    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: Namespaces in QtDBus

    Try first declare and register meta type:
    Qt Code:
    1. Q_DECLARE_METATYPE(Test::S) // declaration
    2.  
    3. // this should be before qDBusRegisterMetaType<Test::S>();
    4. qRegisterMetaType<Test::S>();
    To copy to clipboard, switch view to plain text mode 


    Another thing what happened to:
    Qt Code:
    1. Q_CLASSINFO("D-Bus Introspection", ""
    2. " <interface name=\"com.Test.Foo\">\n"
    3. " <method name=\"foo1\">\n"
    4. " </method>\n"
    5. " <method name=\"foo2\">\n"
    6. " <arg direction=\"in\" type=\"(isd)\" name=\"input\"/>\n"
    7. " </method>\n"
    8. " </interface>\n"
    9. "")
    To copy to clipboard, switch view to plain text mode 
    Last edited by MarekR22; 2nd February 2011 at 08:33. Reason: updated contents

  3. #3
    Join Date
    Jan 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Namespaces in QtDBus

    Hello Marek,

    thank you for your reply. I inserted the Q_CLASSINFO macro and it solved the problem. Thank you very much.

    I did assume that the Introspection information was not necessary for the workings with DBus, as it does not show up in some official examples from Trolltech, like http://doc.trolltech.com/latest/qdbu...orexample.html and so, I planed to leave it out (I do not use the DBus compiler). I guess, I was wrong!

    Thank you for clearing this up!

Similar Threads

  1. QtDBus Qt Creator
    By sngskunk in forum Qt Programming
    Replies: 2
    Last Post: 22nd August 2019, 08:15
  2. Replies: 0
    Last Post: 9th January 2011, 10:06
  3. Help with QtDBus signals
    By zuck in forum Qt Programming
    Replies: 3
    Last Post: 20th May 2009, 05:33
  4. QtDBus on Windows
    By ManuMies in forum Qt Programming
    Replies: 9
    Last Post: 26th January 2009, 13:02
  5. cannot find -QtDBus
    By rajeshs in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2007, 15:08

Tags for this Thread

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.