using Connections with my component
-
Hi all -
I need to send a signal from one of my components. The documentation is brief, but it seems like I'm doing everything necessary:
// Signaler.qml import QtQuick import QtQuick.Controls Rectangle { id: rect signal rectClicked height: 100 width: 100 color: 'lightBlue' MouseArea { id: mouseArea anchors.fill: parent onClicked: { console.log("emitting rectClicked().") rect.rectClicked() } } }
and
// Main.qml import QtQuick import QtQuick.Window import QtQml Window { width: 640 height: 480 visible: true title: qsTr("Hello World") Connections { target: Signaler // is this what's wrong? function onRectClicked() { console.log("signal received.")} } Signaler {} }
No runtime errors. Can someone see why my connection isn't getting the signal?
Thanks...
-
@mzimmers said in using Connections with my component:
My real use of this is a bit more complicated -- the signal is coming from a component that is loaded through a StackView push.
You'll get errors if your Connections object is created before its target even exists.
Also,
id
is not a property so you can't assign it like{ "id": "activitySelector" }
.You could create your Connections object dynamically (at the same time as when you call
push()
). Or, even better, you could do without a Connections at all:Component { id: activitySelectorComp ActivitySelector { onSaveClicked: console.log("ScheduleDetails.qml: received signal from activitySelector!") } } Button { onClicked: scheduleDetailsStackView.push(activitySelectorComp) }
-
-
@johngod I tried that:
import QtQuick import QtQuick.Window import QtQml Window { width: 640 height: 480 visible: true title: qsTr("Hello World") Connections { target: sig function onRectClicked() { console.log("signal received.")} } Signaler {} }
and:
import QtQuick import QtQuick.Controls Rectangle { id: sig signal rectClicked height: 100 width: 100 color: 'lightBlue' MouseArea { id: mouseArea anchors.fill: parent onClicked: { console.log("emitting rectClicked().") sig.rectClicked() } } }
but that gives me a runtime error (actually, two):
qrc:/qt/qml/connections/Main.qml:10:5: QML Connections: Detected function "onRectClicked" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name. qrc:/qt/qml/connections/Main.qml:11: ReferenceError: sig is not defined QML debugging is enabled. Only use this in a safe environment.
-
@mzimmers said in using Connections with my component:
qrc:/qt/qml/connections/Main.qml:11: ReferenceError: sig is not defined
Here is your problem. Main.qml cannot see the ID
sig
. You must add an ID to your object in Main.qml -
@JKSH OK, thank you for that...it works.
My real use of this is a bit more complicated -- the signal is coming from a component that is loaded through a StackView push.
ColumnLayout { Connections { target: activitySelector function onSaveClicked (activity) { console.log("ScheduleDetails.qml: received signal from activitySelector!") } } UtilityButton { id: activitySelectorButton onClicked: { scheduleDetailsDrawer.open() scheduleDetailsStackView.push("ActivitySelector.qml", { "id": "activitySelector" } ) } } Drawer { id: scheduleDetailsDrawer } StackView { id: scheduleDetailsStackView anchors.fill: parent } } }
The signal "saveClicked" comes from within ActivitySelector.qml.
When I try this, I get similar error messages:
qrc:/qt/qml/nga_demo/ScheduleDetails.qml:17:5: QML Connections: Detected function "onSaveClicked" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name. qrc:/qt/qml/nga_demo/ScheduleDetails.qml:18: ReferenceError: activitySelector is not defined qrc:/qt/qml/nga_demo/ScheduleDetails.qml:86: Error: Cannot assign to non-existent property "id"
-
@mzimmers said in using Connections with my component:
My real use of this is a bit more complicated -- the signal is coming from a component that is loaded through a StackView push.
You'll get errors if your Connections object is created before its target even exists.
Also,
id
is not a property so you can't assign it like{ "id": "activitySelector" }
.You could create your Connections object dynamically (at the same time as when you call
push()
). Or, even better, you could do without a Connections at all:Component { id: activitySelectorComp ActivitySelector { onSaveClicked: console.log("ScheduleDetails.qml: received signal from activitySelector!") } } Button { onClicked: scheduleDetailsStackView.push(activitySelectorComp) }
-
M mzimmers has marked this topic as solved on