I want to invoke a slot asynchronously from a different 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 that calls 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
is of type Paintable*
)
But Qt seems to be unable to use uint16_t or char* in invokeMethod because at runtime I get the following message:
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 custom struct color
using qRegisterMetaType()
but since uint16_t
and char*
are no structs or classes this won't work.
I'd be very glad if somebody could show me how to do it or show a good alternative.
The problem with registering uint16_t is this: It's a typedef, and Qt has already registered this type, but it's under a different name. Since the QMetaType system is based on determining types by their 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With