[Qt-qml] Reacting on signals emitted by an Item loaded by a Loader

Cornelius Hald hald at icandy.de
Fri Sep 3 17:14:12 CEST 2010


Hi,

I cannot figure out how I can react on a signal that is emitted by an
Item, which is loaded by a Loader.

For example, I'm having Item1.qml that defines a 'clicked' signal and I
want that if the loaded Item1 is clicked, the rectangle 'rect' gets
green.

Item {
  Loader {
    id: first
    item.onClicked: rect.color = "green" // does not work
  }
  Menu {
    onClicked: first.source = "Item1.qml"
  }
  Rectangle {
    id: rect
    color: "blue" // Should change to green on first.clicked
  }
}


I worked around that by doing something like this:

Item {
  Loader {
    id: first
    function wasClicked() {
      rect.color = "green";
    }
  }
  Menu {
    onClicked: first.source = "Item1.qml"
  }
  Rectangle {
    id: rect
    color: blue
  }
}

And then in Item1.qml I did:
MouseArea {
  onClicked: container.parent.wasClicked()
}

That did work, but I think it that using signals is cleaner. So any
pointers would be really appreciated.

Thanks!
Conny





More information about the Qt-qml mailing list