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.
Code:
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#inclide <QNetworkAccessManager>
#include "mymodel.h"
{
Q_OBJECT
public:
explicit MyClass
(QObject *parent
= 0);
void getData();
signals:
public slots:
void dataLoaded();
private:
MyModel *myModelVariable;
QNetworkAccessManager *manager;
};
#endif // MYCLASS_H
Code:
#include "myclass.h"
MyClass
::MyClass(QObject *parent
) :{
myModelVariable = new MyModel;
manager = new QNetworkAccessManager;
}
void MyClass::getData()
{
QNetworkReply *reply = manager->get("https://localhost/content/");
connect(reply, SIGNAL(finished()), this, SLOT(dataLoaded()));
}
void MyClass::dataLoaded()
{
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
if (!reply) {
return;
}
else
{
if(reply->error())
{
qDebug() << "error: " << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString();
}
else
{
qDebug() << result;
bool parsingOk;
QVariantList contentList = Json::parse(result, parsingOk).toList();
QVariantMap singleContentMap;
if(parsingOk)
{
myModelVariable->setList(contentList);
//how to update qml listview here?
}
}
reply->deleteLater();
}
}
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.
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
and have a method that returns the model
Code:
{
return myModelVariable;
}
Cheers,
_
Re: QtQuick and C++ datamodel
Quote:
Originally Posted by
anda_skoa
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
and have a method that returns the model
Code:
{
return myModelVariable;
}
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?
Re: QtQuick and C++ datamodel
That should happen automatically whenever the model updates. Does your model emit the correct signals?
Cheers,
_
Re: QtQuick and C++ datamodel
Quote:
Originally Posted by
anda_skoa
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.
Code:
#include "mymodel.h"
#include <QDebug>
MyModel
::MyModel(QObject *parent
) :{
roles.
insert(DatelineRole,
QByteArray("dateline"));
}
void MyModel::setList(QVariantList myList)
{
this->myList = myList;
// should I emit signal here?
}
{
return myList.count();
}
QHash<int, QByteArray> MyModel::roleNames() const
{
return roles;
}
{
if (!index.isValid())
return QVariant();
// Return Null variant if index is invalid if (index.row() > (hotThreadList.count()-1) )
QVariantMap itemMap = myList.at(index.row()).toMap();
switch (role)
{
case DatelineRole:
return itemMap.value("dateline").toInt();
case NameRole:
return itemMap.value("name").toString();
case TitleRole:
return itemMap.value("title").toString();
default:
}
}
Re: QtQuick and C++ datamodel
Quote:
Originally Posted by
creation
where should I emit signal?
Whenever you change the data on which the model operates
Quote:
Originally Posted by
creation
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
No, the ListView connects to the model internally, nothing to do on the outside.
Cheers,
_