PDA

View Full Version : QML interaction with C++ (not only signals & slots)



Palmik
31st January 2010, 17:13
TL;DR version
Is it possible to execute C++ methods from inside of QML just like we can do with functions written in JavaScript?

Hi, I just finished reading the 'Same Game' tutorial (http://qt.nokia.com/doc/qml-snapshot/advtutorial.html). I cant deny that it was good reading and that it left mainly positive impressions within me, but there is one thing I was wondering about - would it be possible to use QML & JavaScript just for animations but implement all logic in C++?
Let's use the tutorial as an example
Button {
id: btnA; text: "New Game"; onClicked: initBoard();
anchors.left: parent.left; anchors.leftMargin: 3
anchors.verticalCenter: parent.verticalCenter
}
MouseRegion {
id: gameMR
anchors.fill: parent; onClicked: handleClick(mouse.x,mouse.y);
}

Initialization of the board (initBoard()) does not have to be necessarily considered part of program logic by someone (however I do consider it game logic :)). But clicking on the stones (which is here solved by using mouse region and then calculating the index of the stone of the coordinates of the click - handleClick(int, int)) is definitely part of the game logic (it might not be obvious in this scenario since the game is so simple) but imagine something like gomoku, chess, shogi or whatever.

So now to the point.
Question: Is it possible to link onClicked and other triggers (signals? O.o) to C++ functions (or better methods) instead of JavaScript ones?
Because if yes (yahaha it would be so great! :D) I could easily imagine QGraphicsItem which graphic appearance (colors, background, animations and stuff) would be designed in QML (and even JavaScript) but the sole logic of the item would be in C++, the you would just add the given item into the QGraphicsScene and call it a day.

Disclaimer: I'm not saying this because I think the tutorial is bad au contraire for it's reason which is to show the capabilities of QML. What I'm trying to say is, that I either need another totorial | help from you or new feature :) I hope no one will be offended by this thread. :)

Palmik
1st February 2010, 20:32
Hi,
I think I might have solution. I asked this question in another discussion and the reply was following

Q:



Hi,
I read the comments in this discussion and a lot of people mentioned that you can use QML as frontend fro your C++ backend. Well, I also read the ‘Same game’ tutorial which made me think - is it really possible? If so, please, let me know how, because I’m clueless in this case.
How would you hook (looking at the second page of the tutorial http://qt.nokia.com/doc/qml-snapshot/advtutorial2.html) C++ version of the initBoard() function (in JavaScript there) to the QML?
Or how would you hook (looking at the third page of the turorial http://qt.nokia.com/doc/qml-snapshot/advtutorial3.html) C++ version of the handleClick(int, int) function to the QML?

Because I really do not know how to connect QML and C++. It would be really wonderful to be able to code the looks & animations in QML and JavaScript and then use them in QGraphicsView framework. (You would write the look n’ feel in QML + JS then somehow hook it to your QGraphicsItem subclass and then easily just add the instance of your subclassed item to the scene).

R:

If you expose any C++ object to the Qml Context by setting it as a context property (QmlContext::setContextProperty()) you can access all properties, connect signals/slots and directly invoke slots() from the C++ Object.
So I searched for QmlContext::setContextProperty() (http://qt.nokia.com/doc/qml-snapshot/qmlcontext.html#setContextProperty) . Sure, I can make some variables visible to QML objects thank to this, but still I'm not quite sure how I could use signals and slots thanks to this.