PDA

View Full Version : access rootContext() from Qt Resources QML



cia.michele
7th April 2011, 10:29
Hello to everybody,
I'm a newbie with QML and QT but I like it much!
I'm trying to develop a funny application which must access database, acquire data from Modbus TCP server and display it on QML page.

I'm developing different part separatly, and I've create the interface e some of back-end components, now, my problem is: how paste all into an executable?
I'm using Microsoft Win XP and Win 7 to deploy my application.

Reading QML documentation I found how create Qt Resources from QML files, so I can include them into executable to prevent errors or damage on .qml files. Now I try to create some method to update the field I inserted into QML files.

My main.qml file contains some other qml component accessed by QtResource.

My problem is: I need to expose C++ methods to QML interfaces located into QtResouces path.

I have this code:
class DeclarativeView : public QDeclarativeView
{
Q_OBJECT

public:
explicit DeclarativeView(QWidget *parent = 0);
~DeclarativeView();
QTimer * tim;
Q_PROPERTY (double myValue READ readMyValue);
double myVal;
void Init();
private slots:
void updateValues();

};
the main element is Init function and readMyValue function

void DeclarativeView::Init(){

setFocusPolicy(Qt::StrongFocus);
setResizeMode(QDeclarativeView::SizeRootObjectToVi ew);
setSource(QUrl("qrc:/Sezione1.qml"));
rootContext()->setContextProperty("window",this);
}

double DeclarativeView::readMyValue(){

return myVal;
}



... but when I run my application I got this message:

qrc:/Sezione1.qml:13: ReferenceError: Can't find variable: window

How can access the rootContext from qrc environment?

Is this the right way to do this?

My objective is have only one executable which could access QML files from internal resources .qrc files, but also using Tcp Socket to acquire data and sql model to access database by C++ codes.

Thanks for your time!

Michele

tsp
8th April 2011, 06:21
Swap the lines 5 and 6 in the latter part of your code and see whether it makes any difference. I remember seeing a comment in the documentation that first define context properties and then set the source file and your code you seem to do it in the opposite order.

qttre
6th May 2011, 07:53
thanks,very much,It's very detail..........