Results 1 to 3 of 3

Thread: QML / JS value/reference problem

  1. #1
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Question QML / JS value/reference problem

    Hi,

    i am trying to implement a menu with several sub-menus. Therefore when changing to a sub-menu, I want to push the current color information into an array / var property, like I do with the actions themselves. The problem: I cannot copy the value of a color. It's always the reference that is copied, so the whole storing process becomes pointless, the stored color changes as soon as I assign the new color.

    the QML properties are:
    Qt Code:
    1. property alias color: actionBarMainRect.color;
    2. property var colorsUpperLevel: []
    To copy to clipboard, switch view to plain text mode 

    In JS I use this code
    Qt Code:
    1. var color = actionBarLeft.color
    2.  
    3. actionBarLeft.colorsUpperLevel.push(color)
    4.  
    5. console.log("pushed color1: "+ actionBarLeft.colorsUpperLevel[actionBarLeft.colorsUpperLevel.length-1])
    6. console.log("actionBarLeft.color1" + actionBarLeft.color)
    7. console.log("menuColor1: "+menuColor)
    8. console.log("color1: "+color)
    9.  
    10. actionBarLeft.color = menuColor
    11.  
    12. console.log("pushed color2: "+ actionBarLeft.colorsUpperLevel[actionBarLeft.colorsUpperLevel.length-1])
    13. console.log("actionBarLeft.color2" + actionBarLeft.color)
    14. console.log("menuColor2: "+menuColor)
    15. console.log("color2: "+color)
    To copy to clipboard, switch view to plain text mode 

    with this result:
    qml: pushed color1: #008400 // old color, stored correctly
    qml: actionBarLeft.color1#008400 // still old
    qml: menuColor1: #8400ff // the new color
    qml: color1: #008400 // everything ok
    //---> Now the assignment happens
    qml: pushed color2: #8400ff // ok, we wanted to change this.
    qml: actionBarLeft.color2#8400ff // but not this!!
    qml: menuColor2: #8400ff
    qml: color2: #8400ff // and not this!!
    Any ideas?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QML / JS value/reference problem

    Just to be sure, you have tried using a variable name that is not the same as the property, right?

    Maybe try creating a new color, e.g. using Qt.lighter() with factor 1.0

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    sedi (8th March 2016)

  4. #3
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Thumbs up Re: QML / JS value/reference problem

    Actually, I hadn't tried that before, I'm afraid - but for sure I have by now. That didn't help.

    But... fortunately
    var myColor = Qt.lighter(actionBarLeft.color,1.0)
    Does help indeed! Cool approach.
    Thank you for this birthday present :-)

    Cheeeers!!

Similar Threads

  1. Undefined reference problem
    By deepa in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2011, 12:48
  2. Replies: 2
    Last Post: 29th June 2010, 19:36
  3. problem with pointer-reference
    By mickey in forum General Programming
    Replies: 10
    Last Post: 24th February 2007, 22:59
  4. reference problem
    By high_flyer in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2006, 13:13
  5. Problem: Undefined reference
    By stiank81 in forum Qt Programming
    Replies: 4
    Last Post: 16th February 2006, 15:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.