Results 1 to 3 of 3

Thread: Qt QDbus Sending Custom Types with QVariant

  1. #1
    Join Date
    Jul 2012
    Posts
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Qt QDbus Sending Custom Types with QVariant

    I'm trying to send a custom class ( "Span" ) inside a QVariant across the Dbus session bus in Qt between 2 simple applications. I am using Qt Version 4.8.1 ( but see the same results with 4.8.3 ) Span is a simple class that contains 2 double type properties. I have successfully sent and recovered a QVariant containing just a QString across the dbus interface in the same manner I am trying to do below with a QVariant of a custom class.

    Span contains the following declaration for the QMETATYPE QVariant registration in the class header file:

    Qt Code:
    1. Q_DECLARE_METATYPE(Span)
    To copy to clipboard, switch view to plain text mode 

    I have 2 test applications, one sender and one receiver - both have exactly the same 'Span' class definitions. In my sender app I do this:

    Qt Code:
    1. qDebug() << "Sending QVariant Span to receiver...";
    2. //int spanID = QMetaType::type("Span");
    3. Span span(100,0.5);
    4. //QVariant settingVariant(spanID, &span);
    5. //QVariant settingVariant(QString("HELLO"));
    6. QVariant settingVariant;
    7. settingVariant.setValue(span);
    8.  
    9. QDBusVariant setting( settingVariant );
    10. response = client->setSetting(setting);
    11. qDebug() << "RESPONSE: " << response;
    12.  
    13. QVariant result = setting.variant(); // THIS WORKS - I can just extract my 'Span' here with the correct property values set
    14. QVariant test = QVariant::fromValue(result);
    15. Span testSpan = test.value<Span>();
    16. qDebug() << "Setting Span to -- Low: " << testSpan.m_lowTemp << "High: " << testSpan.m_highTemp;
    To copy to clipboard, switch view to plain text mode 

    The 'setSetting' method is defined as:

    Qt Code:
    1. inline QDBusPendingReply<int> setSetting(const QDBusVariant setting)
    2. {
    3. QList<QVariant> argumentList;
    4. argumentList << QVariant::fromValue(setting);
    5. return asyncCallWithArgumentList(QLatin1String("setSetting"), argumentList);
    6. }
    To copy to clipboard, switch view to plain text mode 

    In the receiver, I register the 'Span' class like this:

    Qt Code:
    1. qRegisterMetaType<Span>();
    2. qDBusRegisterMetaType<Span>();
    To copy to clipboard, switch view to plain text mode 

    and then I attempt to recover the Span class like so:

    Qt Code:
    1. int DbusServerTemplate::setSetting( const QDBusVariant &setting ) {
    2.  
    3. QVariant result = setting.variant();
    4. QVariant test = QVariant::fromValue(result);
    5. Span stuff = test.value<Span>();
    6. qDebug() << "Setting Span to -- Low: " << stuff.m_low << "High: " << stuff.m_high;
    To copy to clipboard, switch view to plain text mode 

    The above code gives me bogus values for the Span class properties:

    Qt Code:
    1. Setting Span to -- Low: 1.44144e-305 High: 5.24729e-261
    To copy to clipboard, switch view to plain text mode 

    What am I doing wrong? I can encode and decode the Span instance in the Sender app but once the receiver class gets it over dbus I'm getting bogus values. I'd really appreciate any ideas / help!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt QDbus Sending Custom Types with QVariant

    Did you implement the streaming operators for converting your type from and to QDBusArgument?

    E.g. see example here http://qt-project.org/doc/qt-4.8/qdb...t.html#details

    Cheers,
    _

  3. #3
    Join Date
    Jul 2012
    Posts
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Qt QDbus Sending Custom Types with QVariant

    Hi there, yes I did here they are - I can extract the properties on the sender application after constructing my Span instance, but just not after receiving it via DBus in the receiver app.

    Qt Code:
    1. // Simple getters for the low and high temps
    2. double Span::getHighTemp() const { return m_highTemp; }
    3. double Span::getLowTemp() const { return m_lowTemp; }
    4.  
    5. // Marshall the Data data into a D-Bus argument
    6. QDBusArgument &operator<<(QDBusArgument &argument, const Span &span)
    7.  
    8. {
    9.  
    10. argument.beginStructure();
    11.  
    12. double high = span.getHighTemp();
    13. double low = span.getLowTemp();
    14. argument << high;
    15. argument << low;
    16. argument.endStructure();
    17.  
    18. return argument;
    19.  
    20. }
    21.  
    22. // Retrieve the Data data from the D-Bus argument
    23. const QDBusArgument &operator>>(const QDBusArgument &argument, Span &span)
    24.  
    25. {
    26.  
    27. double high, low;
    28.  
    29. argument.beginStructure();
    30. argument >> high;
    31. argument >> low;
    32. argument.endStructure();
    33. span.m_highTemp = high;
    34. span.m_lowTemp = low;
    35.  
    36. return argument;
    37.  
    38. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Automatic comparisons for custom types stored in QVariant
    By viulskiez in forum Qt Programming
    Replies: 1
    Last Post: 18th July 2011, 14:07
  2. Working with QDBus and custom types
    By JuBe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2009, 16:20
  3. Meta Types and QVariant
    By kroenecker in forum Qt Programming
    Replies: 4
    Last Post: 26th February 2007, 07:48
  4. QVariant::toString and custom data types
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2007, 15:36
  5. QVariant types support for custom properties
    By Dusdan in forum Qt Tools
    Replies: 9
    Last Post: 11th January 2006, 09:55

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.