I have a situation which I need a component which holds a ListView of its own type. Here is a simplified version:
myComp.qml
import QtQuick
Item {
Text {
id: name
text: qsTr("Hello")
}
ListView{
model: 5
delegate: MyComp{}
}
}
main.qml
import QtQuick 2.15
import QtQuick.Window 2.15
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
MyComp{
}
}
qmldir.txt
MyComp 1.0 myComp.qml
After running this, I get recursive instantiation error which is expected.
My main questions are:
- Is there a workaround to overcome this issue?
- What is the general approach if you want to implement such thing? for example what should I do if I want to implement something like the query builder UI provided here?
As @Atmo mentioned in the comments, you cannot declare a type in itself. However, in your case, an infinite loop will not occur.
As far as I know, there are two options for solving your problem:
1. Loader
The simplest option is to use a Loader [1]. I also suggest using DelegateChooser to switch between delegates, as you don't need to use a Loader on all sub-items and write conditions, etc.
For example:
Group.qml
And usage is like:
Preview:

2. Move Delegate to outside of file
This is a cleaner approach, as it doesn't require a Loader, but it does require using multiple files, which is fine. Here is a simplified example:
Group.qml
Delegate.qml (simplified version)
The usage remains the same:
Preview:
