Hi, Thank You for your answer!
So it should be something like this?!
C++
{
Q_OBJECT
Q_PROPERTY( QString qmlViewPath READ getQmlViewPath
)
public:
explicit View
(QObject *parent
= 0);
};
}
void main()
{
View firstView;
View secondView;
QQmlApplicationEngine appEngine;
appEngine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
// Show view 1
appEngine.
rootContext()->setContextProperty
("viewClassObject",
(QObject *)&firstView
);
// ...
// Show view 2
appEngine.
rootContext()->setContextProperty
("viewClassObject",
(QObject *)&secondView
);
// ...
}
C++
class View : public QObject
{
Q_OBJECT
Q_PROPERTY( QString qmlViewPath READ getQmlViewPath )
public:
explicit View(QObject *parent = 0);
virtual QString getQmlViewPath();
};
}
void main()
{
View firstView;
View secondView;
QQmlApplicationEngine appEngine;
appEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
// Show view 1
appEngine.rootContext()->setContextProperty("viewClassObject", (QObject *)&firstView);
// ...
// Show view 2
appEngine.rootContext()->setContextProperty("viewClassObject", (QObject *)&secondView);
// ...
}
To copy to clipboard, switch view to plain text mode
QML
ApplicationWindow {
visible: true
toolBar: Rectangle{
....
}
Loader {
id: loaderViewContainer
anchors.fill: parent
focus: true
source: viewClassObject.qmlViewPath
}
}
QML
ApplicationWindow {
visible: true
toolBar: Rectangle{
....
}
Loader {
id: loaderViewContainer
anchors.fill: parent
focus: true
source: viewClassObject.qmlViewPath
}
}
To copy to clipboard, switch view to plain text mode
Would the change of the context of the root force the loader to load/reload the qml?
Or would it be better to create a "ViewController" class which contains all the viewsClasses with their contexts so i only have to set the root context (ViewController) once and then set the current view context for each view
e.g.:
void main()
{
ViewController viewController;
QQmlApplicationEngine appEngine;
appEngine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
// Show view which is provided by the viewControllerClass as property
appEngine.
rootContext()->setContextProperty
("viewControllerObject",
(QObject *)&viewController
);
}
changeViewSignalReceipt()
{
// view will be changed automatically by property of the view Controller, but we have to set the context for the loaded view
appEngine.
rootContext()->setContextProperty
("viewClassObject",
(QObject *)&secondView
);
// or do i have to set the "view class" context to the loaderObject?
}
void main()
{
ViewController viewController;
QQmlApplicationEngine appEngine;
appEngine.load(QUrl(QStringLiteral("qrc:/main.qml")));
// Show view which is provided by the viewControllerClass as property
appEngine.rootContext()->setContextProperty("viewControllerObject", (QObject *)&viewController);
}
changeViewSignalReceipt()
{
// view will be changed automatically by property of the view Controller, but we have to set the context for the loaded view
appEngine.rootContext()->setContextProperty("viewClassObject", (QObject *)&secondView);
// or do i have to set the "view class" context to the loaderObject?
}
To copy to clipboard, switch view to plain text mode
Is it possible to set multiple ContextProperties to the rootContext, or do i have to get the Loader Object and set the view Context there?
And how would i get the loader Object from the QML to set its context. Or can i bind the context of the loader object from the viewController?
Bookmarks