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. bug in a JS function
Forum Updated to NodeBB v4.3 + New Features

bug in a JS function

Scheduled Pinned Locked Moved Solved QML and Qt Quick
17 Posts 2 Posters 1.3k Views 2 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.
  • mzimmersM mzimmers

    @JKSH thanks for the suggestion. Unfortunately, I'm still having the same problem, except now it's equipmentEdit (see below) that's undefined.

    // EquipmentConfigEdit.qml
    ColumnLayout {
        id: equipmentEdit
        property var outcome: outcomeComponent.createObject(equipmentEdit) as Outcome
    
        Component {
            id: outcomeComponent
            Outcome {
                id: newOutcome
            }
        }
    

    Used here (I've removed most of the logs):

    function replaceValve(b, i) {
        if (b === 2) { // and it is 2
            var pumpUuid = equipmentEdit.equipmentCopy.uuid
            var assignedValves = equipmentEdit.outcome.valveConfigurationList
            var indexToReplace = assignedValves.indexOf(modelData)
            var newValveUuid = unassignedValves[i]
            assignedValves[indexToReplace] = newValveUuid
            assignedValves.splice(indexToReplace, 1, newValveUuid)
    
     // this line causes the trouble.
            equipmentEdit.outcome.valveConfigurationList = assignedValves
            
            console.log("EquipmentSelectValves.qml.replaceValve(): equipmentEdit.outcome.valveConfigurationList is " + equipmentEdit.outcome.valveConfigurationList)
    

    All my logs suggest that everything is working...up to my assignment of the valve configuration list. I should point out that it isn't just outcome (or equipmentEdit) that is now undefined; it appears to be anything that comes after that statement.

    JKSHJ Offline
    JKSHJ Offline
    JKSH
    Moderators
    wrote on last edited by
    #7

    @mzimmers It's not clear from your snippets:

    • Where is replaceValve() defined? Is it inside EquipmentConfigEdit.qml?
    • Where is `unassignedValves defined? What does it contain?
    • What is the definition of ValveConfigurationList?

    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

    mzimmersM 1 Reply Last reply
    0
    • JKSHJ JKSH

      @mzimmers It's not clear from your snippets:

      • Where is replaceValve() defined? Is it inside EquipmentConfigEdit.qml?
      • Where is `unassignedValves defined? What does it contain?
      • What is the definition of ValveConfigurationList?
      mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #8

      @JKSH said in bug in a JS function:

      @mzimmers It's not clear from your snippets:

      • Where is replaceValve() defined? Is it inside EquipmentConfigEdit.qml?

      No, it's in another QML file. EquipmentConfigEdit.qml has a DelegateChoice that uses VspEdit.qml, which uses another file with the above script.

      • Where is `unassignedValves defined? What does it contain?

      In EquipmentConfigEdit.qml (I forgot to qualify that one; now fixed). Here's the definition:

      property var unassignedValves: outcomeModel.unassignedValves() 
      
      • What is the definition of ValveConfigurationList?
      #define ValveConfigurationList QList<QUuid>
      class Outcome: public QObject
      {
          Q_OBJECT
          QML_ELEMENT
          Q_PROPERTY(ValveConfigurationList valveConfigurationList READ valveConfigurationList WRITE setValveConfigurationList NOTIFY valveConfigurationListChanged FINAL)
          ValveConfigurationList valveConfigurationList() { return m_valveConfigurationList; }
          bool setValveConfigurationList(const ValveConfigurationList &list);
      
      JKSHJ 1 Reply Last reply
      0
      • mzimmersM mzimmers

        @JKSH said in bug in a JS function:

        @mzimmers It's not clear from your snippets:

        • Where is replaceValve() defined? Is it inside EquipmentConfigEdit.qml?

        No, it's in another QML file. EquipmentConfigEdit.qml has a DelegateChoice that uses VspEdit.qml, which uses another file with the above script.

        • Where is `unassignedValves defined? What does it contain?

        In EquipmentConfigEdit.qml (I forgot to qualify that one; now fixed). Here's the definition:

        property var unassignedValves: outcomeModel.unassignedValves() 
        
        • What is the definition of ValveConfigurationList?
        #define ValveConfigurationList QList<QUuid>
        class Outcome: public QObject
        {
            Q_OBJECT
            QML_ELEMENT
            Q_PROPERTY(ValveConfigurationList valveConfigurationList READ valveConfigurationList WRITE setValveConfigurationList NOTIFY valveConfigurationListChanged FINAL)
            ValveConfigurationList valveConfigurationList() { return m_valveConfigurationList; }
            bool setValveConfigurationList(const ValveConfigurationList &list);
        
        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #9

        @mzimmers said in bug in a JS function:

        #define ValveConfigurationList QList<QUuid>
        class Outcome: public QObject
        {
            Q_OBJECT
            QML_ELEMENT
            Q_PROPERTY(ValveConfigurationList valveConfigurationList READ valveConfigurationList WRITE setValveConfigurationList NOTIFY valveConfigurationListChanged FINAL)
            ValveConfigurationList valveConfigurationList() { return m_valveConfigurationList; }
            bool setValveConfigurationList(const ValveConfigurationList &list);
        

        Hmm... I'm not sure if the #define will play nicely with Qt's property system. https://doc.qt.io/qt-6/qtqml-cppintegration-exposecppattributes.html#exposing-properties says "Do not use typedef or using for Q_PROPERTY types as these will confuse moc). I'd imagine that the same goes for #define.

        As a sanity check, put qDebug() << list; inside setValveConfigurationList(). Is your C++ function receiving the expected list?

        Try replacing ValveConfigurationList with QList<QUuid> throughout your header. Does that make a difference? (If not, your best bet for quick troubleshooting is to provide a minimal, compilable example that triggers the issue)

        • Where is replaceValve() defined? Is it inside EquipmentConfigEdit.qml?

        No, it's in another QML file. EquipmentConfigEdit.qml has a DelegateChoice that uses VspEdit.qml, which uses another file with the above script.

        OK. And does VspEdit contain required property EquipmentConfigEdit equipmentEdit or similar? (Remember, use EquipmentConfigEdit instead of var)

        • Where is `unassignedValves defined? What does it contain?

        In EquipmentConfigEdit.qml (I forgot to qualify that one; now fixed). Here's the definition:

        property var unassignedValves: outcomeModel.unassignedValves() 
        

        I don't think this is related to your issue, but do you need a separate property called EquipmentConfigEdit.unassignedValves? Could you just do let newValveUuid = equipmentEdit.outcomeModel.unassignedValves[i] or similar?

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        mzimmersM 1 Reply Last reply
        0
        • JKSHJ JKSH

          @mzimmers said in bug in a JS function:

          #define ValveConfigurationList QList<QUuid>
          class Outcome: public QObject
          {
              Q_OBJECT
              QML_ELEMENT
              Q_PROPERTY(ValveConfigurationList valveConfigurationList READ valveConfigurationList WRITE setValveConfigurationList NOTIFY valveConfigurationListChanged FINAL)
              ValveConfigurationList valveConfigurationList() { return m_valveConfigurationList; }
              bool setValveConfigurationList(const ValveConfigurationList &list);
          

          Hmm... I'm not sure if the #define will play nicely with Qt's property system. https://doc.qt.io/qt-6/qtqml-cppintegration-exposecppattributes.html#exposing-properties says "Do not use typedef or using for Q_PROPERTY types as these will confuse moc). I'd imagine that the same goes for #define.

          As a sanity check, put qDebug() << list; inside setValveConfigurationList(). Is your C++ function receiving the expected list?

          Try replacing ValveConfigurationList with QList<QUuid> throughout your header. Does that make a difference? (If not, your best bet for quick troubleshooting is to provide a minimal, compilable example that triggers the issue)

          • Where is replaceValve() defined? Is it inside EquipmentConfigEdit.qml?

          No, it's in another QML file. EquipmentConfigEdit.qml has a DelegateChoice that uses VspEdit.qml, which uses another file with the above script.

          OK. And does VspEdit contain required property EquipmentConfigEdit equipmentEdit or similar? (Remember, use EquipmentConfigEdit instead of var)

          • Where is `unassignedValves defined? What does it contain?

          In EquipmentConfigEdit.qml (I forgot to qualify that one; now fixed). Here's the definition:

          property var unassignedValves: outcomeModel.unassignedValves() 
          

          I don't think this is related to your issue, but do you need a separate property called EquipmentConfigEdit.unassignedValves? Could you just do let newValveUuid = equipmentEdit.outcomeModel.unassignedValves[i] or similar?

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #10

          @JKSH said in bug in a JS function:

          Hmm... I'm not sure if the #define will play nicely with Qt's property system. https://doc.qt.io/qt-6/qtqml-cppintegration-exposecppattributes.html#exposing-properties says "Do not use typedef or using for Q_PROPERTY types as these will confuse moc). I'd imagine that the same goes for #define.
          As a sanity check, put qDebug() << list; inside setValveConfigurationList(). Is your C++ function receiving the expected list?

          I stepped into it in the debugger; it seems to be getting the correct list.

          Try replacing ValveConfigurationList with QList<QUuid> throughout your header. Does that make a difference? (If not, your best bet for quick troubleshooting is to provide a minimal, compilable example that triggers the issue)

          No, it doesn't.

          Where is replaceValve() defined? Is it inside EquipmentConfigEdit.qml?

          No, it's in another QML file. EquipmentConfigEdit.qml has a DelegateChoice that uses VspEdit.qml, which uses another file with the above script.

          OK. And does VspEdit contain required property EquipmentConfigEdit equipmentEdit or similar? (Remember, use EquipmentConfigEdit instead of var)

          No. VspEdit is used within EquipmentConfigEdit.qml as folllows:

          ListView {
              model: equipmentCopy
              delegate: DelegateChooser {
                  role: "category"
                  DelegateChoice {
                      roleValue: NgaUI.CATEGORY_VSP
                      delegate: VspEdit {
                          equipment: equipmentEdit.equipment
                          equipmentCopy: equipmentEdit.equipmentCopy
          

          equipmentEdit is the id assigned to EquipmentConfigEdit.qml.

          Where is `unassignedValves defined? What does it contain?
          Also in EquipmentConfigEdit.qml:

          property var unassignedValves: outcomeModel.unassignedValves()
          

          I don't think this is related to your issue, but do you need a separate property called EquipmentConfigEdit.unassignedValves? Could you just do let newValveUuid = equipmentEdit.outcomeModel.unassignedValves[i] or similar?

          I did this (no change in results):

          var newValveUuid = outcomeModel.unassignedValves()[i]
          

          I will work on a minimal example. Thanks for the help.

          mzimmersM 1 Reply Last reply
          0
          • mzimmersM mzimmers

            @JKSH said in bug in a JS function:

            Hmm... I'm not sure if the #define will play nicely with Qt's property system. https://doc.qt.io/qt-6/qtqml-cppintegration-exposecppattributes.html#exposing-properties says "Do not use typedef or using for Q_PROPERTY types as these will confuse moc). I'd imagine that the same goes for #define.
            As a sanity check, put qDebug() << list; inside setValveConfigurationList(). Is your C++ function receiving the expected list?

            I stepped into it in the debugger; it seems to be getting the correct list.

            Try replacing ValveConfigurationList with QList<QUuid> throughout your header. Does that make a difference? (If not, your best bet for quick troubleshooting is to provide a minimal, compilable example that triggers the issue)

            No, it doesn't.

            Where is replaceValve() defined? Is it inside EquipmentConfigEdit.qml?

            No, it's in another QML file. EquipmentConfigEdit.qml has a DelegateChoice that uses VspEdit.qml, which uses another file with the above script.

            OK. And does VspEdit contain required property EquipmentConfigEdit equipmentEdit or similar? (Remember, use EquipmentConfigEdit instead of var)

            No. VspEdit is used within EquipmentConfigEdit.qml as folllows:

            ListView {
                model: equipmentCopy
                delegate: DelegateChooser {
                    role: "category"
                    DelegateChoice {
                        roleValue: NgaUI.CATEGORY_VSP
                        delegate: VspEdit {
                            equipment: equipmentEdit.equipment
                            equipmentCopy: equipmentEdit.equipmentCopy
            

            equipmentEdit is the id assigned to EquipmentConfigEdit.qml.

            Where is `unassignedValves defined? What does it contain?
            Also in EquipmentConfigEdit.qml:

            property var unassignedValves: outcomeModel.unassignedValves()
            

            I don't think this is related to your issue, but do you need a separate property called EquipmentConfigEdit.unassignedValves? Could you just do let newValveUuid = equipmentEdit.outcomeModel.unassignedValves[i] or similar?

            I did this (no change in results):

            var newValveUuid = outcomeModel.unassignedValves()[i]
            

            I will work on a minimal example. Thanks for the help.

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #11

            First attempt at a minimal example produced a working script (crud).

            I'm 99% sure that I'm successfully accessing the variables created in the file EquipmentConfigEdit.qml. Now, with the fully qualified variable names, I'm getting the same error, but on "equipmentEdit."

            assignedValves[indexToReplace] = newValveUuid
            console.log("EquipmentSelectValves.qml.replaceValve(): assignedValves (after) is " + assignedValves)
            
            equipmentEdit.outcome.valveConfigurationList = assignedValves
            // line 197 below.
            console.log("EquipmentSelectValves.qml.replaceValve(): equipmentEdit.outcome.valveConfigurationList is " + equipmentEdit.outcome.valveConfigurationList)
            

            produces this error:

            qrc:/qt/qml/NgaIcdFw/qml/equipment/EquipmentSelectValves.qml:197: ReferenceError: equipmentEdit is not defined
            

            if I comment out that log, the next variable I hit is said to be not defined.

            It's really like the call to setValveConfigurationList() is somehow clobbering something in my script.

            mzimmersM 1 Reply Last reply
            0
            • mzimmersM mzimmers

              First attempt at a minimal example produced a working script (crud).

              I'm 99% sure that I'm successfully accessing the variables created in the file EquipmentConfigEdit.qml. Now, with the fully qualified variable names, I'm getting the same error, but on "equipmentEdit."

              assignedValves[indexToReplace] = newValveUuid
              console.log("EquipmentSelectValves.qml.replaceValve(): assignedValves (after) is " + assignedValves)
              
              equipmentEdit.outcome.valveConfigurationList = assignedValves
              // line 197 below.
              console.log("EquipmentSelectValves.qml.replaceValve(): equipmentEdit.outcome.valveConfigurationList is " + equipmentEdit.outcome.valveConfigurationList)
              

              produces this error:

              qrc:/qt/qml/NgaIcdFw/qml/equipment/EquipmentSelectValves.qml:197: ReferenceError: equipmentEdit is not defined
              

              if I comment out that log, the next variable I hit is said to be not defined.

              It's really like the call to setValveConfigurationList() is somehow clobbering something in my script.

              mzimmersM Offline
              mzimmersM Offline
              mzimmers
              wrote on last edited by
              #12

              @JKSH I have a minimal example now. I've zipped it up. Where would you like me to put it?

              JKSHJ 1 Reply Last reply
              0
              • mzimmersM mzimmers

                @JKSH I have a minimal example now. I've zipped it up. Where would you like me to put it?

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by
                #13

                @mzimmers said in bug in a JS function:

                @JKSH I have a minimal example now. I've zipped it up. Where would you like me to put it?

                Any file sharing service of your choice should be fine (Dropbox, Google Drive, Microsoft OneDrive, etc.) Or you could push it to a public Git host (e.g. GitHub, Atlassian Bitbucket, etc.)

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                mzimmersM 1 Reply Last reply
                0
                • JKSHJ JKSH

                  @mzimmers said in bug in a JS function:

                  @JKSH I have a minimal example now. I've zipped it up. Where would you like me to put it?

                  Any file sharing service of your choice should be fine (Dropbox, Google Drive, Microsoft OneDrive, etc.) Or you could push it to a public Git host (e.g. GitHub, Atlassian Bitbucket, etc.)

                  mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #14

                  @JKSH Here it is:

                  my zip zile

                  If you can figure out what's going on with this, I'll buy you a seafood dinner the next time you're in Monterey.

                  1 Reply Last reply
                  0
                  • JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #15

                    Looks like you've found an interesting bug related to Components accessing external IDs.

                    Add pragma ComponentBehavior: Bound to the top of EquipmentSelectValves.qml (see https://doc.qt.io/qt-6/qtqml-documents-structure.html#componentbehavior ). This makes the QML engine a bit stricter though, so you need to also mark your delegate's modelData and index properties as required property (see the examples at https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#models ). This is not necessarily a bad thing, as it enforces good practice.

                    Note: After this, your example will no longer complain that outcome is not defined, but it will complain about equipmentModel because that is truly not defined anywhere in your example.

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    mzimmersM 1 Reply Last reply
                    1
                    • JKSHJ JKSH

                      Looks like you've found an interesting bug related to Components accessing external IDs.

                      Add pragma ComponentBehavior: Bound to the top of EquipmentSelectValves.qml (see https://doc.qt.io/qt-6/qtqml-documents-structure.html#componentbehavior ). This makes the QML engine a bit stricter though, so you need to also mark your delegate's modelData and index properties as required property (see the examples at https://doc.qt.io/qt-6/qtquick-modelviewsdata-modelview.html#models ). This is not necessarily a bad thing, as it enforces good practice.

                      Note: After this, your example will no longer complain that outcome is not defined, but it will complain about equipmentModel because that is truly not defined anywhere in your example.

                      mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by mzimmers
                      #16

                      @JKSH said in bug in a JS function:

                      Looks like you've found an interesting bug related to Components accessing external IDs.

                      I love when things aren't (entirely) my fault.

                      Add pragma ComponentBehavior: Bound to the top of EquipmentSelectValves.qml
                      you need to also mark your delegate's modelData and index properties as required property

                      Done and done.

                      it will complain about equipmentModel because that is truly not defined anywhere in your example.

                      Yeah, I forgot to remove that part (because it bombed before it got there). Shouldn't be a problem in my real app, where I have this line in main.cpp:

                          engine.rootContext()->setContextProperty("equipmentModel", equipmentModel);
                      

                      Let me know when you're in town, and I'll buy you the best dinner Monterey can offer (which is pretty damn good, IMO).

                      Should I report this bug, and if so, how should I characterize it?

                      1 Reply Last reply
                      0
                      • mzimmersM mzimmers has marked this topic as solved on
                      • JKSHJ Offline
                        JKSHJ Offline
                        JKSH
                        Moderators
                        wrote on last edited by JKSH
                        #17

                        Actually, I don't think it's a bug after all. Basically:

                        1. Your replaceValve() function is a "member" of your delegate. (More precisely, replaceValve() exists within the context of your valveBox delegate, which is separate from the context of EquipmentSelectValves by default)
                        2. That function modifies the ListView's model.
                        3. Modifying the model caused your delegate (and its context) to be destroyed.
                        4. Destroying the delegate's context invalidates the references inside replaceValve().

                        Here is a minimal reproducer for your issue:

                        //pragma ComponentBehavior: Bound
                        import QtQuick
                        import QtQuick.Controls.Basic
                        
                        Window {
                            width: 800
                            height: 600
                            visible: true
                        
                            ListView {
                                id: lv
                                anchors.fill: parent
                                model: 1
                                delegate: Button {
                                    text: "Click me"
                        
                                    onClicked: {
                                        console.log("parent: " + parent)
                                        console.log("lv: " + lv)
                                        lv.model = 0
                                        console.log("parent: " + parent)
                                        console.log("lv: " + lv)
                                    }
                        
                                    Component.onDestruction: console.log("Destroyed", this)
                                }
                            }
                        }
                        
                        

                        Output:

                        qml: parent: QQuickItem(0x1257086c4a0)
                        qml: lv: QQuickListView(0x12570653350)
                        qml: Destroyed Button_QMLTYPE_1(0x12570653b30)
                        qml: parent: null
                        Main.qml:22: ReferenceError: lv is not defined
                        

                        pragma ComponentBehavior: Bound changes QML's behaviour and puts replaceValve() inside the same context as EquipmentSelectValves, so you get the outcome that you expect. Qt can't make
                        this the default yet because it will break old user code. But https://doc.qt.io/qt-6/qtqml-documents-structure.html#componentbehavior does say "The default value of ComponentBehavior is Unbound.... In a future version of Qt the default will change to Bound."

                        I haven't visited Monterey before, but I'll give you a buzz if I'm ever in town. That seafood sounds tempting ;-)

                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                        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