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. Question regarding Loader and Signals

Question regarding Loader and Signals

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 5 Posters 8.5k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    billouparis
    wrote on last edited by
    #1

    Here is the example:

    main.qml
    @Rectangle {
    width: 360
    height: 360

    Loader{
        id: menuLoader
        source: "SceneMenu.qml"
    }
    
    Connections {
        id: menuConnection
        target: menuLoader.item
        onGamestart:
        {
            menuLoader.source = "GameStarted.qml"
    
        }
    }
    

    }
    @

    SceneMenu.qml
    @Rectangle {
    width: 100
    height: 62
    color: "red"
    signal gamestart

    MouseArea{
        anchors.fill: parent
        onClicked: {
            gamestart()
        }
    }
    

    }
    @

    GameStarted.qml
    @
    Rectangle {
    width: 100
    height: 62
    color: "blue"

    }
    @

    This example causes an issue while clicking on the red rectangle:
    @QML Connections: Cannot assign to non-existent property "onGamestart"@

    I found two workarounds:
    either declare the same signal inside the GameStarted.qml file
    signal gamestart

    or modify the main connection with:

    @ Connections {
    id: menuConnection
    target: menuLoader.item
    onGamestart:
    {
    menuConnection.target = null
    menuLoader.source = "GameStarted.qml"

        }
    }
    

    @

    Is there any better way of doing this, I mean a real solution?
    Thank you,

    Bill

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dmcr
      wrote on last edited by
      #2

      What if you give an id to your rectangle like id : idRedRectangle, and then call idRedRectangle.gamestart() ?

      P.S. : i would also declare the signal like signal gamestart()

      dmcr

      1 Reply Last reply
      0
      • B Offline
        B Offline
        billouparis
        wrote on last edited by
        #3

        I am not sure you get the point i was emphasizing in the first message. Maybe I have to give more details. The error is occuring because once the second qml files is loaded, that is the GameStarted.qml in my exemple, the onGamestart signal/property does not exist anylonger, and the connection is raising an error.

        The problem is not located while sending the signal from the first file (SceneMenu.qml).

        Anyway I changed my code to follow your suggestions, and it does nothing better or worse.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dmcr
          wrote on last edited by
          #4

          Hello,

          i read the code fast, and indeed it was quite predictible that nothing changed "better or worse" ;).
          However i think the loader is done to load an qml object at a special time, which is what you are doing, but in that case why did you use a single loader ?
          I would use two, if the scene menu has to be loaded dynamicly,
          or have only once, with the menu a simple item, and the source loaded as you done, then the the two objects still exists.

          Anyway the second code may be all right, even if i did not understand
          @_menuConnection.target = null_@

          dmcr

          1 Reply Last reply
          0
          • B Offline
            B Offline
            billouparis
            wrote on last edited by
            #5

            Hello dmcr,

            using two loaders will cost more in memory I guess, and i'm always in seek of the least memory consumption. Also it will require more time to compile two objects rather than one if I am right? Gotta check again with the QtCreator QML profiler.

            I don't want to still have both QLM objects still existing at the same time.

            The menuConnection.target = null instruction is one workaround I found to get rid of the Connection once the second QML is loaded/ is to be loaded to avoid the error I had previously, although I am absolutely not sure what it really does internally, and if this instruction can lead to other problems else where.

            Thank you for your suggestions anyway. Any other are still welcome :)

            Bill

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dmcr
              wrote on last edited by
              #6

              Ah ok, i understand why you want to have only one object. ( memory seeking )
              In that case, i think the solution is to have some script to manage the case, as you have done, switching between the two, and in that case you may have all your "valuable values" in the main object ?
              This way is a little delicate, but if you do it properly it should work ; the truth is that i was not aware that memory was still a problem ;)

              dmcr

              dmcr

              1 Reply Last reply
              0
              • B Offline
                B Offline
                billouparis
                wrote on last edited by
                #7

                It sure still is for embedded systems on targets! Especially when working for the low-cost solution market...

                1 Reply Last reply
                0
                • X Offline
                  X Offline
                  X-Krys
                  wrote on last edited by
                  #8

                  You should use the ignoreUnknownSignals on your Connection element.

                  More details in Qt Doc : http://qt-project.org/doc/qt-4.8/qml-connections.html#ignoreUnknownSignals-prop :

                  bq. Property Documentation
                  ignoreUnknownSignals : bool
                  Normally, a connection to a non-existent signal produces runtime errors.
                  If this property is set to true, such errors are ignored. This is useful if you intend to connect to different types of objects, handling a different set of signals for each object.

                  1 Reply Last reply
                  0
                  • Aaron HouA Offline
                    Aaron HouA Offline
                    Aaron Hou
                    wrote on last edited by
                    #9

                    So it is.
                    Thanks!

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #10

                      [quote author="arrowhou" date="1384162598"]So it is.
                      Thanks![/quote]

                      Hi,

                      I also found that "ignoreUnknownSignals:true" will solve the above problem. Please mark this post as "Solved, if that solves the problem.

                      Ansif

                      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