Results 1 to 2 of 2

Thread: QML interaction with C++ (not only signals & slots)

  1. #1
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QML interaction with C++ (not only signals & slots)

    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. 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
    cpp Code:
    1. Button {
    2. id: btnA; text: "New Game"; onClicked: initBoard();
    3. anchors.left: parent.left; anchors.leftMargin: 3
    4. anchors.verticalCenter: parent.verticalCenter
    5. }
    To copy to clipboard, switch view to plain text mode 
    cpp Code:
    1. MouseRegion {
    2. id: gameMR
    3. anchors.fill: parent; onClicked: handleClick(mouse.x,mouse.y);
    4. }
    To copy to clipboard, switch view to plain text mode 

    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! ) 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.

  2. #2
    Join Date
    Jan 2009
    Location
    Czech Republic
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QML signal - slot interaction with C++ (QmlContext)

    Hi,
    I think I might have solution. I asked this question in another discussion and the reply was following

    Q:
    Quote Originally Posted by Palmik

    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:
    Quote Originally Posted by Thomas
    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() . 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.

Similar Threads

  1. about signals and slots
    By Sandip in forum Qt Programming
    Replies: 9
    Last Post: 15th July 2008, 17:02
  2. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 11:31
  3. Signals and slots
    By sylvarant in forum Newbie
    Replies: 4
    Last Post: 11th September 2007, 16:48
  4. Signals and Slots
    By merry in forum Qt Programming
    Replies: 4
    Last Post: 22nd February 2007, 09:11
  5. Signals and Slots in dll
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2006, 09:12

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.