Sending commands to another QML when mousarea is clicked



  • Good day guys, im developing a virtual pet game, like tamagotchi, and i was wondering on how i can make a status qml that can show the status of the chosen pet.
    @import QtQuick 1.0

    //This is my pets home qml

    Rectangle {
    width: home_aspin.width; height: home_aspin.height

    AnimatedImage { id: home_aspin; x: 0; y: 0; width: 240; height: 316;
    source: "Home(Aspin)" }

    MouseArea {
    id: ahome2map
    x: 165
    y: 268
    width: 72
    height: 36
    onClicked: {mainLoader.source = "map.qml";}
    }

    MouseArea {
    id: ahome2status
    x: 7
    y: 268
    width: 72
    height: 36
    onClicked: {mainLoader.source = "status.qml";
    }
    }

    MouseArea {
    id: ahome2sleep
    x: 19
    y: 108
    width: 72
    height: 36
    onClicked: {mainLoader.source = "sleep.qml";}
    }

    MouseArea {
    id: ahome2play
    x: 156
    y: 102
    width: 72
    height: 36
    onClicked: {mainLoader.source = "walk.qml";}
    }

    MouseArea {
    id: ahome2bath
    x: 151
    y: 51
    width: 72
    height: 36
    onClicked: {mainLoader.source = "bath.qml";}
    }

    MouseArea {
    id: ahome2feed
    x: 19
    y: 51
    width: 72
    height: 36
    onClicked: {mainLoader.source = "feed.qml";}
    }
    }
    @

    For example i want to feed my pet, i will click the mousearea id:ahome2feed and it will load the feed qml

    @import QtQuick 1.0

    //This is my feed qml

    Rectangle {
    width: feeding.width; height: feeding.height

    AnimatedImage { id: feeding; x: 0; y: 0; width: 240; height: 316;
    source: "Feeding(Aspin)" }

    MouseArea {
    id: feed2home
    x: 79
    y: 272
    width: 100
    height: 41
    onClicked: {mainLoader.source = "home_aspin.qml";}
    }

    }
    

    @

    @import QtQuick 1.0

    //this is my status qml

    Rectangle {
    width: sleeping.width; height: sleeping.height

    Image {
        id: name
        source: "status.jpg"
    }
    signal textPosted(string textFromnaming)
    onTextPosted: textpreview.text = textFromnaming
    
    MouseArea {
       id: sleep2home
       x: 82
       y: 276
       width: 84
       height: 39
       onClicked:  {mainLoader.source = "home_aspin.qml"; }
    

    }

     Text {
       id: textpreview
       x: 55
       y: 55
       width: 90
       height: 80
       text: "text"
       font.pixelSize: 12
    

    }
    }
    @

    the status in my pet game is hunger, and it has a default value of 50, since i click the feed mousearea, i want to increase the hunger status of my pet, increasing the default value to 5, so huger will be 55,

    How can i do this?
    can you please provide me some codes?

    Thanks in advance,
    hope you could help me in my project



  • It looks like a perfect duplicate of "Creating a status QML for my virtual pet game":http://developer.qt.nokia.com/forums/viewthread/7866/

    You don't need post multiple threads for same problem



  • its kind of hard to achieve what you have told as you are dynamically creating items.

    Once you change the source of mainLoader, the old item is deleted which looses all the history/local data of it.



  • so the transitions of my qml to another qml is wrong?
    what should i do?



  • Others looks ok .. but for status you cannot use mainLoader.source. Have an instance of status created and keep it always and call a method of it when ever you want to update your status.



  • Hi,

    please take a look to the Qt-Complex framework I have released. It works with a loader object (a replica of the Qml loader with some custom properties) and see how it works. You can include the framework in your code and use it, it is done just for this.

    The principle is that you canuse the how many different instances of the same loader structure and destroy the loaded objects when you need. The events are passed between the loaded objects and the main objects throught the loader in a transparent mode while all the properties changes / settings are done binding values to custom properties between the main qml and the loader instances while are managed with functions between the loaded object and the loader properties.

    Thus, you pass parameters in a independent way while events works like a pass-thorugh.
    I put a first quick-start guide of the framework on the wiki and the last updated documentation can be found together with the sources at projects.developer.nokia.com/Qt-Complex

    If see before the Qt-Complex project you find also a test application qml document where there is an example of loaded objects and previously loaded objects deleted by another object.



  • @Alicemirror, Did you prepare a wiki with available elements documentation, and the steps to include Qt-Complex framework in to applications? And few samples will also help.

    I guess if you have good documentation, it will become easy for people to try it out.



  • @Vijay, I am doing so. This is the Beta 1.0 today I release the 1.0 rc. This complete version includes the dynamic gemonmetry management for multiple devices. The documentation is on for some pieces and other will be updated in very few time. I have already started to write a first wiki page "here":http://developer.qt.nokia.com/wiki/category:Tools::QtComplex. This page incldes the link to the full documentation on the project wiki. All will be updated shortly.



  • [quote author="Vijay Bhaska Reddy" date="1310999670"]It looks like a perfect duplicate of "Creating a status QML for my virtual pet game":http://developer.qt.nokia.com/forums/viewthread/7866/
    [/quote]

    I deleted the duplicated thread.



  • what if i join all the items in my status qml to my home qml,



  • did not get you .. do you mean creating an alias property in your home.qml for all the status properties in yoru status.qml??

    Can you elaborate...



  • i will remove the status qml, and i will create texts in my home qml that will show the status of my pet which is hunger and had a default value of 50.
    just like what i said on my first post, if click the feed mousearea it will show my feed qml that shows the dog eating his food. when i go back to my home qml, the hunger status of my pet will increase to 5, how can i do that.



  • @Vijay Bhaska Reddy youre righ, when i load another qml using loader, the items on my qml are reset to its original value, how can i fix that?

    Is there any other way i can view other qml using mousearea without resetting my qml?



  • If you use Loader, it will always reset. Try other approaches and post code if you face any problem.

    But one thing for sure you need to store status / store in global object.

    Try out storing it in C++ side, expose few properties of a QObject derived class and updated them with your score/status in your QML side.

    When you call status.qml page, read these properties and display them. This seems like a reasonable solution to me.


  • Moderators

    Also, another thought, depending on the structure of your app's QML and how many elements you have, and so on, you could possibly forego the Loader altogether and create all of your elements at runtime. Then you could set the opacity or visible properties to show or hide individual elements as needed. That way you could still maintain bindings between different elements of your application.



  • After clicking mousearea to load another qml, i will make the changed items properties i dont want to reset? OK ill try this, Thanks, hope this will work


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.