Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture a signal in QML?

Tags:

qt

qml

How I can send s signal from one qml component to another?

Below is an example:

Rectangle {
    id: main
    width: 360; height: 360
    signal clicked()

    Text {
        id: testStr
        anchors.centerIn: parent
        text: "Hello World"
    }
    MouseArea {
        id: mouseArea
        anchors.fill: parent
        onClicked: { Qt.quit(); }
    }

    Component.onCompleted: clicked()
    onClicked:  testStr.text = "Demo"
}

How do I capture the signal in other Component?

like image 519
Mr.Tu Avatar asked Feb 29 '12 01:02

Mr.Tu


2 Answers

You should use connect() method of component's signals (signals themselves are objects).

function clickHandler() {
    console.log('main clicked')
}
Component.onCompleted: {
    main.clicked.connect(clickHandler)
}

See http://developer.qt.nokia.com/doc/qt-4.8/qmlevents.html

like image 115
sergk Avatar answered Sep 24 '22 17:09

sergk


In the other object, you simply add a on word followed by the signal name. EG:

Rectangle {
  YourQmlObject {
    onClicked: { ... }
  }
}

(clicked is somewhat a confusing signal name because it's common. But if you had called your signal orange, you'd make the binding onOrange:)

like image 40
Wes Hardaker Avatar answered Sep 23 '22 17:09

Wes Hardaker