Simple acces of C++ object data from QML -
i'm making board game in qt/c++ using qml. important game data represented in single class. intention have qml sheets access 1 object , draw game board depending on data.
what simplest approach exposing c++ data members qml?
now know question has been asked, i've seen answers , qt documentation. am, however, not satisfied. i've seen, way seems to make q_property out of every single variable want access qml. looks me tedious , unnecessary, not mention stretch code 3x it's original length, making worse read. also, in cases won't need write function data members, example.
and why bother q_property overhead when write q_invokable getters situations need?
here's example of how simple hoped when read in qt project documentation: "this enables c++ data , functions accessible directly qml, little or no modification."
class game : public qobject { q_object public: explicit game(qobject *parent = 0); colors npc[3]; // colors being enum declared elsewhere player players[4]; // player non-qobject class containing player stats } ... game gmain; qdeclarativecontext *context = viewer.rootcontext(); context->setcontextproperty("game",&gmain);
qml in ideal world:
image { id : image1 source: { if (game.npc[0] === 0) { if (game.players[1].lifecount > 0) { return "pics/figg.png" } else { return "pics/stoneg.png" } }
now how close can qml , how go it? i'm interested in handling simple c++ style arrays , enums (have lot of in game) - need write helper functions, e.g. int game.getnpcat(int i)
instead of using game.npc[i]
?
i realize way don't want tried , trusted, , reason... in situation (small one-man project) seems using cannon kill fly (although gui building part in qml amazingly simple , quite joy use) - having wrap around every data member including simplest int seems... ridiculously excessive.
maybe have missed somewhere, in case humbly apologize. thank thoughts on matter.
in order:
q_property
: when @ page quoted, discuss using q_property
method expose properties qml. if don't use q_property
, understanding variables won't registered qmlviewer (or have you). q_property
needs q_invokable
get/set variables. if don't use q_property
, though, class properties not appear in qml.
setting image source: if may remember, qml forge between css , javascript. if you're looking make image's source change depending on condition outside of image
element, can create javascript function achieve have quoted:
image { id: image1 function getimage() { if (game.npc[0] === 0) { if (game.players[1].lifecount > 0) { image1.source="pics/figg.png"; } else { image1.source="pics/stoneg.png"; } } }
however, function won't run itself: you'll have associate signal, create in c++ class (put function under label named signals:
(not within public
-- see here on how write signals)). based on example, i'm guessing c++ object called game
.
game { id: gamekeeper //or whatever want name onupdate: image1.getimage() //replace onupdate signal } image { id: image1 function getimage() { if (gamekeeper.npc[0] === 0) { if (gamekeeper.players[1].lifecount > 0) { image1.source="pics/figg.png"; } else { image1.source="pics/stoneg.png"; } } }
in theory, should able reference arrays way javascript (i'm not familiar js myself).
array handling: on c++ side, looks best way through qlist. fortunately, qlist iterates similar normal array. found this, should -- ignore second dimension.
hope helps.
Comments
Post a Comment