Question regarding Loader and Signals
-
Here is the example:
main.qml
@Rectangle {
width: 360
height: 360Loader{ 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 gamestartMouseArea{ 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 gamestartor 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
-
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()
-
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.
-
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_@ -
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
-
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
-
It sure still is for embedded systems on targets! Especially when working for the low-cost solution market...
-
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. -
So it is.
Thanks! -
[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