Solved QML - How to add into a qml file two different qml files, based on condition. Is it posible?
-
Say we have : main.qml
And I want to add first.qml, or second.qml with the same id (but different contents) inside an item {
} from main.qml, based on a condition.
-
-
It didn't helped me, I tried with an Loader, but didn't get expected results.
-
Maybe this page then?
http://doc.qt.io/qt-5/qml-qtquick-loader.html
It's more recent, so should work better. -
@Flavian said:
And I want to add first.qml, or second.qml with the same id
Is this - having two objects with the same
id
, allowed at all? I don't use QML but I think theid
should be unique, shouldn't it? -
@Flavian What didn't work ? Its plain simple. The component name is actually the file name. They are available readily if its in the same path.
//File1 import QtQuick 2.0 Rectangle { id: rect color: "green" width: 40 height: 40 } //File2 import QtQuick 2.0 Rectangle { id: rect //same id as File1 color: "red" width: 40 height: 40 } //main.qml import QtQuick 2.0 Item { width: 200 height: 200 File1 { x: 0 } File2 { x: 100 } }
@kshegunov Not a problem if they are in different file. It would conflict if its in the same file just like some public member in C++.
-
Hello everyone, thank you for you kind answers, I was hoping for something like this:
Item{Loader {
//id:loader
source: (condition ? "File1.qml" : "File2.qml")
}
}And rely on the loader's id, or the id from the file1.qml , or file2.qml(in my case identical)-> it didn't provide the functionality, I really didn't access directly File1.qml (like loader.property -> or directly with the id from inside File1.qml . It seemed a little overhead to try loader.id(File.qml).
After of hours of trial and error, i have ended in using both files:
Item{
File1{}
File2{}}
And adapting them with different calls, as needed.Thank you again for you kind effort, you are the best!
-
@Flavian The
Loader
method will work too. To access component loaded insideLoader
you need to use Loader.item property. -
You are right, thank you!