Use a variable in another file
-
wrote on 27 Apr 2017, 13:42 last edited by
Hello,
I have 2 files:
-Button.qml
-Other.qmlI have 2 variable in button.qml (clikedX and clikedY)and I would like to retrieve the value of these variables in other.qml
how should I do it?
Button.qml:
Rectangle { border.color: "black" radius:5 width:60 height:75 property var clikedX; property var clikedY; MouseArea{ anchors.fill:parent onClicked: { loader.source="other.qml" clikedX=parent.x clikedY=parent.y }
other.qml:
I would like here use the value of clikedX and ClikedY (so the value of parent.x and parent.y...)
-
wrote on 28 Apr 2017, 06:29 last edited by
Button.qml
Window { visible: true width: 640 height: 480 property real clikedX; property real clikedY; Rectangle { border.color: "black" radius:5 width:60 height:75 anchors.verticalCenter: parent.verticalCenter MouseArea{ anchors.fill:parent onClicked: { clikedX=parent.x clikedY=parent.y callOther.otherX=clikedX callOther.otherY=clikedY }} } Other{ id:callOther } }
Other.qml
Item { property real otherX; property real otherY; //youre code here }
-
@aktay So i have a variable in one file and i want use the value of this variable in an other file...
-
wrote on 1 May 2017, 11:24 last edited by
Maybe this is what you want :
Button.qml
Rectangle { border.color: "black" radius:5 width:60 height:75 property var clikedX; property var clikedY; signal btnClick() MouseArea{ anchors.fill: parent onClicked: { clikedX = mouseX clikedY = mouseY btnClick() } } }
Other.qml
Window { visible: true width: 640 height: 480 //title: qsTr("Hello World") Button { onBtnClick: { console.log(clikedX) console.log(clikedY) //do something with your clikedX/clikedY } } }
-
Maybe this is what you want :
Button.qml
Rectangle { border.color: "black" radius:5 width:60 height:75 property var clikedX; property var clikedY; signal btnClick() MouseArea{ anchors.fill: parent onClicked: { clikedX = mouseX clikedY = mouseY btnClick() } } }
Other.qml
Window { visible: true width: 640 height: 480 //title: qsTr("Hello World") Button { onBtnClick: { console.log(clikedX) console.log(clikedY) //do something with your clikedX/clikedY } } }
wrote on 1 May 2017, 11:35 last edited byIsn't this similar to the following code?
Button.qml
onClicked: { clikedX=parent.x clikedY=parent.y callOther.otherX=clikedX callOther.otherY=clikedY }} } Other{ id:callOther } }
Does not include all the content in Button.qml?
-
Hi @aktay
It's similar but a bit different, by using the btnClick() signal, I'm decoupling the Buttom.qml from Other.qml, that way Button.qml works as a generic button that can be used anywherewrote on 1 May 2017, 12:18 last edited byI know it. But I think @Titi01 does not want it.
@Titi01 said in Use a variable in another file:
If I use
/ Other{ id:callOther }
I have all the contents of Other.qml that appears in Button.qml
Let's see what happens.
Thank you for your response.
-
Hello,
I have 2 files:
-Button.qml
-Other.qmlI have 2 variable in button.qml (clikedX and clikedY)and I would like to retrieve the value of these variables in other.qml
how should I do it?
Button.qml:
Rectangle { border.color: "black" radius:5 width:60 height:75 property var clikedX; property var clikedY; MouseArea{ anchors.fill:parent onClicked: { loader.source="other.qml" clikedX=parent.x clikedY=parent.y }
other.qml:
I would like here use the value of clikedX and ClikedY (so the value of parent.x and parent.y...)
-
wrote on 2 May 2017, 12:47 last edited by
I create objects of "Button.qml" in the main.qml.
But I would like that when I click on this button that clikedX and clikedY contain the value of parent.x and parent.y.
Then after you can use the value of clikedX and clikedY in other.qml
But I do not create a Button in Other.qml ... @Yashpal @johngod @aktay -
I create objects of "Button.qml" in the main.qml.
But I would like that when I click on this button that clikedX and clikedY contain the value of parent.x and parent.y.
Then after you can use the value of clikedX and clikedY in other.qml
But I do not create a Button in Other.qml ... @Yashpal @johngod @aktaywrote on 8 May 2017, 04:15 last edited by@Titi01 Hopefully, you are creating an object of Other.qml in main.qml then, accessing clickedX and clickedY should be ease as objects of other.qml and button.qml are siblings. Other than this I'm unable to guess what you want to achieve.
3/14