Solved qml function in Qt6
-
So I've been porting one of my older programs into Qt6 and I've been getting things working so far, but I am struggling with a function that I had to get help with a while back:
https://forum.qt.io/topic/104946/qstringlist-in-qml/15.
This was able to make a QStringList accessible to my QML function so I could assign text to a bunch of labels.
Here is the QML function:
Item { id: readlistValues function readValues(anArray) { for (var i=0; i<33; i++) //console.log("list: ", anArray[i]) label0Text = anArray[0] //snip... label33Text = anArray[33] } }
Now when this function is called, I get the error: "Error: Cannot assign [undefined] to QString".
To make things REALLY strange - when I change the Connection{} that calls this function from the Qt6 version to the deprecated version, it works (my label text's are populated), but I get the message:
"Parameter "lblMsg" is not declared. Injection of parameters into signal handlers is deprecated. Use JavaScript functions with formal parameters instead."
// Deprecated Connections { target: modfinder onSetModule: { modFinder.setModule(lblMsg) } } // Connections { // target: modFinder // function onSetModule(lblMsg) { // modFinder.setModule(lblMsg) // } // }
<edited - added information>
Any help appreciated!
-
-
@MScottM said in qml function in Qt6:
Item { id: readlistValues function readValues(anArray) { for (var i=0; i<33; i++) //console.log("list: ", anArray[i]) label0Text = anArray[0] //snip... label33Text = anArray[33] } }
anArray[33]
? Are you sure you have so many elements? Also I'd very much question the wisdom of having a million of these things:labelXXXText
."Parameter "lblMsg" is not declared. Injection of parameters into signal handlers is deprecated. Use JavaScript functions with formal parameters instead."
Connections { target: modfinder onSetModule: { modFinder.setModule(lblMsg) } }
modFinder
vsmodfinder
, alsolblMsg
isn't defined, specify explicitly through an object id. Is this property part of thetarget
? Perhaps it's supposed to be:modFinder.setModule(modFinder.lblMsg)
-
Hello @kshegunov, thank you for taking a look! To your question about if I'm sure about the number of elements, yes, there are exactly 33 labels that I set with different information when ever a particular GUI element is clicked. The text for the elements are read from a configuration file that I can change whenever something in the system changes.
Having said that, I do this kind of thing as a challenge to learn and increase my skill - I would consider myself an advanced beginner. If you can suggest a better way to do this, I will happily listen! This was one of the trickiest parts of the program and I remember spending quite a bit of time figuring out how to get it working.
So here is something really strange: the deprecated Connection that you show above is the one that works, and I never noticed the issue with the (non)capitalized name. If I change it to modFinder, it stops working. I have that defined in my main.cpp:
moduleFinder modFinder;
If I change the new version to match, it still doesn't work (crashes after "ReferenceError: modfinder is not defined")
Connections { target: modfinder //changed from modFinder function onSetModule(lblMsg) { modFinder.setModule(modFinder.lblMsg) } }
-