Qt signal / slot mechanism over the network

I want to be able to send Qt signals over the network. Serialization of a signal call is fairly straightforward using the Qt meta-system:

  • Create qMetaMethod using the static method ::fromSignal
  • Get the method name, parameter names, their typeIds [1] and values ​​using the created meta-method.
  • Pack everything in your preferred format (JSON, XML) and send it.

But so far I have not been able to figure out how to call a signal using serialized data: QMetaObject::invokeMethod(..) takes the signal / method name as a string. The problem is with the arguments: they should be provided as a QGenericArgument , and they can only be created using the Q_ARG macro, which requires the actual type (not its name or typeId ) and the corresponding value. In addition, during compilation, it is necessary to determine the number of arguments; there is no invokeMethod(..) that accepts a list of arguments.

Am I missing something? Or is there a better / alternative way to do this?

[1] A further question: how can I guarantee that types always get the same type of ID when registering with Q_DECLARE_METATYPE(..) ?

+5
source share
1 answer

It is not true that you cannot create a QGenericArgument yourself. You are not recommended, but what you are trying to do is very implementation dependent. It is not so much: you specify the type name and the pointer to the data of this type. For instance:.

 QGenericArgument one() { static const char type[] = "int"; static const int data = "1"; return QGenericArgument{type, (void*)&data); } 

Refer to the Non-Retractable Visitor section of this answer for more sample code.

How to ensure that types always get the same type identifier when registering with Q_DECLARE_METATYPE(..) ?

No. You must use type names, and each process must localize them for types.

If you do not want to implement it yourself, use something ready-made, for example, MIT-licensed qt-remote-signals .

+3
source

All Articles