I have a QML object as follows:
// File: ControlView.qml
Rectangle {
id: view
property bool darkBackground: false
Text {
id: textSingleton
}
SoundEffect {
id: playCalSound
source: "qrc:/sound/calender_time_camera.wav"
}
}
I have another control that is contained within it and my issue is that I cannot access the playCalSound or textSingleton elements.
// File: MyControl.qml
ControlView {
....
playCalSound.play() // playCalSound is not defined
textSingleton.font.pixelSize // textSingleton is not defined
view.textSingleton // view is not defined
}
You should define properties for objects you want use outside file. Try this
Rectangle {
id: view
property bool darkBackground: false
property var effect: playCalSound
property alias singletext: textSingletone.text
Text {
id: textSingleton
}
SoundEffect {
id: playCalSound
source: "qrc:/sound/calender_time_camera.wav"
}
}
..............
ControlView {
....
Component.onComplited: effect.play()
singletext: 'some text'
}
Related
TankModel { id: _model }
Connections {
target: _model
onModelUpdated: {
_tankList.enabled = true;
}
}
StackView {
id: _stack
anchors.fill: parent
initialItem: _page1
}
Component {
id: _page1
ComboBox {
id: _tankList
model: _model
}
}
After running the program, I get the error:
ReferenceError: _tankList is not defined
The error points to line number 6.
How to make ComboBox visible to Connections?
How to solve this problem?
You can instanciate your model inline. In this case the model can access its parent.
...
ComboBox {
id: _tankList
model: TankModel {
id: _model
onModelUpdated : {
_tankList.enabled = true;
}
}
}
...
I want to check if an item of component is vissible or not but I don't know how to do this.
Here is my code but error is "ReferenceError: tagFilterPlaylist is not defined" how to solve this?
Repeater {
id: tagRepeater
model: main.playListBrowseModel
Component {
id: componentTagFilterPlaylist
BasicUI.Tag {
id: tagFilterPlaylist
tag: "playlist"
selected: true
}
}
Loader {
id: filterLoader
sourceComponent:
if (item_type === "playlist"){
console.debug("check"+tagFilterPlaylist)
if (!tagFilterPlaylist) {
tagFilterPlaylist.visible = true;
return componentTagFilterPlaylist
}
}
}
}
You can't refer to an id within a Component because you first need to have an instance of that Component. What you should do is simply keep a boolean property outside the Repeater that keeps track of whether or not you're displaying your one and only tagFilterPlaylist.
property bool playlistVisible: false
Component {
id: componentTagFilterPlaylist
...
}
Repeater {
id: tagRepeater
model: main.playListBrowseModel
Loader {
id: filterLoader
sourceComponent:
if (item_type === "playlist"){
if (!playlistVisible) {
playlistVisible = true;
return componentTagFilterPlaylist
}
}
}
}
Suppose I had the Component
Component {
id: myComp1
Item {
id: item
ListView {
id: listView
model : ListModel { id: listModel }
delegate : RowLayout { /* display model data*/ }
Component.onCompleted {
// get data from server ...
model.append(dataFromServer)
}
}
}
}
Then I have a second Component, which is another page in the stack, and I want to use this component to update mycomp1, i.e:
Component {
id: myComp2
Button {
onClicked: {
myComp1.item.listView.listModel.append(someNewData) // want to be able to do this
}
}
}
And these components are tied together in a StackView
Now, this doesnt seem to work since myComp2 cant seem to access the necessary scope to update the model of myComp1. Is there any way around this?
Thanks for the help.
The problem is that a Component is like a type declaration. It does not define an instance of an object, so you cannot access its members.
You could pull the ListModel outside of that Component so that both Components can access it.
ListModel {
id: listModel
}
Component {
id: comp1
ListView { model: listModel }
}
Component {
id: comp2
Button {
onClicked: { listModel.append(someNewData) }
}
}
I have a TreeView, and on the signal onSortIndicatorChanged I would like to set the checked property of a CheckBox inside the delegate of a TableViewColumn in my TreeView. However, I don't know how to do this.
Component {
id: mycomp
Item {
id: myitm
CheckBox {
id: mycbx
checked: true
}
}
}
TreeView {
TableViewColumn {
delegate: myDelegate
}
onSortIndicatorChanged{
// set the checked property of the delegates to true
}
}
You could try to add a property checked to your tree view and bind the check boxes to this property instead. Then, in the even handler, just set the value of the TreeView's property:
Component {
id: mycomp
Item {
id: myitm
CheckBox {
id: mycbx
checked: view.checked
}
}
}
TreeView {
id: view
property bool checked: false
TableViewColumn {
delegate: mycomp
}
onSortIndicatorChanged {
view.checked = true
}
}
Update:
In case you want to set a per-item checked state, you can use a similar approach where you store a function as a property of the view. In the delegate, you could call this stored function to get an item specific property value:
Component {
id: mycomp
Item {
id: myitm
CheckBox {
id: mycbx
checked: view.checkFunction ? view.checkFunction(modelData) : true
}
}
}
TreeView {
id: view
property var checkFunction: null
TableViewColumn {
delegate: mycomp
}
onSortIndicatorChanged {
view.checkFunction = function(modelData) {
// Calculate the checked state based on the modelData
return modelData.foo == "bar";
}
}
}
There's an element FlowChart with properties p1,p2,p3 ... pn.
SensorValuesSetter must be able to access these properties by using the element Button which is located deeply in it.
I imagine the solution like that:
// main.qml
// ...
FlowChart {
id: flowChart
anchors.fill: parent
SensorValuesSetter {
id: valueSetterWindow
}
// ...
}
// SensorValuesSetter.qml
ApplicationWindow {
id: valueSetterWindow
// ...
GridLayout {
// ...
Label { text: "Давление 1: "; Layout.fillWidth:true; }
ValueInputField { id: p1_val_field; }
Label { text: "Давление 2: "; Layout.fillWidth:true; }
ValueInputField { id: p2_val_field; }
// ....
Button {
id: button
text: qsTr("Применить")
onPressed: {
valueSetterWindow.parent.p1.value = Number.fromLocaleString(p1_val_field.text)
valueSetterWindow.parent.p2.value = Number.fromLocaleString(p2_val_field.text)
// ...
}
}
But in this case errors TypeError: Cannot read property 'p1' of undefined or TypeError: Cannot read property 'p1' of undefined appear.
Could you please explain me what's the problem? I guess the clue is that I tried to refer to the parent's parent with wrong way.
ApplicationWindow is no Item and therefore has no property parent.
You can either rely on dynamic scoping (see more on the scopes here)
Or you pass the parent explicitly in a new property:
// SensorValuesSetter.qml
ApplicationWindow {
property Item parent // Add the missing property. Maybe some other name...
[...]
}
And where you instantiate it:
SomeItem {
id: myItem
SensorValuesSetter {
parent: myItem // Set it explicitly
}
}