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. Setting a QML property to undefined via JavaScript
Forum Updated to NodeBB v4.3 + New Features

Setting a QML property to undefined via JavaScript

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 1 Posters 132 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
    bclark
    wrote on 26 Mar 2025, 00:12 last edited by
    #1

    I created a bunch of rectangles dynamically, and I want the user to be able to drag them around, but they should be “locked” when the system is in a particular mode. To do this, I want to toggle between settings in the MouseArea:
    drop.target: undefined
    drop.target: parent

    I have all of my rectangles (referred to as verts) in an array, and I want to be able to iterate through them and set them to either “parent” or “undefined”.
    vert[i].drop.target="undefined"
    I am able to do this with color, by passing it a string.
    vert[i].color=“yellow”

    But it does not like the term “undefined” (I'm not sure if it's ok with "parent" yet or not -- it doesn't error, but I don't know yet if it's doing the right thing). If I try passing them in quotes, as a string, it gives the error:
    TypeError: Value is undefined and could not be converted to an object

    If I remove the quotes, I get the same error. I have also tried:
    drop.target: null
    drop.target: “”

    I even tried the combination of:
    readonly property var notDefined: undefined
    verts[i].drag.target = notDefined;

    Same error.

    I get the feeling I need some sort of object or enum that means “parent” or “undefined”. Something like…
    QtQuick.<EnumClass>.parent or
    QtQuick.<EnumClass>.undefined
    But I can’t find that anywhere. Can somebody point me to it?

    Here's the code that I'm using. I feed it the array and a boolean.

    function setVertsDraggable(verts, draggable) {
         for (var i=0; i<verts.length; i++) {
              if (draggable) {
                   verts[i].drag.target = "parent";
              }
              else {
                   verts[i].drag.target = "undefined";
              }
         }
    }
    
    

    Thanks,
    bc

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bclark
      wrote on 26 Mar 2025, 04:24 last edited by
      #2

      After poking around some more, I think the error message may be referring to the vert[i] object as being undefined, not the value I'm trying to feed it.

      But my vert object works otherwise, and I can change the color on it using similar syntax, so it should be valid. Also, when I do a typeof(vert[i]), I get something along the lines of Vertex_QMLTYPE_3(0x6000003b82a0)
      I get the same thing if I say console.log(vert[i]). That suggests to me that my object is valid.

      The verts are created dynamically, and they are created from the following .qml file, using these commands:
      var component = Qt.createComponent("Vertex.qml");
      var vertex = component.createObject(parent, {x: xPos, y: yPos, width: size, color:"yellow"});

      import QtQuick
      
      Rectangle {
          width: 8
          height: 8
          color: yellow
          property string tag: ""
      
          MouseArea {
              anchors.fill: parent
              drag.target: undefined
              drag.smoothed: false
              onReleased: {
                  polyCanvas.requestPaint()
              }
          }
      }
      

      Please let me know if you've got something...
      bc

      1 Reply Last reply
      0

      1/2

      26 Mar 2025, 00:12

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved