Solved RE: QML id usage
-
@6thC I am using voice recognition over here based on the voice i am doing some changes is it possible using states ???
-
Well, I don't know the conditions to change states, that would be up to you. But sure. I think. It's possible I misunderstand or didn't read what you want enough.
I use states for changing the gui, not images but color and animations based on sensor's numeric values.
states: [ State { name: "inSpec"; PropertyChanges { target: indicator; color: hudGreen; } }, State { name: "warning"; PropertyChanges { target: indicator; color: "gold"; } }, State { name: "maximum"; PropertyChanges { target: indicator; color: "red"; } } transitions: [ Transition { from: "maximum" to: "inSpec" SequentialAnimation { loops: 1; ColorAnimation { duration: 300; from: "red"; to: hudGreen; } ColorAnimation { duration: 300; from: hudGreen; to: "transparent";} } }, Transition { from: "warning" to: "inSpec" ColorAnimation { target: root; duration: 300} ColorAnimation { duration: 300; from: "red"; to: hudGreen; } ColorAnimation { duration: 300; from: hudGreen; to: "transparent";} }, ...
And I use this in a method where I pass a control as an argument (with the states and transitions in it) and the sensor value like:
function adjustState(control, value){ ... inSpec expression control.state = "inSpec"; ... warning expression control.state = "warning"; ... maximum expression control.state = "maximum";
-
I tried using property aliasing...but no output can anyone pls guide me further in this matter....it would be helpful.
-
@Naveen_D You need to provide a very minimal complete code of your application which can reproduce the problem. This would be more useful for others to understand.
-
@p3c0 ya sure here is the code,
main.qml
import QtQuick 2.7 import QtQuick.Window 2.2 import QtQuick.Layouts 1.2 import QtQuick 2.5 Window { // flags: Qt.FramelessWindowHint title: qsTr("Infotainment") width: (Screen.width/4)+200 height: (Screen.height/4) visible: true ExampleRect1 { id: rect1id visible: false } ExampleRect2 { id: rect2id visible: false } Row { id: rectrow spacing: 100 anchors.centerIn: parent Rectangle { id: myRect width: 150; height: 100 color: "transparent" border.color: "black" Text { id: rect1text text: qsTr("Rectangle1") font.bold: true font.pixelSize: 24 color: "red" anchors.centerIn: parent } MouseArea { id: mouseArea anchors.fill: parent onClicked: { rect1id.visible= true rectrow.visible = false rect2id.visible= false } } } Rectangle { id: myRect2 width: 150; height: 100 color: "transparent" border.color: "black" Text { id: rect2text text: qsTr("Rectangle2") font.bold: true font.pixelSize: 24 color: "blue" anchors.centerIn: parent } MouseArea { id: mouseArea2 anchors.fill: parent onClicked: { rect2id.visible= true rectrow.visible= false rect1id.visible= false } } } } }
ExampleRect1.qml
import QtQuick 2.0 import QtQuick.Window 2.2 Rectangle{ id: mainRect width: (Screen.width/4)+200 height: (Screen.height/4) visible: false property alias imgSource : img.source Rectangle{ id: imgRect implicitHeight: 100; implicitWidth: 150 anchors.centerIn: parent Image { id: img source: "qrc:/PlayIcon.png" } } }
RectExample2.qml
import QtQuick 2.0 import QtQuick.Window 2.2 import QtQuick.Layouts 1.2 import QtQuick 2.5 Rectangle{ id :mainRect width: (Screen.width/4)+200 height: (Screen.height/4) ExampleRect1{ id:exampleRect1Object } Rectangle{ id: voicerect implicitHeight: 100; implicitWidth: 150 anchors.centerIn: parent color: "red" Text { id: voiceText text: qsTr("Voice Recognition") anchors.centerIn: parent font.pixelSize: 18 } MouseArea{ id: voicemousearea anchors.fill: parent onClicked: { rect1id.visible =true exampleRect1Object.imgSource= "qrc:/pauseIcon.png" mainRect.visible= false } } } }
-
@Naveen_D Ok. And what do you require in this example ?
-
@p3c0
In the above example, in ExampleRect2.qml i have created an object of ExampleRect1.qmlonClicked: {
rect1id.visible =true
exampleRect1Object.imgSource= "qrc:/pauseIcon.png"
mainRect.visible= false
}using that object i am changing the image set in exampleRect1.qml but the image remains same, it will not change...
-
@Naveen_D AFAICS you are creating 2 instances of
ExampleRect1
, one is inmain.qml
and other is inExampleRect2.qml
and you are trying to update this instance instead of one inmain.qml
-
@p3c0 Ya i got the output...thanks
-
@Naveen_D Great!