July 18, 2011

jr_jags jr_jags
Lab Rat
87 posts

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.

  1. import QtQuick 1.0
  2.  
  3. //This is my pets home qml
  4.  
  5. Rectangle {
  6.     width: home_aspin.width; height: home_aspin.height
  7.  
  8.    AnimatedImage { id: home_aspin; x: 0; y: 0; width: 240; height: 316;
  9.        source: "Home(Aspin)" }
  10.  
  11.    MouseArea {
  12.        id: ahome2map
  13.        x: 165
  14.        y: 268
  15.        width: 72
  16.        height: 36
  17.        onClicked:  {mainLoader.source = "map.qml";}
  18.    }
  19.  
  20.    MouseArea {
  21.        id: ahome2status
  22.        x: 7
  23.        y: 268
  24.        width: 72
  25.        height: 36
  26.        onClicked:  {mainLoader.source = "status.qml";
  27.        }
  28.    }
  29.  
  30.    MouseArea {
  31.        id: ahome2sleep
  32.        x: 19
  33.        y: 108
  34.        width: 72
  35.        height: 36
  36.        onClicked:  {mainLoader.source = "sleep.qml";}
  37.    }
  38.  
  39.    MouseArea {
  40.        id: ahome2play
  41.        x: 156
  42.        y: 102
  43.        width: 72
  44.        height: 36
  45.        onClicked:  {mainLoader.source = "walk.qml";}
  46.    }
  47.  
  48.    MouseArea {
  49.        id: ahome2bath
  50.        x: 151
  51.        y: 51
  52.        width: 72
  53.        height: 36
  54.        onClicked:  {mainLoader.source = "bath.qml";}
  55.    }
  56.  
  57.    MouseArea {
  58.        id: ahome2feed
  59.        x: 19
  60.        y: 51
  61.        width: 72
  62.        height: 36
  63.        onClicked:  {mainLoader.source = "feed.qml";}
  64.    }
  65.  }

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

  1. import QtQuick 1.0
  2.  
  3. //This is my feed qml
  4.  
  5. Rectangle {
  6.     width: feeding.width; height: feeding.height
  7.  
  8.    AnimatedImage { id: feeding; x: 0; y: 0; width: 240; height: 316;
  9.        source: "Feeding(Aspin)" }
  10.  
  11.    MouseArea {
  12.        id: feed2home
  13.        x: 79
  14.        y: 272
  15.        width: 100
  16.        height: 41
  17.        onClicked:  {mainLoader.source = "home_aspin.qml";}
  18.    }
  19.  
  20.     }

  1. import QtQuick 1.0
  2.  
  3. //this is my status qml
  4.  
  5. Rectangle {
  6.     width: sleeping.width; height: sleeping.height
  7.  
  8.     Image {
  9.         id: name
  10.         source: "status.jpg"
  11.     }
  12.     signal textPosted(string textFromnaming)
  13.     onTextPosted: textpreview.text = textFromnaming
  14.  
  15.     MouseArea {
  16.        id: sleep2home
  17.        x: 82
  18.        y: 276
  19.        width: 84
  20.        height: 39
  21.        onClicked:  {mainLoader.source = "home_aspin.qml"; }
  22.    }
  23.  
  24.      Text {
  25.        id: textpreview
  26.        x: 55
  27.        y: 55
  28.        width: 90
  29.        height: 80
  30.        text: "text"
  31.        font.pixelSize: 12
  32.    }
  33.    }

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

15 replies

July 18, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

It looks like a perfect duplicate of Creating a status QML for my virtual pet game [developer.qt.nokia.com]

You don’t need post multiple threads for same problem

July 18, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

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.

July 19, 2011

jr_jags jr_jags
Lab Rat
87 posts

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

July 19, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

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.

July 19, 2011

Alicemirror Alicemirror
Lab Rat
825 posts

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.

 Signature 

Enrico Miglino (aka Alicemirror)
Tech Consulting
Islas Baleares, Ibiza (Spain)
http://www.contesti.eu

July 19, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

@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.

July 19, 2011

Alicemirror Alicemirror
Lab Rat
825 posts

@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 [developer.qt.nokia.com]. This page incldes the link to the full documentation on the project wiki. All will be updated shortly.

 Signature 

Enrico Miglino (aka Alicemirror)
Tech Consulting
Islas Baleares, Ibiza (Spain)
http://www.contesti.eu

July 19, 2011

Alexandra Alexandra
Hobby Entomologist
607 posts
Vijay Bhaska Reddy wrote:
It looks like a perfect duplicate of Creating a status QML for my virtual pet game [developer.qt.nokia.com]

I deleted the duplicated thread.

 Signature 

*THE CAKE IS A LIE*
Web Community Manager - Qt Development Frameworks

July 19, 2011

jr_jags jr_jags
Lab Rat
87 posts

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

July 19, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

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…

July 19, 2011

jr_jags jr_jags
Lab Rat
87 posts

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.

July 19, 2011

jr_jags jr_jags
Lab Rat
87 posts

@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?

July 19, 2011

Vijay Bhaska Reddy Vijay Bhaska Reddy
Lab Rat
399 posts

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.

July 19, 2011

mlong mlong
Mad Scientist
1517 posts

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.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

July 20, 2011

jr_jags jr_jags
Lab Rat
87 posts

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

 
  ‹‹ how to add progress bar and bookmark in qml      Loading a qmldir from a qrc file ››

You must log in to post a reply. Not a member yet? Register here!