×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    31

    Get missied in pass data from c++ to qml listview

    I want to use listview in QML whose data model is com from c++,
    how ever i get missed with the following two part of codes.

    This first codes below fail to append and display data in QML.
    and the simulator debug tells that file:///opt/loveNote/qml/loveNote/DocumentPage.qml:34: ReferenceError: Can't find variable: myModel
    Code:
    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
        QScopedPointer<QApplication> app(createApplication(argc, argv));
    
        AnimalModel model;
             model.addAnimal(Animal("Wolf", "Medium"));
             model.addAnimal(Animal("Polar bear", "Large"));
             model.addAnimal(Animal("Quoll", "Small"));
             model.addAnimal(Animal("A", "B"));
             model.addAnimal(Animal("b", "7"));
             model.addAnimal(Animal("V", "4"));
             model.addAnimal(Animal("D", "5"));
             model.addAnimal(Animal("G", "5"));
             model.addAnimal(Animal("K", "2"));
             model.addAnimal(Animal("S", "B"));
             model.addAnimal(Animal("Z", "U"));
             model.addAnimal(Animal("C", "0"));
             model.addAnimal(Animal("W", "O"));
             model.addAnimal(Animal("bear", "Lge"));
    
        QmlApplicationViewer viewer;
    
       //   QDeclarativeContext *ctxt = viewer.rootContext();
         //   ctxt->setContextProperty("myModel", &model);
       QDeclarativeView view;
         view.rootContext()->setContextProperty("myModel", &model);
         view.setSource(QUrl("/opt/loveNote/qml/loveNote/main.qml")); 
         view.show();
    
    
        viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
        viewer.setMainQmlFile(QLatin1String("qml/loveNote/main.qml"));
        viewer.showExpanded();
    
        return app->exec();
    }

    The second part is correct
    Code:
    Q_DECL_EXPORT int main(int argc, char *argv[])
    {
        QScopedPointer<QApplication> app(createApplication(argc, argv));
    
        AnimalModel model;
             model.addAnimal(Animal("Wolf", "Medium"));
             model.addAnimal(Animal("Polar bear", "Large"));
             model.addAnimal(Animal("Quoll", "Small"));
             model.addAnimal(Animal("A", "B"));
             model.addAnimal(Animal("b", "7"));
             model.addAnimal(Animal("V", "4"));
             model.addAnimal(Animal("D", "5"));
             model.addAnimal(Animal("G", "5"));
             model.addAnimal(Animal("K", "2"));
             model.addAnimal(Animal("S", "B"));
             model.addAnimal(Animal("Z", "U"));
             model.addAnimal(Animal("C", "0"));
             model.addAnimal(Animal("W", "O"));
             model.addAnimal(Animal("bear", "Lge"));
    
    
        QmlApplicationViewer viewer;
    
         QDeclarativeContext *ctxt = viewer.rootContext();
           ctxt->setContextProperty("myModel", &model);
    /*
    QDeclarativeView view;
         view.rootContext()->setContextProperty("myModel", &model);
         view.setSource(QUrl("/opt/loveNote/qml/loveNote/main.qml"));    //
         view.show();
    */
    
    
        viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
        viewer.setMainQmlFile(QLatin1String("qml/loveNote/main.qml"));
        viewer.showExpanded();
    
        return app->exec();
    }
    And the second part successfuly display data in listview in my QML

    my Listview codes here
    Code:
      ListView{
            id:docListView;
            //anchors.bottomMargin: 5
            anchors.topMargin: 0
    
            objectName: "docView";
            anchors.top: pageTitle.bottom;
            anchors.left: parent.left;
            anchors.right: parent.right;
            anchors.bottom: parent.bottom;
            clip:true;
            model:myModel
    
            delegate:
                Rectangle{
                width:480;
                height:80;
                Text {
                    text: "Document: " + title + ", " + subtext
                   font.pointSize: 15
                }
    
            }
    
     }
    What my main problem is how to dynamically append model data from c++ to ListView successfully.
    Could anyone tell me how to do it?
    I dont want to append data in the main function like this above.
    I want to make it in a class ,when i call the class 's function i can apend data to ListView.

  2. #2
    Registered User
    Join Date
    Jun 2012
    Posts
    105

    Re: Get missied in pass data from c++ to qml listview

    If you intend to add data for example when pressing a button on the phone's screen, you need to make a function Q_INVOKABLE in your model (so that you can call it from QML).

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    31

    Re: Get missied in pass data from c++ to qml listview

    Got missed,could u please take my codes as examples???

  4. #4
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    4,033

    Re: Get missied in pass data from c++ to qml listview

    I wrote an article some time back , might be that is helpful : http://www.developer.nokia.com/Commu...L_from_C%2B%2B

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    31

    Re: Get missied in pass data from c++ to qml listview

    I've read your article again and again ,finally i konw where i got missed.
    Thank you ......

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    31

    Re: Get missied in pass data from c++ to qml listview

    Thank you!!!
    Quote Originally Posted by vineet.jain View Post
    I wrote an article some time back , might be that is helpful : http://www.developer.nokia.com/Commu...L_from_C%2B%2B

Similar Threads

  1. Listview Item/Data Search
    By prodigy47 in forum Nokia N9
    Replies: 12
    Last Post: 2012-06-11, 06:55
  2. navigating listview is too slow when having lot of data
    By Rondo23 in forum [Archived] Qt Quick
    Replies: 2
    Last Post: 2011-12-13, 13:04
  3. how to get listview data from another qml page
    By web_tur in forum Nokia N9
    Replies: 5
    Last Post: 2011-09-14, 19:05
  4. How to pass QSqlTableModel to ListView?
    By Sasler in forum [Archived] Qt Quick
    Replies: 3
    Last Post: 2011-04-15, 22:07

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×