c++ - Registering Qt::[Enum/Flag] as QtMetaType -
i wanted create object access values of qmouseevent
.
this event has e.g.
qt::mousebutton qmouseevent::button() const
so add class myqmlmouseevent
q_property(qt::mousebutton button read button)
and necessary getter.
i expected, on qml side, can use such:
myqmlmouseevent.button === qt.buttonleft
as qt.buttonleft
available, , used e.g. mousearea
's clicked
signal.
now test, handle signal in pass myqmlmouseevent
-instance in qml this:
onmyqmlmousevent: console.log(json.stringify(mymouseevent))
which prints properties, fails on qt::mousebutton
type. gives me error:
qmetaproperty::read: unable handle unregistered datatype
'qt::mousebutton'
property'myqmlmouseevent::button'
here might solve calling qregistermetatype
register type.
unsure if in case right or work for
qt
available in qml - registering parts of again seems wrong me?- can
qregistermetatype
enums , flags class have little control about?
intrestingly: fails qt::mousebutton
, qt::mouseeventflags
not qt:mousebuttons
(which alias qt::mousebutton
).
means circumvent problem qt::mousebutton
changing type plural, , qt::mouseeventflags
passed int
indeed not available in qml (should qt.mouseeventcreateddoubleclick
)
still, out of curiosity, , if might necessary in future: what right way register enums , flags qt
namespace if not registered yet? don't want register them every time in main.cpp
whenever use completly different class.
set q_property
type int
there no need declare or register metatypes use enum.
c++
class mousebuttonobject : public qobject { q_object q_property(int button read button) public: explicit mousebuttonobject(qobject *parent = nullptr); qt::mousebutton button() { return qt::leftbutton; } };
qml
if (mousebuttonobject.button === qt.leftbutton) { console.log("button left button") }
Comments
Post a Comment