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. QML not working
Forum Updated to NodeBB v4.3 + New Features

QML not working

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 641 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.
  • M Offline
    M Offline
    MartinD
    wrote on last edited by
    #1

    Why this code works:

    import QtQuick 2.5
    import QtQuick.Window 2.2
    
    Window {
        visible: true
    
        Rectangle {
            anchors.fill: parent
    
            property int a: 300;
    
            MouseArea {
                anchors.fill: parent
                onClicked: {
                  console.log(a) // prints 300
                }
            }
        }
    }
    

    and why this code doesn't work:

    import QtQuick 2.5
    import QtQuick.Window 2.2
    
    Window {
        visible: true
    
        //Rectangle {
         //   anchors.fill: parent
    
            property int a: 300;
    
            MouseArea {
                anchors.fill: parent
                onClicked: {
                  console.log(a) // prints ReferenceError: a is not defined
                }
            }
      //  }
    }
    
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by A Former User
      #2

      Hi!

      In your first example a is a property of the rectangle and the mouse area is a child of the rectangle. Thus the mouse area can access a.

      In your second example a is a property of the window and the mouse area is a child of the window's contentItem. This contentItem is not a child of the window but an attached property of the window. Thus the mouse area cannot access it.

      If you give the window its own id, say mainWindow you can access a everywhere with mainWindow.a.

      See also: http://doc.qt.io/qt-5/qml-qtquick-window-window.html#contentItem-attached-prop

      1 Reply Last reply
      1

      • Login

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