How to integrate c++ and qt quick(qml)
-
hi ixsci
I used qt creator rc version...created a new project and choosed qt mobile project ,then i added a new qml file (ran.qml)and return the below code..
@
import Qt 4.7Rectangle {
id: myRect; width: 640; height: 400; state: "OldState" Rectangle { id: movingRect; width: 60; height: 60; color: "red" MouseArea { anchors.fill: parent; onClicked: if (myRect.state == "OldState") myRect.state = "NewState"; else myRect.state = "OldState" } } states: [ State { name: "NewState" PropertyChanges { target: movingRect; x: 580; y: 340; radius: 30; color: "green" } } ] transitions: [ Transition { from: "OldState"; to: "NewState" PropertyAnimation { properties: "x,y,color,radius"; duration: 1000 } }, Transition { from: "NewState"; to: "OldState" PropertyAnimation { properties: "x,y,color,radius"; duration: 1000 } } ]
}
now i opened the main.cpp file and return the below code..
#include "mainwindow.h"
#include <QtGui/QApplication>
#include<QtDeclarative/QDeclarativeEngine>
#include<QtDeclarative/QDeclarativeComponent>
#include<QtDeclarative/QDeclarativeItem>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);MainWindow mainWindow; QDeclarativeEngine *engine = new QDeclarativeEngine; QDeclarativeComponent component(engine, QUrl::fromLocalFile("ran.qml")); QObject *myObject = component.create(); QDeclarativeItem *item = qobject_cast<QDeclarativeItem*>(myObject); mainWindow.setOrientation(MainWindow::ScreenOrientationAuto); mainWindow.showExpanded(); return app.exec();
}
@
so tell me whether its correct????Thanks
imrrk@@[EDIT: added @-tags for code formatting, Volker]
-
Your code has one C++ mistake:QDeclarativeItem* item = qobject_cast<QDeclarativeItem*>(myObject);
You will see nothing with your corrected code though. You must create scene for showing QML in it. I have no handy example for this but Vass presented simplified way to show QML in C++ code. Just follow the Vass code. When you master it you can move further to more complex examples with your own created scenes, if you will need any. But at the moment it would be better to use pre-created QDeclarativeView for comprehending rudiments of C++ and QML relationship. -
ixsci
By using Vass code
I getting fllowing errors
1.C:\Qt\qtcreator-2.0.94\intgmobile-build-simulator/../intgmobile/main.cpp:14: error: undefined reference to `_imp___ZN16QDeclarativeViewC1EP7QWidget'2.C:\Qt\qtcreator-2.0.94\intgmobile-build-simulator/../intgmobile/main.cpp:18: error: undefined reference to `_imp___ZN16QDeclarativeView9setSourceERK4QUrl'
3.:-1: error: collect2: ld returned 1 exit status
-
imrrk, add
@QT += declarative@
to your .pro file.P.S. And please don't multipost.
-
imrrk What i version of Qt on your device? For Qt quick you need install "developers build Qt 4.7":http://wiki.forum.nokia.com/index.php/Qt_4.7_for_Symbian^3_-_developer_version.
-
[quote author="imrrk" date="1293685687"]Hi everyone
I tried with all the possibilities listed here...the example works fine in simulator but when i try to deploy it on device using Nokia Qt SDK i m getting error as follows1.DeclarativeView no such file or directory
[/quote]The latest Nokia Qt SDK available has support for Qt 4.6 only for release builds. So you cannot build QML app release with this SDK.