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. [SOLVED] Dynamical aliases or equivalent of C pointers in QML
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Dynamical aliases or equivalent of C pointers in QML

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 2.2k 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.
  • R Offline
    R Offline
    rominf
    wrote on last edited by
    #1

    Hi.
    I need to do a computer graphics lab: drawing ellipses and circles with a different algorithms. My teacher requires different algorithms for circles and ellipses (of course, I can draw circle as an ellipse, but the teacher is not satisfied with this approach). So I decided to place properties "circle" and "ellipse" for active figure into root object:
    @
    property var circle: ({ x: 256, y: 256, r: 64 })
    property var ellipse: ({ x: 256, y: 256, width: 64, height: 128 })
    @
    Of course circle and ellipse have common properties (x, y) and common functions in which they participate (drawWithQtDefaultFunction, drawWithBresenhamMethod, ...). But they are not completely identical, so I cannot just write "ellipse" everywhere. I think it is logical to have pointer (alias, reference) to active figure (figure -> circle or figure -> ellipse) to avoid plenty of code like this:
    @
    TextField
    {
    id: edtX
    text: (isCircle? circle : ellipse).x
    validator: DoubleValidator { }
    onTextChanged: (isCircle? circle : ellipse).x = parseFloat(text)
    }
    @
    It can be simplified a little bit with additional function:
    @
    function activeFigure()
    {
    return isCircle? circle : ellipse;
    }
    @
    but I don't like code appearance (parentheses confuses me):
    @
    text: activeFigure().x
    @

    I've tried to use property aliases but (as I understood):

    They can be applied only to QML objects (not to property var (i.e. JavaScript object)). So I have to have wrapper like:

    @
    QtObject
    {
    id: circle
    property var obj: ({ x: 256, y: 256, r: 64 })
    }
    @
    that isn't convenient.

    Even with wrappers I cannot change aliases. I got:

    @QML PropertyChanges: Cannot assign to read-only property "b"@
    here is simple code for test:
    @
    import QtQuick 2.0

    Rectangle
    {
    id: obj_root

    QtObject
    {
        id: obj_x
        property string str: "x"
    }
    
    QtObject
    {
        id: obj_y
        property string str: "y"
    }
    
    property alias b: obj_x
    
    states:
    [
        State 
        {
            name: "state_x"
            PropertyChanges 
            {
                target: obj_root
                b: obj_x
            }
        },
        State
        {
            name: "state_y"
            PropertyChanges
            {
                target: obj_root
                b: obj_y
            }
        }
    ]
    state: "state_x"
    Text
    {
        text: b.str
        anchors.centerIn: parent
    }
    MouseArea
    {
        anchors.fill: parent
        onClicked:
        {
            parent.state = parent.state == "state_x"? "state_y" : "state_x"
        }
    }
    

    }
    @

    So I decided to use function with condition, but I don't really like it.

    Are there better options?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rominf
      wrote on last edited by
      #2

      OK, got it. I forgot that JavaScript assignment is not a data, but a reference copy (I'm a complete novice in JavaScript, only 3 days). So I just needed to make assignment on checkbox checked:
      @
      property var circle: ({ x: 256, y: 256, r: 64 })
      property var ellipse: ({ x: 256, y: 256, width: 64, height: 128 })
      property var figure: ellipse
      ...
      CheckBox
      {
      id: chkCircle
      text: "Circle"
      checked: false
      onCheckedChanged: figure = checked? circle : ellipse;
      }
      ...
      figure.x = parseFloat(text);
      @

      This concrete problem is solved, but I still need dynamic aliases and full bindings between QML objects and JavaScript objects.

      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