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. Invalid write to global property
QtWS25 Last Chance

Invalid write to global property

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 3 Posters 12.0k Views
  • 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
    brut.sh
    wrote on last edited by
    #1

    Hi all! Could u help me with my little proble? I'm begginer in qt/qml. And I'd like to change some properties of qml component from anonter qml file, and I've decided to use JS like this:
    @.pragma library
    sc = String;
    function setSc(ss) {
    sc = ss;
    }

    function getSc () {
    return sc;
    }@
    Try to use it:
    @import "setInfo.js" as Scr
    ....
    onClicked: Scr.setSc("#777777")
    .....
    color: Scr.getSc()@
    But it doesn't work
    @qrc:/setInfo.js:8: ReferenceError: sc is not defined
    qrc:/setInfo.js:4: Error: Invalid write to global property "sc"@
    Can anyone help me? Will be grateful for u'r any solutions.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi and Welcome to Qt DevNet.

      Try declaring sc as var.

      157

      1 Reply Last reply
      1
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        I think you forgot to add var keyword before your sc variable name:
        @
        var sc = String;
        @

        (Z(:^

        1 Reply Last reply
        0
        • B Offline
          B Offline
          brut.sh
          wrote on last edited by
          #4

          @var sc;
          .......
          qrc:/Info.qml:6:12: Unable to assign [undefined] to QColor
          qrc:/main.qml:52:19: Unable to assign [undefined] to QString@
          And: @var sc = String;
          ......
          qrc:/Info.qml:6:12: Unable to assign a function to a property of any type other than var.
          qrc:/main.qml:52:19: Unable to assign a function to a property of any type other than var.@

          main.qml:52 @text: Scr.getSc()@
          Info.qml:6 @color: Scr.getSc()@

          1 Reply Last reply
          0
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            That is because it is not able to understand what String object is.
            Assingn some string instead,
            @
            var sc = "green";
            @

            157

            1 Reply Last reply
            0
            • B Offline
              B Offline
              brut.sh
              wrote on last edited by
              #6

              It works, thank u. But I can't write property by this way:
              @onClicked: Scr.setSc("red")@
              And it have not any errors using this.
              Have any idea?

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                It does write. Try this
                @
                onClicked: {
                Scr.setSc("red")
                rect.color = Scr.getSc() //assuming rect is id of that element
                }
                @

                157

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  brut.sh
                  wrote on last edited by
                  #8

                  In my case, "rect" is located in another qml file, and I can not call to its property by this way. It seems, that setSc method does not work right.

                  1 Reply Last reply
                  0
                  • p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

                    bq. It seems, that setSc method does not work right.

                    You can try printing the value in setSc function to check if it works.
                    @
                    function setSc(ss) {
                    sc = ss
                    console.log(sc)
                    }
                    @

                    157

                    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