ReferenceError: "Item" is not defined - qt

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
}
}
}
}

Related

Why is the component used in the StackView not defined if it is declared in the program?

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;
}
}
}
...

Change property of delegate on signal

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";
}
}
}

How to refer to the parent of the parent correctly?

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
}
}

QML - Retrieving index of delegate in DelegateModel

I have trouble retrieving the index of a delegate that is instantiated inside a DelegateModel for a ListView.
The minimal example as following:
LastProcedures.qml
ListModel {
ListElement {
procedure: "Liver Resection"
surgeon: "Prof. Dr. Joyride"
recent: true
}
...
}
main.qml
ListView {
id: list_lastProcedures
model: displayDelegateModel
}
DelegateModel {
id: displayDelegateModel
delegate: lastProceduresDelegate
model: LastProcedures {}
groups: [
DelegateModelGroup {
includeByDefault: false
name: "recent"
}
]
filterOnGroup: "recent"
Component.onCompleted: {
var rowCount = model.count;
items.remove(0,rowCount);
for( var i = 0;i < rowCount;i++ ) {
var entry = model.get(i);
// Only the recent three
if((entry.recent == true) && (items.count < 3)) {
items.insert(entry, "recent");
}
}
}
}
Component {
id: lastProceduresDelegate
Text{
text: model.index
}
}
The text index prints always -1. Without a DelegateModel it prints the index in the ListView. How can I access the correct index of the delegate in the Listview?
you can use "lastProceduresDelegate.DelegateModel.itemsIndex" instead of "model.index"
just like this:
Component {
id: lastProceduresDelegate
Text{
text: lastProceduresDelegate.DelegateModel.itemsIndex
}
I ended up with not removing all entries and adding them back to groups, but instead just remove unwanted entries. This ways the index stays valid.
If someone could explain this behavior further, that would be nice.
DelegateModel {
id: displayDelegateModel
delegate: lastProceduresDelegate
model: LastProcedures {}
groups: [
DelegateModelGroup {
includeByDefault: true
name: "recenttrue"
}
]
filterOnGroup: "recenttrue"
Component.onCompleted: {
for( var i = 0;i < items.count;i++ ) {
var entry = items.get(i).model;
// Only the recent
if((entry.recent != true)) {
items.removeGroups(i, 1, "recenttrue");
}
}
}
}
The DelegateModel has some hidden magic regarding groups (it's not very visible but it's here ). For each group you create, the DelegateModel attached property will receive two new properties: <group>Index and in<Group>.
In your case this means you will get the following properties: recentIndex and inRecent (or in your own answer: recenttrueIndex and inRecenttrue).
I think with what you want to do you should go the recenttrue route and draft the Component as follows:
Component {
id: lastProceduresDelegate
Text {
text: lastProceduresDelegate.DelegateModel.recenttrueIndex
}
}

Cannot access elements of the container

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'
}

Resources