Using uint16_t and char * with QMetaObject :: invokeMethod ()
I want to call an async slot from another thread using QMetaObject :: invokeMethod ()
The class containing the slot is declared as:
class Paintable : public QObject {
Q_OBJECT
[...]
public slots:
void drawString(uint16_t x, uint16_t y, uint16_t size, const char* str, color c);
}
and the method calling invokeMethod is defined as:
void drawStringAsynchronously(uint16_t x, uint16_t y, uint16_t size, const char* str, color c) {
QMetaObject::invokeMethod(paintable,
"drawString",
Qt::QueuedConnection,
Q_ARG(uint16_t, x), Q_ARG(uint16_t, y), Q_ARG(uint16_t, size),
Q_ARG(const char*, str), Q_ARG(color, c));
}
(where paintable
has a type Paintable*
)
But Qt doesn't seem to be able to use uint16_t or char * in invokeMethod because I get the following message at runtime:
QMetaMethod::invoke: Unable to handle unregistered datatype 'const char*'
and
QMetaMethod::invoke: Unable to handle unregistered datatype 'uint16_t'
respectively.
I was able to successfully register my own construct color
with qRegisterMetaType()
, but since uint16_t
both char*
are not structs or classes, this will not work.
I would be very happy if someone could show me how to do this or show me a good alternative.
source to share
The problem with registering uint16_t is this: it is a typedef, and Qt has already registered this type, but under a different name. Since the QMetaType system is based on defining types by name, this causes problems.
You can get around this with
Q_DECLARE_METATYPE(uint16_t)
then
qRegisterMetaType<uint16_t>("uint16_t");
This creates an alias so that metatypes can be created with that name.
source to share