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.
#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
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QObject>
#inclide <QNetworkAccessManager>
#include "mymodel.h"
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject *parent = 0);
void getData();
signals:
public slots:
void dataLoaded();
private:
MyModel *myModelVariable;
QNetworkAccessManager *manager;
};
#endif // MYCLASS_H
To copy to clipboard, switch view to plain text mode
#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();
}
}
#include "myclass.h"
MyClass::MyClass(QObject *parent) :
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
{
QByteArray result = reply->readAll();
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();
}
}
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.
Bookmarks