0

I know this could be a really small thing i am missing here, but I have spent some good amout our hours trying to figure this out. I am from an Objective-C background and what I am trying to do is this:

I have a main.qml which is a navigationPane and it has 2 other external pages added to it as attached object. The two pages have grid list views. Now, There is a MyApp.cpp file that loads a Json file and populates the result in the main.qml file. I only display the relevalt items on this page at first. When the user taps on any item, I want to take them to page2.qml which has a grid list view as I mentioned above and populate it with dataModel passed from main.qml (which has all the data from MyApp.cpp). This has give me no joy at all! I need help. What can I do to make this work? Please I need help on this one...

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Donald King
  • 9
  • 1
  • 3
  • I'm a bit hesitant to tag this with C++, QML and JSON. Would those be about right? Or you can add the tags. – Bernhard Barker Feb 10 '13 at 01:04
  • Maybe you could use property aliases to expose properties or objects, as explained here: http://bbcascadescode.tumblr.com/post/40740528163/stack-of-screens-passing-information-around – Miguel López Feb 11 '13 at 18:14
  • Since you're getting your data from a DataModel that is on your C++ code (from a Q_INVOKABLE function, I suppose), why don't you just call the same function again? It would make the code simpler. – Dielson Sales Feb 13 '13 at 05:09

1 Answers1

0

I'm not sure if this is best practice or not, but the easiest method I found was to use the Qt global object.

Basically assign either your data or an object to the Qt object and then you can access it from any other page.

E.g.
In main.qml in onCreationCompleted or whatever function you receive your data in:

function getData(data) {
    Qt.myVariable = data;
}

Then in your other page(s) you can access it. E.g. in Page2.qml:

Page {
    onCreationComplete: {
        myLabel.text = Qt.myVariable;
    }
}

As I mentioned, this works for objects as well, so you can assign a navigation pane, a sheet, a page, etc. So things like "Qt.myNavPane.push(page)" becomes possible.

I encountered issues where I needed to use Qt for various purposes. Signals and slots are better practice I think, but are not always practical.

hyarion
  • 2,251
  • 2
  • 17
  • 28
  • Hi smart guys... Thanks so much for your responses. the data I am looking to pass to the next page is a data model to be used in the grid list view in page2.qml. I have used property alias to pass text sat at from page1.qml to page2.qml but when I pass the model, it doesn't populate the list. Also page2.qml has a property called detailData which is a variant alias. Hope I am making sense? @peter – Donald King Feb 11 '13 at 22:41
  • You could try using a function instead of a property alias. So on page2 you could have "function passData(dataModel)" and then on Page1 you can call the function something like "page1.passData(myDataModel)". If I understand you correctly that may be what you are looking for. – hyarion Feb 12 '13 at 07:15