Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
uint8_t and friends on QML
GRAlves last edited by GRAlves
I have a C++ class with several slots with signatures like void slotname(uint8_t arg1, uint16_t arg2). I would like to call those slots from QML. I have successfully exposed an object of this class to the QML engine, but when I try to call the method with :
from QML I get the following error:
Error: Unknown method parameter type: uint8_t
What is the correct way to expose uint8_t and friends types to qml?
uint8_tis way to specific for QML (the JS engine in specific). Or set the slot parameter type to QVariant and let QML do the conversation. Then if you really like you can get a uint8_t out of the QVariant. But in every case you never can guarantee that the types are the same between C++ and QML.
GRAlves last edited by
Is there a way to register a "default conversion" from QVariant to uint8_t, throwing exceptions if it is out of range? I have over 200 slots that use uint8 and friends and I don't want to change the signature of them (would impact a lot of existing code).
Is there a way to register a "default conversion" from QVariant to uint8_t, throwing exceptions if it is out of range?
no and no.
You can write a script which does a regex replacement for all parameters to int type.
Or depending on your code design it may be possible to introduce a "wrapper slot / invokable method"? Then expose this object to QML which forwards the calls to your existing class.
But there is no solution for a clean integration without changing your code. The JS- and C++ world are just not fully compatible.