I have class, which reads data from file to 2D array.
So i need to display that array in qml TableView.
I have QVector<QVector> table; to display it as data in my TableModel.
The OperatingFiles object creates in main.cpp it contains functions to encode/decode passwords and save them to file. Functions for this object is also called from qml code
So what i want is to make "table = passwordsDecoded" somewhere but i don't know how to do it.
OperatingFiles.h :
class OperatingFiles : public QObject
{
Q_OBJECT
public:
OperatingFiles();
public slots:
QVector<QVector<QString>> getVector(); // returns passwordsDecoded
private:
QVector<QVector<QString>> passwordsDecoded;
};
#endif // OPERATINGFILES_H
TableModel.h:
class TableModel : public QAbstractTableModel
{
Q_OBJECT
QVector<QVector<QString>> table;
public:
explicit TableModel(QObject *parent = nullptr);
int rowCount(const QModelIndex & = QModelIndex()) const override;
int columnCount(const QModelIndex & = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role) const override;
QHash<int, QByteArray> roleNames() const override;
};
TableModel.cpp:
#include "tablemodel.h"
TableModel::TableModel(QObject *parent)
: QAbstractTableModel{parent}
{
QVector<QString> mini; // this only for test that it really appears in TableView (it appears)
mini.append("rstrst");
mini.append("rstrst");
mini.append("rstrst");
table.append(mini);
table.append(mini);
table.append(mini);
}
int TableModel::rowCount(const QModelIndex &) const
{
return table.size();
}
int TableModel::columnCount(const QModelIndex &) const
{
return table.at(0).size();
}
QVariant TableModel::data(const QModelIndex &index, int role) const
{
switch (role) {
case Qt::DisplayRole:
return table.at(index.row()).at(index.column());
default:
break;
}
return QVariant();
}
QHash<int, QByteArray> TableModel::roleNames() const
{
return { {Qt::DisplayRole, "display"} };
}
qml:
TableView {
anchors.fill: parent
columnSpacing: 1
rowSpacing: 1
clip: true
model: TableModel{}
delegate: Rectangle {
implicitWidth: 100
implicitHeight: 50
border.width: 0
Text {
text: display
anchors.centerIn: parent
}
}
}
main.cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "controlbuttons.h"
#include "operatingfiles.h"
#include "tablemodel.h"
int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
qmlRegisterType<TableModel>("TableModel", 0,1,"TableModel");
QQmlApplicationEngine engine;
ControlButtons *appManager = new ControlButtons(&app);
engine.rootContext()->setContextProperty("appManager", appManager);
OperatingFiles *fileOperator = new OperatingFiles();
engine.rootContext() -> setContextProperty("fileOperator", fileOperator);
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
I tried to make functions in TableModel that could fill "table" with data from my object which was called directly from qml ("OnClicked:"), tried make constructor which will get object with 2D array.
I seen ton of vids and read docs but literally no idea how to do it.
So whole chain is: choose file✓ -> read file✓ -> decode✓ -> fill 2D array✓ -> send to model (Somehow) -> appear it in UI✓
Maybe it could be done if i make my 2D array global so i could access to it from anywhere but it not a solution.
Thanks!
The easiest modification would be to add a Q_INVOKABLE to your TableModel which allows to set the table
class TableModel : public QAbstractTableModel
{
...
Q_INVOKABLE void setTable(const QVariant& value)
{ //inline for briefity
beginResetModel();
table = value.value<QVector<QVector<QString>>>();
endResetModel();
}
}
Then from QML you can do <id of TableModel>.setTable(fileOperator.getVector())
Another option would be a Q_PROPERTY with a similar setter method. Then you would use <id of TableModel>.table = fileOperator.getVector()
I'm not sure about how you want your structure to look like, but if you don't need TableModel to be instantiatable from QML you could make it available as contextProperty the same way as you did with the fileOperator. Then you will have more control on the TableModel, for instance, you can call a fill function from C++ side when done.
Related
I'm building GUI to control my ADC, connected to the STM32 board. Communications are handled by UART. Everything works fine. The problem is to properly handling my custom channel structure as a model object to be able to present data in different ways depending on the delegate object.
I have tried a few different solutions, none of them works. The case looks trivial however is a little different than presented examples on the forum or in tutorials on the web because I'm trying to update the data model from c++ side, not from qml. That forces me to invoke setData() override method to be invoked on c++ side and propagate dataChange() signal into qml. Let's present some code.
That's how the app presents the data models after initialization. This data are provided in SensorList constructor. No matter what I tried, I can not change those values on qml side. Even when I confirmed using debugger that vector of my channels in sensorList is changing when data are received from sensor. This looks like a problem with communication between C++ and QML.
My data handler.
ADS8588h.h
#ifndef CHANNEL_H
#define CHANNEL_H
#include <QObject>
#include <QString>
class AdsChannel {
Q_GADGET
Q_PROPERTY(QString name READ name )
Q_PROPERTY(double value READ value )
public:
AdsChannel(const QString& channelName, const double value)
: mName{channelName}, mValue{value} {}
AdsChannel() = default;
AdsChannel(const AdsChannel& rhs) = default;
AdsChannel& operator=(const AdsChannel& rhs) = default;
void setName(const QString& name) { mName = name;}
void setValue(const double value) { mValue = value; }
const QString name() const { return mName; }
double value() const { return mValue; }
private:
QString mName;
double mValue;
};
Q_DECLARE_METATYPE(AdsChannel);
#endif // CHANNEL_H
SensorList.h which collects list of channels
#ifndef SENSORLIST_H
#define SENSORLIST_H
#include "AdsChannel.h"
class SensorList : public QObject
{
Q_OBJECT
public:
explicit SensorList(QObject *parent = nullptr)
{
mItems.push_back(AdsChannel("CH1",5.0));
mItems.push_back(AdsChannel("CH2",55.0));
mItems.push_back(AdsChannel("CH3",0.0));
mItems.push_back(AdsChannel("CH4",0.0));
mItems.push_back(AdsChannel("CH5",0.0));
mItems.push_back(AdsChannel("CH6",0.0));
mItems.push_back(AdsChannel("CH7",0.0));
mItems.push_back(AdsChannel("CH8",0.0));
}
QVector<AdsChannel> items() const
{
return mItems;
}
bool setItemAt(int index, const AdsChannel &item)
{
if (index < 0 || index >= mItems.size())
return false;
const AdsChannel &oldItem = mItems.at(index);
if (item.name() == oldItem.name() && item.value() == oldItem.value())
return false;
mItems[index] = item;
return true;
}
private:
QVector<AdsChannel> mItems;
};
#endif // SENSORLIST_H
SensorDataModel.h
#ifndef SENSORDATAMODEL_H
#define SENSORDATAMODEL_H
#include <QAbstractListModel>
#include <QStandardItemModel>
#include "AdsChannel.h"
#include "SensorList.h"
class SensorList;
class SensorDataModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(SensorList *list READ list WRITE setList)
public:
explicit SensorDataModel(QObject *parent = nullptr);
enum {
NameRole = Qt::UserRole +1 ,
ValueRole
};
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = ValueRole) const override;
// Editable:
bool setData(const QModelIndex &index, const QVariant &value,
const int role = ValueRole) override;
Qt::ItemFlags flags(const QModelIndex& index) const override;
virtual QHash<int, QByteArray> roleNames() const override;
SensorList *list() const;
void setList(SensorList *list);
private:
SensorList *mList;
};
#endif // SENSORDATAMODEL_H
SensorDataModel.cpp
#include "SensorDataModel.h"
#include "SensorList.h"
SensorDataModel::SensorDataModel(QObject *parent)
: QAbstractListModel(parent)
, mList(new SensorList)
{
}
int SensorDataModel::rowCount(const QModelIndex &parent) const
{
// For list models only the root node (an invalid parent) should return the list's size. For all
// other (valid) parents, rowCount() should return 0 so that it does not become a tree model.
if (parent.isValid() || !mList)
return 0;
return mList->items().size();
}
QVariant SensorDataModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || !mList)
return QVariant();
const AdsChannel item = mList->items().at(index.row());
switch (role) {
case NameRole:
return QVariant(item.name());
case ValueRole:
return QVariant(item.value());
}
return QVariant();
}
bool SensorDataModel::setData(const QModelIndex &indexModel, const QVariant &value,const int role)
{
// indexModel is empty as this method is invocable from c++ side. Index which is given in dataChange() signal is calculated
// based on index data which are updated
//When data arrived from a sensor this how this method is invoked.
//mData.setData(QModelIndex(),QVariant::fromValue<AdsChannel>(temp),SensorDataModel::ValueRole);
if (!mList)
return false;
auto newElement = value.value<AdsChannel>();
const auto listElements = mList->items();
AdsChannel item;
int indexFinder = 0;
for(indexFinder ; indexFinder < rowCount(); ++indexFinder)
{
if(newElement.name() == listElements.at(indexFinder).name()) {
item = listElements.at(indexFinder);
break;
}
}
switch (role) {
case NameRole:
item.setName(newElement.name());
break;
case ValueRole:
item.setValue(newElement.value());
break;
}
if (mList->setItemAt(indexFinder, item)) {
emit dataChanged (this->index(indexFinder,0), this->index(indexFinder,0), QVector<int>() << role);
//This signal is emited but never reached on QML side
return true;
}
return false;
}
Qt::ItemFlags SensorDataModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::NoItemFlags;
return Qt::ItemIsEditable;
}
QHash<int, QByteArray> SensorDataModel::roleNames() const
{
QHash<int, QByteArray> names;
names[NameRole] = "channelName";
names[ValueRole] = "value";
return names;
}
SensorList *SensorDataModel::list() const
{
return mList;
}
void SensorDataModel::setList(SensorList *list)
{
// I never use a setList() as my list is initialized throw constructor.
beginResetModel();
if (mList)
mList->disconnect(this);
mList = list;
endResetModel();
}
main.cpp
#include "ads8588h.h"
#include "SensorList.h"
#include "SensorDataModel.h"
int main(int argc, char *argv[]) {
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
ADS8588H driver ;
qmlRegisterType<ADS8588H>("AdsDriver", 1, 0, "AdsDriver"); // Used to some other features
qmlRegisterType<SensorDataModel>("SensorModel", 1, 0, "SensorDataModel");
qmlRegisterUncreatableType<SensorList>("SensorModel", 1, 0, "SensorList",
QStringLiteral("SensorList should not be created in QML"));
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty(QStringLiteral("sensorList"), driver.getSensor()->list());
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreated, &app,
[url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl) QCoreApplication::exit(-1);
},
Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
QML code using simple TEXT label to eliminate some potential bug using CANVAS, requestPaint() signals and other.
import QtQuick 2.0
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.3
import SensorModel 1.0
Item {
id:root
anchors.fill: parent
ListView {
implicitWidth: root.width * 0.8
implicitHeight: width
x:root.width/2 - width/2
y:170
id: customList
focus: true
spacing: 4
snapMode: ListView.SnapOneItem
highlightRangeMode: ListView.StrictlyEnforceRange
highlightMoveDuration: 250
orientation: ListView.Vertical
boundsBehavior: Flickable.StopAtBounds
model: SensorDataModel{
list: sensorList
onDataChanged: console.log("ss") //<-- This signal never appear
}
delegate: Item{
width: parent.width
height: width * 0.2
// ChannelProgressBar{
// id:licznik
// anchors.fill: parent
// labelSuffix: suffix
// labelChannel: model.channelName
// progressValue: model.value
// }
Row{
spacing: 20
Text{
id:label1
font.pixelSize: 40
text: model.channelName
}
Text{
id:label2
font.pixelSize: 40
text: model.value
}
}
}
}
}
So my question is What im doing wrong that qml did not updated data even when dataChanged() signal is emitted.
I am trying to expose QList with custom objects (Sample) into QML. Whenever I store those custom objects (they inherits form QObject) into QList<QObject *>, they show up, but without info, but when I try expose them as a QList<Sample *>, they don't.
sample.h
class Sample : public QObject
{
Q_OBJECT
Q_PROPERTY(QString getVar READ getVar WRITE setVar NOTIFY varChanged)
public:
explicit Sample();
//! Returns var
QString getVar() const { return var; }
//! Sets var
void setVar(const QString &a);
signals:
varChanged();
protected:
QString var;
};
Class containing list looks like this
samplecontrol.h
class SampleManager : public QObject
{
Q_OBJECT
Q_PROPERTY(QList<Sample *> getSampleList READ getSampleList NOTIFY sampleListChanged)
public:
SampleManager(const QString &path);
//! Returns sample list
QList<Sample *> getSampleList() const { return sampleList_; }
signals:
sampleListChanged();
protected:
QList<Sample *> sampleList_;
};
I am setting context with
view_->rootContext()->setContextProperty("slabGridModel", QVariant::fromValue(samplecontrol.getSampleList()));
As I said, when i tired
QList<QObject *> datalist;
datalist.append(sampleManager.getSampleList().first());
datalist.append(sampleManager.getSampleList().last());
it worked. How can I make it work with QList<Sample *>?
Thanks for your help!
You can pass a list of QObjects, but the problem is that it will not notify the view if more elements are added, if you want the view to be notified you must use a model that inherits from QAbstractItemModel. On the other hand how are you doing the datamodel as qproperty it is better to expose the SampleManager:
samplemodel.h
#ifndef SAMPLEMODEL_H
#define SAMPLEMODEL_H
#include "sample.h"
#include <QAbstractListModel>
class SampleModel : public QAbstractListModel
{
Q_OBJECT
public:
using QAbstractListModel::QAbstractListModel;
~SampleModel(){
qDeleteAll(mSamples);
mSamples.clear();
}
int rowCount(const QModelIndex &parent = QModelIndex()) const override{
if (parent.isValid())
return 0;
return mSamples.size();
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override{
if (!index.isValid())
return QVariant();
if(role == Qt::UserRole){
Sample *sample = mSamples[index.row()];
return QVariant::fromValue(sample);
}
return QVariant();
}
void appendSample(Sample * sample)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
mSamples << sample;
endInsertRows();
}
QHash<int, QByteArray> roleNames() const{
QHash<int, QByteArray> roles;
roles[Qt::UserRole] = "sample";
return roles;
}
private:
QList<Sample *> mSamples;
};
#endif // SAMPLEMODEL_H
samplemanager.h
#ifndef SAMPLEMANAGER_H
#define SAMPLEMANAGER_H
#include "samplemodel.h"
#include <QObject>
class SampleManager : public QObject
{
Q_OBJECT
Q_PROPERTY(SampleModel* model READ model WRITE setModel NOTIFY modelChanged)
public:
using QObject::QObject;
SampleModel *model() const{
return mModel.get();
}
void setModel(SampleModel *model){
if(mModel.get() == model)
return;
mModel.reset(model);
}
signals:
void modelChanged();
private:
QScopedPointer<SampleModel> mModel;
};
#endif // SAMPLEMANAGER_H
main.cpp
#include "samplemanager.h"
#include "samplemodel.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QTime>
#include <QTimer>
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
SampleManager manager;
manager.setModel(new SampleModel);
// test
QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&manager](){
manager.model()->appendSample(new Sample(QTime::currentTime().toString()));
});
timer.start(1000);
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("manager", &manager);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
GridView {
anchors.fill: parent
model: manager.model
delegate:
Rectangle {
width: 100
height: 100
color: "darkgray"
Text {
text: sample.getVar
anchors.centerIn: parent
}
}
}
}
The complete example can be found in the following link.
I am trying to make the model QAbstractTableModel in cpp and connect to qml.
This code works well.
MyModel.h
#ifndef MYMODEL_H
#define MYMODEL_H
#include <QAbstractTableModel>
class MyModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum AnimalRoles {
TypeRole = Qt::UserRole + 1,
SizeRole
};
explicit MyModel(QObject *parent = nullptr);
// Basic functionality:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
protected:
QHash<int, QByteArray> roleNames() const;
private:
QList<Animal> m_animals;
};
#endif // MYMODEL_H
MyModel.cpp
#include "MyModel.h"
#include <QDebug>
MyModel::MyModel(QObject *parent)
: QAbstractTableModel(parent)
{
qDebug() << __FUNCTION__;
addAnimal(Animal("Wolf", "Medium"));
addAnimal(Animal("Polar bear", "Large"));
addAnimal(Animal("Quoll", "Small"));
}
int MyModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_animals.size();
}
int MyModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 2;
}
QVariant MyModel::data(const QModelIndex &index, int role) const
{
qDebug() << __FUNCTION__ << index.row() << index.column() << role;
if (!index.isValid())
return QVariant();
const Animal &animal = m_animals[index.row()];
switch (role) {
case TypeRole:
return animal.type();
case SizeRole:
return animal.size();
default:
break;
}
return QVariant();
}
void MyModel::addAnimal(const Animal &animal)
{
qDebug() << __FUNCTION__;
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_animals << animal;
endInsertRows();
}
QHash<int, QByteArray> MyModel::roleNames() const
{
qDebug() << __FUNCTION__;
QHash<int, QByteArray> roles;
roles[TypeRole] = "type";
roles[SizeRole] = "size";
return roles;
}
main.cpp
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
MyModel model;
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("myModel", &model);
engine.load(QUrl(QStringLiteral("qrc:/resources/qmls/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
main_test.qml
import QtQuick 2.0
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
Window {
id: main_view
width: 250
height: 600
visible: true
ListView {
id: list_view
width: 200; height: 250
model: myModel
delegate: Text { text: "Animal Test: " + type + ", " + size }
}
TableView {
id: table_view
objectName: "tableView"
width: 250; height: 250
anchors.top: list_view.bottom
model: myModel
TableViewColumn {
id: type_col
role: "type"
title: "Type"
width: 100
}
TableViewColumn {
id: size_col
role: "size"
title: "Size"
width: 100
}
}
}
It looks like this
But, if I change the main.cpp a little bit, the list view dispaly as normal, but not the table view.
main.cpp
#include "MainView.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
MainView mainView;
return app.exec();
}
MainView.h
#ifndef MAINVIEW_H
#define MAINVIEW_H
#include <QObject>
#include <QQmlApplicationEngine>
class MainView: public QObject
{
Q_OBJECT
public:
explicit MainView(QObject *parent=nullptr);
void initializeView();
private:
QQmlApplicationEngine m_engine;
};
#endif // MAINVIEW_H
MainView.cpp
#include "MainView.h"
#include "MyModel.h"
#include <QQmlContext>
MainView::MainView(QObject *parent)
: QObject(parent)
{
initializeView();
}
void MainView::initializeView()
{
MyModel model;
m_engine.rootContext()->setContextProperty("myModel", &model);
m_engine.load(QUrl(QStringLiteral("qrc:/resources/qmls/main_test.qml")));
}
It looks like this.
I really don't understand why this happens. What the difference between ListView and TableView in the second case? And how to fix it, i.e. make data display in the second case? Thank in advance.
The problem is in initializeView, model is a local variable in that scope, and every local variable is deleted from the memory when the function finishes executing. In the first case model will be eliminated when the application is closed, in the second case it will be eliminated when initializeView ends that is when the window is displayed, there are 2 possible solutions:
Create a pointer and to manage the memory we pass to MainView as a parent so that he removes it from memory (this last is a feature of the QObjects):
void MainView::initializeView()
{
MyModel *model = new MyModel(this);
m_engine.rootContext()->setContextProperty("myModel", model);
m_engine.load(QUrl(QStringLiteral("qrc:/resources/qmls/main_test.qml")));
}
Make the model member of the class.
*.h
#ifndef MAINVIEW_H
#define MAINVIEW_H
#include <QObject>
#include <QQmlApplicationEngine>
class MainView: public QObject
{
Q_OBJECT
public:
explicit MainView(QObject *parent=nullptr);
void initializeView();
private:
QQmlApplicationEngine m_engine;
MyModel model{this};
};
#endif // MAINVIEW_H
*.cpp
...
void MainView::initializeView()
{
m_engine.rootContext()->setContextProperty("myModel", &model);
m_engine.load(QUrl(QStringLiteral("qrc:/resources/qmls/main_test.qml")));
}
To understand the behavior I have added more points of impression:
MyModel::~MyModel()
{
qDebug()<<"destructor";
}
TableView {
...
Component.onCompleted: {
console.log("completed table")
if(!timer.running)
timer.running = true
}
}
ListView {
...
Component.onCompleted: {
console.log("completed list")
if(!timer.running)
timer.running = true
}
}
Timer {
id: timer
interval: 0;
onTriggered: console.log(myModel, table_view.model, list_view.model)
}
And I get the following:
MyModel
addAnimal
addAnimal
addAnimal
roleNames
data 0 0 257
data 0 0 258
data 1 0 257
data 1 0 258
data 2 0 257
data 2 0 258
roleNames
qml: completed list
data 0 0 257
data 0 0 258
qml: completed table
destructor
qml: null null null
We note that ListView manages to load the data, whereas TableView in the middle of the load is called the destructor.
Possible explanation: I think that the ListView stores the data making a copy of them and only updates when the model notifies, should also be notified when the model is deleted to clean the data, it seems that this is a bug. On the other hand, TableView, being at the moment of loading and deleting the model, is null, so it is notified and cleans the data.
Doing another test:
void MainView::initializeView()
{
MyModel *model = new MyModel;
m_engine.rootContext()->setContextProperty("myModel", model);
m_engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QTimer::singleShot(1000, model, &MyModel::deleteLater);
}
It is observed that both load the data correctly, and after a second the model is destroyed, but the one that is only notified is the TableView since it is the only one that cleans the data shown, and ListView does not.
my conclusion that it is a ListView bug.
I'm trying to use QML TreeView Model. The example from Qt doesn't include how to create the model. I read this post and tried to use the code from #Tarod but the result is not what I expected.
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "animalmodel.h"
#include <qqmlcontext.h>
#include <qqml.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
AnimalModel model;
model.addAnimal("wolf", "Medium");
model.addAnimal("Bear", "Large");
QQmlApplicationEngine engine;
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty("myModel", &model);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
animalmodel.h
#ifndef ANIMALMODEL_H
#define ANIMALMODEL_H
#include <QStandardItemModel>
class AnimalModel : public QStandardItemModel
{
Q_OBJECT //The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system.
public:
enum AnimalRoles {
TypeRole = Qt::UserRole + 1,
SizeRole
};
AnimalModel(QObject *parent = 0);
Q_INVOKABLE void addAnimal(const QString &type, const QString &size);
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
protected:
QHash<int, QByteArray> roleNames() const;
};
#endif // ANIMALMODEL_H
animalmodel.cpp
#include "animalmodel.h"
AnimalModel::AnimalModel(QObject *parent)
: QStandardItemModel(parent)
{
}
void AnimalModel::addAnimal(const QString &type, const QString &size)
{
QStandardItem* entry = new QStandardItem();
entry->setData(type, TypeRole);
auto childEntry = new QStandardItem();
childEntry->setData(size, SizeRole);
entry->appendRow(childEntry);
appendRow(entry);
}
QVariant AnimalModel::data(const QModelIndex & index, int role) const {
QStandardItem *myItem = itemFromIndex(index);
if (role == TypeRole)
return myItem->data(TypeRole);
else if (role == SizeRole) {
if (myItem->child(0) != 0)
{
return myItem->child(0)->data(SizeRole);
}
}
return QVariant();
}
QHash<int, QByteArray> AnimalModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[TypeRole] = "type";
roles[SizeRole] = "size";
return roles;
}
main.qml
import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
menuBar: MenuBar {
Menu {
title: qsTr("&File")
MenuItem {
text: qsTr("&Open")
onTriggered: messageDialog.show(qsTr("Open Action Triggered"));
}
MenuItem {
text: qsTr("&Exit")
onTriggered: Qt.quit();
}
}
}
TreeView {
anchors.fill: parent
model: myModel
TableViewColumn {
title: "Name"
role: "type"
width: 300
}
TableViewColumn {
title: "Size"
role: "size"
width: 300
}
}
}
What I got is something like this:
Result
What I want to have is the animal size as a child of animal type.
Model sub-classing is one of the worst minefields in Qt. The advice is always to have it go through the model test (https://wiki.qt.io/Model_Test) to see if everything was implemented correctly.
On the other hand, in 90% of the cases you do not need to subclass a model at all as the default models provided by Qt work quite well. What I'd do is just use QStandardItemModel using, on the C++ side, only the QAbstractItemModel interface (i.e. force yourself to use QAbstractItemModel* model = new QStandardItemModel(/*parent*/);) this way, if in the future you feel like you really need to reimplement the model (for efficiency) you'll just need to change 1 line in your existing code.
In your case:
void AnimalModel::addAnimal(const QString &type, const QString &size)
{
if(columnCount()==0) insertColumn(0); // make sure there is at least 1 column
insertRow(rowCount()); // add a row to the root
const QModelIndex addedIdx = index(rowCount()-1,0);
setData(addedIdx, type, TypeRole); // set the root data
insertRow(rowCount(addedIdx),addedIdx ); // add 1 row ...
insertColumn(0,addedIdx ); // ... and 1 column to the added root row
setData(index(0,0,addedIdx), size, SizeRole); // set the data to the child
}
I've figured out how to bind a model derived from QAbstractListModel to a QML view.
But the next thing I tired does not work. If a new Item is added to the model the QML view will not update. Why is that?
DataObject.h
class DataObject {
public:
DataObject(const QString &firstName,
const QString &lastName):
first(firstName),
last(lastName) {}
QString first;
QString last;
};
SimpleListModel.h
class SimpleListModel : public QAbstractListModel
{
Q_OBJECT
enum /*class*/ Roles {
FIRST_NAME = Qt::UserRole,
LAST_NAME
};
public:
SimpleListModel(QObject *parent=0);
QVariant data(const QModelIndex &index, int role) const;
Q_INVOKABLE int rowCount(const QModelIndex &parent = QModelIndex()) const;
QHash<int, QByteArray> roleNames() const;
void addName(QString firstName, QString lastName);
private:
Q_DISABLE_COPY(SimpleListModel);
QList<DataObject*> m_items;
};
SimpleListModel.cpp
SimpleListModel::SimpleListModel(QObject *parent) :
QAbstractListModel(parent)
{
DataObject *first = new DataObject(QString("Firstname01"), QString("Lastname01"));
DataObject *second = new DataObject(QString("Firstname02"), QString("Lastname02"));
DataObject *third = new DataObject(QString("Firstname03"), QString("Lastname03"));
m_items.append(first);
m_items.append(second);
m_items.append(third);
}
QHash<int, QByteArray> SimpleListModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[/*Roles::*/FIRST_NAME] = "firstName";
roles[/*Roles::*/LAST_NAME] = "lastName";
return roles;
}
void SimpleListModel::addName(QString firstName, QString lastName)
{
DataObject *dataObject = new DataObject(firstName, lastName);
m_items.append(dataObject);
emit dataChanged(this->index(m_items.size()), this->index(m_items.size()));
}
int SimpleListModel::rowCount(const QModelIndex &) const
{
return m_items.size();
}
QVariant SimpleListModel::data(const QModelIndex &index, int role) const
{
//--- Return Null variant if index is invalid
if(!index.isValid())
return QVariant();
//--- Check bounds
if(index.row() > (m_items.size() - 1))
return QVariant();
DataObject *dobj = m_items.at(index.row());
switch (role)
{
case /*Roles::*/FIRST_NAME:
return QVariant::fromValue(dobj->first);
case /*Roles::*/LAST_NAME:
return QVariant::fromValue(dobj->last);
default:
return QVariant();
}
}
AppCore.h
class AppCore : public QObject
{
Q_OBJECT
Q_PROPERTY(SimpleListModel *simpleListModel READ simpleListModel CONSTANT)
public:
explicit AppCore(QObject *parent = 0);
SimpleListModel *simpleListModel() const;
public slots:
void addName();
private:
SimpleListModel *m_SimpleListModel;
};
AppCore.cpp
AppCore::AppCore(QObject *parent) :
QObject(parent)
{
m_SimpleListModel = new SimpleListModel(this);
}
SimpleListModel *AppCore::simpleListModel() const
{
return m_SimpleListModel;
}
void AppCore::addName()
{
m_SimpleListModel->addName("FirstnameNEW", "LastnameNEW");
}
main.cpp
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
QQuickView *view = new QQuickView();
AppCore *appCore = new AppCore();
qRegisterMetaType<SimpleListModel *>("SimpleListModel");
view->engine()->rootContext()->setContextProperty("appCore", appCore);
view->setSource(QUrl::fromLocalFile("main.qml"));
view->show();
return a.exec();
}
main.qml
// ...
ListView {
id: myListView
anchors.fill: parent
delegate: myDelegate
model: appCore.simpleListModel
}
MouseArea {
anchors.fill: parent
onClicked: {
appCore.addName()
console.log('rowCount: ' + appCore.simpleListModel.rowCount())
}
}
//...
you should call beginInsertRows and endInsertRows instead of emitting the signal
void SimpleListModel::addName(QString firstName, QString lastName)
{
DataObject *dataObject = new DataObject(firstName, lastName);
// tell QT what you will be doing
beginInsertRows(ModelIndex(),m_items.size(),m_items.size());
// do it
m_items.append(dataObject);
// tell QT you are done
endInsertRows();
}
these 2 functions emit all needed signals
You're ignoring the semantics of a QAbstractItemModel. There are two kinds of signals that a model must emit:
data change signals: They must be emitted after the data was changed. A data change is a change of value of an existing item. Other changes to the model are not called data changes - the terminology here has a specific meaning.
structure change signals: They must be emitted before and after any structural change. A structural change is addition or removal of any of the items. The beginXxxYyy and endXxxYyy helper functions emit those signals.