Padawan42
Padawan42

Reputation: 117

Qt RemoteObjects

I try to send custom Object with Qt Remote Objects.

I declared my class like that:

#include <QDebug>
#include <QObject>
#include <QDataStream>

class Model : public QObject {
  Q_OBJECT
public:
  explicit Model(QObject *parent = nullptr);

  Model(const Model &other);

  ~Model();

  bool operator!=(Model &other);
  Model operator=(Model &other);

  QDataStream operator<<(const Model&);
  Model operator>>(const QDataStream);
signals:

public slots:

public:
  QString text1;
  QString text2;

};

Q_DECLARE_METATYPE(Model)

My rep file looks like:

#include "../model/Model.h"

class Remote {
  PROP(Model resultModel);

  SLOT(void resultModelChanged_slot(Model resultModel));
};

When I try to build the application I get the following error:

error: no match for ‘operator<<’ (operand types are ‘QDataStream’ and ‘const Model’)
         stream << *static_cast<const T*>(t);

Does anyone know about some complex QtRemoteObject example? The example I have found is pretty simple and works (https://doc.qt.io/qt-5.10/qtremoteobjects-gettingstarted.html) but I haven't found an example with complex (custom) types.

In the documentation about rpc I read that customTypes are supported (https://doc.qt.io/qt-5.10/qtremoteobjects-repc.html)

Would be awesome if someone has an idea about that issue...

Thanks

Upvotes: 1

Views: 1095

Answers (1)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38499

It must be declared in the global scope, outside of the class:

QDataStream &operator<<(QDataStream &stream, const Model&);

Be careful with the returned value, the operator << must return the same stream as it takes through the first argument, i.e. by a reference.

The class inside should declare a friend function:

friend QDataStream &operator<<(QDataStream &stream, const Model&);

Upvotes: 3

Related Questions