Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. qml function in Qt6
Qt 6.11 is out! See what's new in the release blog

qml function in Qt6

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 977 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.
  • M Offline
    M Offline
    MScottM
    wrote on last edited by MScottM
    #1

    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!

    kshegunovK 1 Reply Last reply
    0
    • M Offline
      M Offline
      MScottM
      wrote on last edited by
      #4

      Okay, got it.

      Per this StackExchange answer, and this Qt page, the following is an accepted way to do what I need:

      onSetModule: lblMsg => modFinder.setModule(lblMsg)
      

      Anyway, it works and clears up the warnings.

      1 Reply Last reply
      0
      • M MScottM

        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!

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #2

        @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 vs modfinder, also lblMsg isn't defined, specify explicitly through an object id. Is this property part of the target? Perhaps it's supposed to be:

        modFinder.setModule(modFinder.lblMsg)
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        1
        • M Offline
          M Offline
          MScottM
          wrote on last edited by
          #3

          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)
                  }
              }
          
          1 Reply Last reply
          0
          • M Offline
            M Offline
            MScottM
            wrote on last edited by
            #4

            Okay, got it.

            Per this StackExchange answer, and this Qt page, the following is an accepted way to do what I need:

            onSetModule: lblMsg => modFinder.setModule(lblMsg)
            

            Anyway, it works and clears up the warnings.

            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