I have a main.qml and inside that i have two banners(titlebanner and bottombanner) as shown in below.
Window {
id: main
visible: true
Image {
source: "qrc:/banner.png"
anchors.fill: parent
}
TitleBanner {
x:0
y:10
}
BottomBanner {
x:0
y:420
}
}
In TitleBanner.qml, i have below code
Item {
Rectangle {
id : id_title_banner
property real value : titlebanner.title
Image {
source: "qrc:/banner_title.png";
}
//todo:: some animation stuffs on 'value' later
}
}
In BottomBanner.qml, i have below code
Item {
Rectangle {
id : id_bottom_banner
property real value : bottombanner.title
Image {
source: "qrc:/banner_bottom.png";
}
//todo:: some animation stuffs on 'value' later
}
}
In C++ side, I am keeping two separate objects(for later flexibility) for both title banner and bottom banner. I set the root context property to expose the title banner objects from C++ to qml as below
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
//create title banner object
CTitleBanner *objTitleBanner = new CTitleBanner();
//create bottom banner object
CBottomBanner *objBottomBanner = new CBottomBanner();
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("titlebanner", objTitleBanner);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
My question is, how can we set context property for both titlebanner and bottombanner qml files separately instead of setting it to root context ? My concern is at later point if more banner comes, I set it to rootContext. Is it a proper way of doing ? How can we create seperate context for each banner ?
QObject contextDatawhich has two properties:titlebannerandbottombannerwhich you set to be (by coincidence maybe?) your two original banner objects. However I think the answer to my initial question should be "no". - derM