PDA

View Full Version : Using QDeclarativeView::Show()



proj_symbian
30th May 2011, 07:42
Hello all,

i am making a QML application for symbian devices which has a very rich GUI at the same time all the data is coming from web services in form a xml.

So what i have done is my all parsing and logic engines are written in c++ and the UI is written in QML. Now i am facing some problems in that.

When i start the application i make a QNetworkAccessManager get request to get the initial data to show. At that time i load a main.qml file which only have a wait indicator.

Now when the asynchronous get method completes inside my slot GotNetworkReply, i am starting to parse the xml response and then i want to show another QML view for that i use the following code:


QDeclarativeView homeScreenView;
homeScreenView.SetSource("my QML path");
homeScreenView.Show();

I get no error but my homescreen QML file doesnt shows up, can any body help me what wrong i am doing or what's the other approach to update the data in QML from C++.

Regards
Ronald

wysota
30th May 2011, 08:56
Try using QUrl::fromLocalFile(). And make sure the file exists (QFile::exists()) first.

proj_symbian
30th May 2011, 09:17
hello wysota

thanks for your attention and the code i pasted there was just a test code, because i was using QUrl::fromLocalfile().

Following is the exact code i am using:



QFile file("qml/HyBridApp/homescreen.qml");
if(file.exists())
{
QDeclarativeView homeScreenView;
homeScreenView.setSource(QUrl::fromLocalFile("qml/HyBridApp/homescreen.qml"));
homeScreenView.showFullScreen();
}
else
{
qDebug()<<"file doesnt exists";
}


There is no error but homescreen.qml doesnt comes up. :-(

Regards

wysota
30th May 2011, 09:19
Whats the contents of the qml file?

proj_symbian
30th May 2011, 09:23
its just a simple rectangle, as of once the view shows up i will add the actual UI:



Rectangle {
width: 360
height: 360
Text {
anchors.centerIn: parent
text: "Hello World"
}
MouseArea {
anchors.fill: parent
onClicked: {
Qt.quit();
}
}

}


If you want i can post all of my project here in a rar file??

wysota
30th May 2011, 09:34
You are missing the following line:
import QtQuick 1.0

proj_symbian
30th May 2011, 10:51
Hello Wysota

thanks again for your reply, but my QML contains that, let me rephrase the problem again:

1. following is my main.cpp where at first i am making an get request:


int main(int argc, char *argv[])
{
QApplication app(argc, argv);

NetworkRequest httpRequest;
httpRequest.GetNewsResponse("http://iphone.blick.ch/");

QmlApplicationViewer viewer;
viewer.setOrientation(QmlApplicationViewer::Screen OrientationLockPortrait);
viewer.setMainQmlFile(QLatin1String("qml/HybridApp/main.qml"));
viewer.showExpanded();

return app.exec();
}


Here main.qml contains a wait indicator and is waiting for the data from the server.

2. Where NetworkRequest is a class derived from QObject and it simply helps in making an http get request from the method GetNewResponse.

3. in NetworkRequest Class i have connect the finished(QNetworkReply*) signal of QNetworkAccessManager with my GotNetWorkReply(QNetworkReply*) slot of that class. Everything works fine here.

4. Now when i get the reply from the network i write the following code:


QFile file("qml/HybridApp/HomeScreen.qml");
if(file.exists())
{
qDebug()<< "file exists";
QDeclarativeView homeScreenView;
homeScreenView.setSource(QUrl::fromLocalFile("qml/HybridApp/HomeScreen.qml"));
homeScreenView.showFullScreen();
}
else
{
qDebug()<<"hell file doesnt exists";
}



5. Here HomeScreen.qml contains :


import QtQuick 1.0

Rectangle {
width: 360
height: 640
color: "red"
}




5. Now what i want is when i get the ansynchronous reply from the webserver i want to show this QML file, using some possible options, i am trying that using QDeclarativeView::Show(), but its not working the QML file doesnt comes up.

Hope you now got my problem???

Waiting for your reply.

wysota
30th May 2011, 11:26
3. in NetworkRequest Class i have connect the finished(QNetworkReply*) signal of QNetworkAccessManager with my GotNetWorkReply(QNetworkReply*) slot of that class. Everything works fine here.
Does the slot get called?

proj_symbian
30th May 2011, 11:32
yes the slot gets called infact when i set break point my break point goes inside the file.exists() statement. over the QDeclarativeView::show method but the QML file never comes up.

wysota
30th May 2011, 11:38
Does the same qml file work when called with qmlviewer?

By the way, you can pass a remote url to QDeclarativeView::setSource() and the file will be downloaded automatically if available.

proj_symbian
30th May 2011, 11:46
yes it works separately in the QMLViewer and i have tried the other way too by placing that in a remote url, but nothing happens.

I have attached the code here if you have time please have a look and let me know, its such a simple thing but its not happening. :-(

Regards,

wysota
30th May 2011, 11:51
You are creating a new declarative view on the stack so it gets out of scope and is destroyed immediately.

proj_symbian
30th May 2011, 11:55
oh yes yes that was the problem indeed thanks a lot wysota for your patience and help,

i have one more doubt is this the correct approach i am doing or there is any better approach to update data on QML file?? I have gone through the docs but i was not able to understand.

Regards,

wysota
30th May 2011, 12:12
I don't know what is best but I don't see a point in creating a new declarative view if you just want to change the script. You can call setSource() on the existing view.

proj_symbian
30th May 2011, 19:13
yes you are correct, but how can i get the reference of QApplicationViewer present in main.cpp to my NetworkManager class??

I know this is a very basic question but i am not able to do that

wysota
30th May 2011, 19:41
It doesn't have to be in the NM class. Emit a signal and catch it elsewhere where you have a pointer to the view at hand.

proj_symbian
30th May 2011, 20:06
apologies but its still not clear , i have a pointer of QApplicationViewer in main.cpp only, how can i get that on my NM class, are you telling i need to emit a signal from my main.cpp but i guess thats not possible.

a simple code example for illustration will be greatful.

wysota
30th May 2011, 20:26
apologies but its still not clear , i have a pointer of QApplicationViewer in main.cpp only, how can i get that on my NM class, are you telling i need to emit a signal from my main.cpp but i guess thats not possible.
So create another class to encapsulate what you already have or subclass QApplicationViewer and add appropriate slot to it.