Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Need help! Very weird qml issue - property binding doesn't work! [solved]
Forum Updated to NodeBB v4.3 + New Features

Need help! Very weird qml issue - property binding doesn't work! [solved]

Scheduled Pinned Locked Moved Mobile and Embedded
qmlqtquickbinding
3 Posts 2 Posters 3.0k 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.
  • F Offline
    F Offline
    flashmozzg
    wrote on last edited by flashmozzg
    #1

    It's very weird because I use almost the same pattern in another qml file and there it works correctly! I'm sure I'm not reassigning value anywhere!
    I have something like

    Item {
    property bool isExpanded: false
    MouseArea {
                anchors.fill: parent
                onClicked: {
                    isExpanded = !isExpanded
                    console.log(isExpanded)
                    console.log(myId.visible)
                }
    }
    MyCustomItem {
            id: myId
            visible: isExpanded
    // other stuff
    }
    }
    

    IsExpanded changes after I click but item visibility always stays the same!
    And I have many other properties for my item (for example height: isExpanded ? someval : 0)
    which doesn't change too! It sort of works If i always change everything manually but whats the point then? And in another .qml I use similar pattern and there it works!
    UPDATE: If I put, for example on double click/another button:

    myId.visible = Qt.binding(function() {return isExpanded})
    

    It works as it should after that! So, for some unknown reason it doesn't 'bind' them when in should in regular property declaration (visible: isExpanded). I had to very explicitly tell qt to bind property to make it work. WTF?

    1 Reply Last reply
    1
    • F Offline
      F Offline
      flashmozzg
      wrote on last edited by
      #2

      I found the issue! Very unobvious (haven't found documented anywhere) and it fails silently.
      The problem is that MyCustomItem contained property with the same name in itself (property bool isExpanded) so here:

      MyCustomItem {
          id: myId
         visible: isExpanded
       // other stuff
      }
      

      isExpanded was from MyCustomItem. But without any indication of that. Well, now I know that this could happen and would probably spot it if it happened again but this is really counter-intuitive. What if it wasn't my item but someone else's and I didn't know that it had such property? qt creator could've at least warned about possible ambiguities.

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

        Hi,

        @flashmozzg said:

        The problem is that MyCustomItem contained property with the same name in itself

        This is called variable shadowing.

        this is really counter-intuitive. What if it wasn't my item but someone else's and I didn't know that it had such property?

        Idieally, that someone else would document all the properties of the component, and you would read the documentation before using their component.

        But to avoid ambiguities, I suggest always specifying the owner whenever you refer to a property that isn't part of the current component, e.g.

        MyCustomItem {
            id: myId
            visible: parent.isExpanded
            // other stuff
        }
        

        qt creator could've at least warned about possible ambiguities.

        That could be a useful feature in Debug mode. You can submit a feature request to https://bugreports.qt.io/ if you wish.

        I don't want this to be enabled in Release mode though, because it would degrade the performance of the QML engine.

        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