I can't get the signal to work in QML
-
Hi.
I want to use signal in QML.
I did it this way, but I can't do.main.qml
Window { width: 640 height: 480 visible: true title: qsTr("Hello World") MyRect{ anchors.centerIn: parent height: 50 width: 50 color: "red" border.width: 1 border.color: "gray" } Connections{ target: MyRectForm function testSig(){ console.log("get") } } }
MyRectForm.qml
MyRectForm{ signal testSig Connections{ target: rectMouseArea function onClicked(){ console.log("send") testSig() } } }
The content of MyRect.ui.qml is a very simple rectangle.
I get log "send", when I click this rectangle. But I don't get "get".I thought it might be possible, so I tried changing "function testSig()" to "function onTestSig()", but I got an error.
It error string is
[QML Connections: Detected function "onTestSig" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name.]How can I get the signal to work?
-
@eyllanesc
Sorry, It in MyRect.qmlRectangle{ id: systemRect property alias rectMouseArea: rectMouseArea MouseArea{ id: rectMouseArea anchors.fill: parent } }
@w-tkm MyRectForm makes no sense in main.qml, target must have the id of the item that has the signal:
import QtQuick 2.15 import QtQuick.Window 2.15 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") MyRect{ id: myRect anchors.centerIn: parent height: 50 width: 50 color: "red" border.width: 1 border.color: "gray" } Connections{ target: myRect function onTestSig(){ console.log("get") } } }
-
Hi.
I want to use signal in QML.
I did it this way, but I can't do.main.qml
Window { width: 640 height: 480 visible: true title: qsTr("Hello World") MyRect{ anchors.centerIn: parent height: 50 width: 50 color: "red" border.width: 1 border.color: "gray" } Connections{ target: MyRectForm function testSig(){ console.log("get") } } }
MyRectForm.qml
MyRectForm{ signal testSig Connections{ target: rectMouseArea function onClicked(){ console.log("send") testSig() } } }
The content of MyRect.ui.qml is a very simple rectangle.
I get log "send", when I click this rectangle. But I don't get "get".I thought it might be possible, so I tried changing "function testSig()" to "function onTestSig()", but I got an error.
It error string is
[QML Connections: Detected function "onTestSig" in Connections element. This is probably intended to be a signal handler but no signal of the target matches the name.]How can I get the signal to work?
-
@eyllanesc
Sorry, It in MyRect.qmlRectangle{ id: systemRect property alias rectMouseArea: rectMouseArea MouseArea{ id: rectMouseArea anchors.fill: parent } }
-
@eyllanesc
Sorry, It in MyRect.qmlRectangle{ id: systemRect property alias rectMouseArea: rectMouseArea MouseArea{ id: rectMouseArea anchors.fill: parent } }
@w-tkm MyRectForm makes no sense in main.qml, target must have the id of the item that has the signal:
import QtQuick 2.15 import QtQuick.Window 2.15 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") MyRect{ id: myRect anchors.centerIn: parent height: 50 width: 50 color: "red" border.width: 1 border.color: "gray" } Connections{ target: myRect function onTestSig(){ console.log("get") } } }
-
@w-tkm MyRectForm makes no sense in main.qml, target must have the id of the item that has the signal:
import QtQuick 2.15 import QtQuick.Window 2.15 Window { width: 640 height: 480 visible: true title: qsTr("Hello World") MyRect{ id: myRect anchors.centerIn: parent height: 50 width: 50 color: "red" border.width: 1 border.color: "gray" } Connections{ target: myRect function onTestSig(){ console.log("get") } } }
@eyllanesc
Thanks,I have one more question.Row{ anchors.centerIn: parent Repeater{ id: rectSet model: 3 MyRect{ width: 50 height: 50 color: "red" border.width: 1 border.color: "gray" } } }
What should I do if I use a Repeater in this way?
-
@eyllanesc
Thanks,I have one more question.Row{ anchors.centerIn: parent Repeater{ id: rectSet model: 3 MyRect{ width: 50 height: 50 color: "red" border.width: 1 border.color: "gray" } } }
What should I do if I use a Repeater in this way?