View Full Version : QML screens managed by cpp application

18th November 2011, 09:00
Hello all

I need to change my application gui from widget base to qml based
the application main view has: static header, static foother and at the middel, changing content (same as QMainWindow that have toolbar statusbar and central widget).
in widget base application i used QStackedWidget to handle the switching content (switch between widgets/views) and i need to do it in QML with transition between two screens (while new screen change the current screen)

more than that i have to:
1) create view on demand
2) not destroy the created view after it no longer the current screen (for feture use)

how can i implement QStackWidget idea based on QML (i dont want to embed the QStackedWidget in QDeclarativeItem, register it and use it)
and most important to manage it in the CPP code (manage what will be the new current view, and which 2 screen to animate the transition)

(i try to use ApplicationWindow from the desktop-components and set the ApplicationWindow data in the cpp code using setContextProperty, but i'm not successeded and if it success and show the new view the transition wasnt work

my implementation is something like:

QDeclarativeView mMainView;
mpQmlRootContext = mMainView.rootContext();
mpQmlEngine = mMainView.engine();
mpQmlRootObject = dynamic_cast<QObject*>(mMainView.rootObject());

i create QDeclarativeComponent for each switching screen (i.e for each .qml file that need to be in the content) as

QDeclarativeComponent* mpTestComp = new QDeclarativeComponent(mpQmlEngine, QUrl::fromLocalFile("../Resources/Qml/MainWindow/Home/Home.qml"));

when the user ask for Home screen we create the screen and save its QDeclarativeItem

QObject *myObject = mpTestComp->create();
QDeclarativeItem* item = qobject_cast<QDeclarativeItem*>(myObject);
connect(item, SIGNAL(sigPhotosClicked()),this,SLOT(SltTestChange ()));

i try some option to change the content using the data
option 1:
mpQmlRootContext->setContextProperty("data", QVariant::fromValue<QDeclarativeItem*>( item ));

The qml "MainWindow.qml" is something like:


statusBar: Rectangle{.......}
toolBar: Rectangle{........}


this was not worked


mpQmlRootContext->setContextProperty("dataItem", QVariant::fromValue<QDeclarativeItem*>( item ));

The qml "MainWindow.qml" is something like:

data: dataItem
statusBar: Rectangle{.......}
toolBar: Rectangle{........}


Can some one please help me
Thanks in advanced