Results 1 to 6 of 6

Thread: QtQuick and C++ datamodel

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2013
    Posts
    8
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo

    Default QtQuick and C++ datamodel

    For example, I have a class named MyClass that contains a variable named myModelVariable.
    myModelVariable is a class that inherits QAbstractListModel.
    As like many other class that inherits QAbstractListModel, there is always a list variable in it.
    In my case, I use QVariantList, and it's empty when initialized.

    MyClass has a function that connect to network and the result is a JSON list that I will use to update data in myModelVariable.


    maybe this is example can describe my problem.


    Qt Code:
    1. #ifndef MYCLASS_H
    2. #define MYCLASS_H
    3.  
    4. #include <QObject>
    5. #inclide <QNetworkAccessManager>
    6. #include "mymodel.h"
    7.  
    8. class MyClass : public QObject
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit MyClass(QObject *parent = 0);
    14.  
    15. void getData();
    16.  
    17. signals:
    18.  
    19. public slots:
    20. void dataLoaded();
    21.  
    22. private:
    23. MyModel *myModelVariable;
    24.  
    25. QNetworkAccessManager *manager;
    26.  
    27. };
    28.  
    29. #endif // MYCLASS_H
    To copy to clipboard, switch view to plain text mode 



    Qt Code:
    1. #include "myclass.h"
    2.  
    3. MyClass::MyClass(QObject *parent) :
    4. QObject(parent)
    5. {
    6. myModelVariable = new MyModel;
    7. manager = new QNetworkAccessManager;
    8. }
    9.  
    10. void MyClass::getData()
    11. {
    12. QNetworkReply *reply = manager->get("https://localhost/content/");
    13. connect(reply, SIGNAL(finished()), this, SLOT(dataLoaded()));
    14. }
    15.  
    16. void MyClass::dataLoaded()
    17. {
    18. QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
    19.  
    20. if (!reply) {
    21. return;
    22. }
    23. else
    24. {
    25. if(reply->error())
    26. {
    27. qDebug() << "error: " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString();
    28. }
    29. else
    30. {
    31. QByteArray result = reply->readAll();
    32. qDebug() << result;
    33.  
    34. bool parsingOk;
    35.  
    36. QVariantList contentList = Json::parse(result, parsingOk).toList();
    37. QVariantMap singleContentMap;
    38.  
    39. if(parsingOk)
    40. {
    41. myModelVariable->setList(contentList);
    42.  
    43. //how to update qml listview here?
    44. }
    45. }
    46.  
    47. reply->deleteLater();
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 


    My question is how can I update my ListView on qml?
    Is it possibel to use property binding with NOTIFY?
    if it's possible, what should I write in that function?

    Btw, I'm still new to QtQuick and QML and I'm really thankful for any helps.
    Last edited by creation; 23rd July 2013 at 10:35.

Similar Threads

  1. QtQuick structure
    By codeman in forum Qt Quick
    Replies: 4
    Last Post: 21st May 2013, 10:35
  2. QtQuick 2.1
    By shadowroot in forum Qt Quick
    Replies: 1
    Last Post: 6th April 2013, 08:44
  3. What is QtQuick/QML really for?
    By Huk in forum Qt Quick
    Replies: 7
    Last Post: 21st June 2012, 20:33
  4. Designer or QtQuick?
    By Jeffb in forum Qt Quick
    Replies: 5
    Last Post: 9th August 2011, 06:38
  5. Using a common datamodel across dialogs?
    By Frozenjim in forum Qt Programming
    Replies: 1
    Last Post: 14th October 2008, 00:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.