Results 1 to 6 of 6

Thread: QtQuick and C++ datamodel

  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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtQuick and C++ datamodel

    You can either set the model as a context property or the instance of MyClass and make the model acessible as a Q_PROPERTY.

    The latter is probably the thing you want todo, should be as easy as
    Qt Code:
    1. Q_PROPERTY(QAbstractItemModel* model READ model CONSTANT)
    To copy to clipboard, switch view to plain text mode 
    and have a method that returns the model

    Qt Code:
    1. QAbstractItemModel *MyClass::model() const
    2. {
    3. return myModelVariable;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    creation (28th July 2013)

  4. #3
    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 Re: QtQuick and C++ datamodel

    Quote Originally Posted by anda_skoa View Post
    You can either set the model as a context property or the instance of MyClass and make the model acessible as a Q_PROPERTY.

    The latter is probably the thing you want todo, should be as easy as
    Qt Code:
    1. Q_PROPERTY(QAbstractItemModel* model READ model CONSTANT)
    To copy to clipboard, switch view to plain text mode 
    and have a method that returns the model

    Qt Code:
    1. QAbstractItemModel *MyClass::model() const
    2. {
    3. return myModelVariable;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    hi anda_skoa, thanks for your reply.
    The model was binding with qml, but I have a few problems here.

    If myModelVariable was not initiated with at least one item of data, then ListView on the qml doesn't exist and whenever function getData() was called, it didn't show anything.

    But if myModelVariable was initiated, then to update existing data in qml function getData() was called, I must do something, for example click on the ListView on the qml.
    If I didn't do that, it's still old data shown there.
    Is there any way to update it automatically, maybe add signal in my c++ code?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtQuick and C++ datamodel

    That should happen automatically whenever the model updates. Does your model emit the correct signals?

    Cheers,
    _

  6. #5
    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 Re: QtQuick and C++ datamodel

    Quote Originally Posted by anda_skoa View Post
    That should happen automatically whenever the model updates. Does your model emit the correct signals?

    Cheers,
    _
    where should I emit signal?
    what signal should I emit?
    If I read the documentation, there is a signal from QAbstractListModel class named dataChanged.
    But it looks like it only update for one item only, not all items in list.

    Or do I need to create Connections in qml file like what I found here Signal Slot connection with QML and Qt C++ code ?

    this is my model.

    Qt Code:
    1. #include "mymodel.h"
    2. #include <QDebug>
    3.  
    4. MyModel::MyModel(QObject *parent) :
    5. {
    6. roles = QAbstractListModel::roleNames();
    7. roles.insert(DatelineRole, QByteArray("dateline"));
    8. roles.insert(NameRole, QByteArray("name"));
    9. roles.insert(TitleRole, QByteArray("title"));
    10. }
    11.  
    12. void MyModel::setList(QVariantList myList)
    13. {
    14. this->myList = myList;
    15. // should I emit signal here?
    16. }
    17.  
    18. int MyModel::rowCount(const QModelIndex &parent) const
    19. {
    20. return myList.count();
    21. }
    22.  
    23. QHash<int, QByteArray> MyModel::roleNames() const
    24. {
    25. return roles;
    26. }
    27.  
    28. QVariant MyModel::data(const QModelIndex &index, int role) const
    29. {
    30. if (!index.isValid())
    31. return QVariant(); // Return Null variant if index is invalid
    32. if (index.row() > (hotThreadList.count()-1) )
    33. return QVariant();
    34.  
    35. QVariantMap itemMap = myList.at(index.row()).toMap();
    36.  
    37. switch (role)
    38. {
    39. case DatelineRole:
    40. return itemMap.value("dateline").toInt();
    41. case NameRole:
    42. return itemMap.value("name").toString();
    43. case TitleRole:
    44. return itemMap.value("title").toString();
    45. default:
    46. return QVariant();
    47. }
    48. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QtQuick and C++ datamodel

    Quote Originally Posted by creation View Post
    where should I emit signal?
    Whenever you change the data on which the model operates

    Quote Originally Posted by creation View Post
    what signal should I emit?
    Depends on what kind of change you are affecting. if the data value on a certain position changes then dataChanged() is the one to emit. if you are adding rows then you usually call beginInsertRows and endInsertRows which in turn will take care of emitting the right signals.

    In your case, when you completely replace the data, it might be the most effecitve thing to just call reset().

    Quote Originally Posted by creation View Post
    Or do I need to create Connections in qml file like what I found here Signal Slot connection with QML and Qt C++ code ?
    No, the ListView connects to the model internally, nothing to do on the outside.

    Cheers,
    _

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.