PDA

View Full Version : Problems with marshalling a struct to Qt/DBus



laumann
28th December 2009, 17:56
Hi people,


I'm (fairly) new to C++/Qt/DBus and stuck with a problem I need to solve ASAP. I've also read the provided tutorials from the Qt Assistant and tried to look up on the internet for a solution, however no help as of now. I strongly hope anyone of you guys can help me out.

Let's get straight to the point:


I'm trying to send this simple struct over DBus:



/*** Types.h ***/
// Struct Definition:
struct Position {
int x;
int y;
int angle;
};

The Qt Metatype declaration(s) are also made:



// Qt metatype declaration:
Q_DECLARE_METATYPE(Position)
Q_DECLARE_METATYPE(QList<Position>)


The operators << and >> are being overloaded for proper serializing the struct:



// Marshall the Position data into a D-Bus argument
inline QDBusArgument &operator<<(QDBusArgument &argument, const Position &myPos)
{
argument.beginStructure();
argument << myPos.x << myPos.y << myPos.angle;
argument.endStructure();
return argument;
}

// Retrieve the Position data from the D-Bus argument
inline const QDBusArgument &operator>>(const QDBusArgument &argument, Position &myPos)
{
argument.beginStructure();
argument >> myPos.x >> myPos.y >> myPos.angle;
argument.endStructure();
return argument;
}


As far as that goes, everything's fine. Now, I've set up an Dbus interface for the transmission:



/*** Interface.cpp ***/

// Declarations in the interface constructor:
qDBusRegisterMetaType<Position>();
qDBusRegisterMetaType<QList<Position> >();



Finally, this is my method for the creation of the method call - it's also where the trouble lies in:




// calling the overloaded operator '<<':
bool Interface::sendPosition(Position pos) {
QDBusMessage message = QDBusMessage::createMethodCall(DEST,PATH,IFACE, "sendPos");
message << pos; // ERROR happens here
return QDBusConnection::sessionBus().send(message);
}


The error occurrs when trying to compile the code - it's something around the usage of the overloaded << operator. This is the error message I get:

src/Interface.cpp: In member function ‘bool Interface::sendPosition(QString, Position)’:
src/Interface.cpp:179: error: no match for ‘operator<<’ in ‘message << pos’
/usr/include/qt4/QtDBus/qdbusmessage.h:109: note: candidates are: QDBusMessage& QDBusMessage::operator<<(const QVariant&)
/usr/include/qt4/QtCore/qchar.h:389: note: QDataStream& operator<<(QDataStream&, const QChar&)
[...]
//plenty of candidates listed, but none is matching...
[...]
src/Types.h:27: note: QDBusArgument& operator<<(QDBusArgument&, const Position&)


OS is Ubuntu Linux 9.04, Qt Version 4.5.0 (installed with the 'Synaptic Package Manager').


Any help is highly appreciated!


Thanks + Cheers,
Sascha.

elysion
8th April 2010, 10:53
Hi,

I'm trying to do quite about exactly the same thing you have tried. I get the code to compile, but for some reason the method that takes the struct as a parameter, does not show up in the method listing I get with qdbus.

Have you managed to get this working yet? Anyone have any ideas what might be wrong?

elysion
8th April 2010, 11:25
Hi again,

I noticed that (at least if i understand your post correctly) you seem to have the stream operators in the header file. For me this didn't work. To get it working, I added the signatures of the methods to the header file and the implementation to a separate source file. This way it does compile, but as stated before, does not show up in the method list.

Vladimir Jovic
21st January 2014, 10:40
Check the signature http://qt-project.org/doc/qt-4.8/qdbusmessage.html#operator-lt-lt

The simplest solution :

// calling the overloaded operator '<<':
bool Interface::sendPosition(Position pos) {
QDBusMessage message = QDBusMessage::createMethodCall(DEST,PATH,IFACE, "sendPos");
message << QVariant::fromValue(pos);
return QDBusConnection::sessionBus().send(message);
}