Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Sending commands to another QML when mousarea is clicked
Forum Update on Monday, May 27th 2025

Sending commands to another QML when mousarea is clicked

Scheduled Pinned Locked Moved QML and Qt Quick
16 Posts 5 Posters 5.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jr_jags
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • T Offline
      T Offline
      thisisbhaskar
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thisisbhaskar
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jr_jags
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • T Offline
            T Offline
            thisisbhaskar
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • AlicemirrorA Offline
              AlicemirrorA Offline
              Alicemirror
              wrote on last edited by
              #6

              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.

              Enrico Miglino (aka Alicemirror)
              Balearic Dynamics
              Islas Baleares, Ibiza (Spain)
              www.balearicdynamics.com

              1 Reply Last reply
              0
              • T Offline
                T Offline
                thisisbhaskar
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0
                • AlicemirrorA Offline
                  AlicemirrorA Offline
                  Alicemirror
                  wrote on last edited by
                  #8

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

                  Enrico Miglino (aka Alicemirror)
                  Balearic Dynamics
                  Islas Baleares, Ibiza (Spain)
                  www.balearicdynamics.com

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    troubalex
                    wrote on last edited by
                    #9

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

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

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      jr_jags
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        thisisbhaskar
                        wrote on last edited by
                        #11

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

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          jr_jags
                          wrote on last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            jr_jags
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              thisisbhaskar
                              wrote on last edited by
                              #14

                              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.

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mlong
                                wrote on last edited by
                                #15

                                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.

                                Software Engineer
                                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                                1 Reply Last reply
                                0
                                • J Offline
                                  J Offline
                                  jr_jags
                                  wrote on last edited by
                                  #16

                                  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

                                  1 Reply Last reply
                                  0

                                  • Login

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • Users
                                  • Groups
                                  • Search
                                  • Get Qt Extensions
                                  • Unsolved