I was playing around with QSqlQueryModel but I am completely stuck right now.
I've searched for a solution the entire day but no luck so far.
What I do got working is that it pulls data from my sqlite database BUT for some reason I can not show it in my listview. It looks like my rolenames do not exist.
I get messages like....ReferenceError: id is not defined..... for each row that I pull out of the database.
I've used an example from: http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML
I've tried both examples but I always get the same problem.
My ccp file looks like this...
#include "artistssqlmodel.h"
const char* ArtistsSqlModel::COLUMN_NAMES[] = {
"id",
"word",
NULL
};
const char* ArtistsSqlModel::SQL_SELECT = "SELECT id, word FROM dictionary LIMIT 5";
ArtistsSqlModel::ArtistsSqlModel(QObject *parent) :
QSqlQueryModel(parent)
{
mydb=QSqlDatabase::addDatabase("QSQLITE");
QString dbPath = "E://mydb.db";
mydb.setDatabaseName(dbPath);
connectToDb();
int idx = 0;
QHash<int, QByteArray> roleNames;
while( COLUMN_NAMES[idx]) {
roleNames[Qt::UserRole + idx + 1] = COLUMN_NAMES[idx];
idx++;
}
//roleNames(roleNames);
refresh();
}
void ArtistsSqlModel::connectToDb()
{
//QString bla = "Default";
if(!mydb.open())
{
qDebug() << "Database didnt open";
}
else
{
qDebug() << "Your database is open";
}
}
void ArtistsSqlModel::refresh()
{
this->setQuery(SQL_SELECT);
}
QVariant ArtistsSqlModel::data(const QModelIndex &index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if(role < Qt::UserRole)
{
value = QSqlQueryModel::data(index, role);
}
else
{
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
return value;
}
and my qml file looks like this
import QtQuick 2.2
import QtQuick.Window 2.1
Window {
visible: true
width: 800
height: 800
ListView{
anchors.fill:parent
model:artistsModel
delegate: Item{
width:parent.width
height: width/10
Text {
id: name
text: word
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
anchors.fill:parent
}
}
}
}
I hope I provided enough info and I hope somebody knows what I am doing wrong. I'm new with qt and c++ but normally I can solve the issues with some research.
This time I am completely stuck and I need to figure out a way to get the data from my sqlite db into my listsviews.
EDIT:
Thanks for the reply here is all the rest of the code (these are the files I've got)
The artistssqlmodel.h file
#ifndef ARTISTSSQLMODEL_H
#define ARTISTSSQLMODEL_H
#include <QObject>
#include <QSqlQueryModel>
#include <QDebug>
class ArtistsSqlModel : public QSqlQueryModel
{
Q_OBJECT
public:
explicit ArtistsSqlModel(QObject *parent);
void refresh();
QVariant data(const QModelIndex &index, int role) const;
void connectToDb();
signals:
public slots:
private:
const static char* COLUMN_NAMES[];
const static char* SQL_SELECT;
QSqlDatabase mydb;
};
#endif // ARTISTSSQLMODEL_H
and the main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "artistssqlmodel.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlContext *context = engine.rootContext();
ArtistsSqlModel *artistsSqlModel = new ArtistsSqlModel( qApp);
context->setContextProperty("artistsModel", artistsSqlModel);
engine.load(QUrl(QStringLiteral("qrc:///qml/main.qml")));
return app.exec();
}
It looks like my rolenames do not exist
Exactly. You doesn't define the rolenames where QML knows to look for. The wiki where you got the example is out dated. The "new" way to expose the rolenames for a model is reimplementing the QAbstractItemModel::rolenames() method. Move the rolenames definition to it:
QHash<int, QByteArray> ArtistsSqlModel::rolenames() const
{
int idx = 0;
QHash<int, QByteArray> roleNames;
while( COLUMN_NAMES[idx]) {
roleNames[Qt::UserRole + idx + 1] = COLUMN_NAMES[idx];
idx++;
}
return roleNames;
}
By the way, I suggest you to use the new Qt documentation, if you are not using yet. I think is much clear and organized than the old one.
I hope this help you.
Related
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.
I am building an application to create and edit gpx files on a map. I want to be able to render a line and set of markers from a single model.
Each GPX point has a co-ordinate, elevation and a time. I am aiming to create a model that can be used to show it on the map and also show the elevation profile.
I have modified the answer to this question QT QmlMap PolyLine not updated properly when underlying model changes in order to base my model on a structure of GPX points.
main.c
#include "mainwindow.h"
#include <QApplication>
#include <QQmlApplicationEngine>
#include "mapmarker.h"
#include <QQmlContext>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
//Added for model registration
QQmlApplicationEngine engine;
qmlRegisterType<MarkerModel>("MarkerModel", 1, 0, "MarkerModel");
engine.load(QUrl(QStringLiteral("qrc:/mainWindow.qml")));
return app.exec();
}
mainWindow.qml
import QtQuick 2.3
import QtQuick.Window 2.3
import QtLocation 5.9
import MarkerModel 1.0
ApplicationWindow {
id: appWindow
width: 1512
height: 1512
visible: true
x:100
y:100
MarkerModel{
id: markerModel
}
Plugin {
id: mapPlugin
name: "osm"
}
Map {
id: mapView
anchors.fill: parent
plugin: mapPlugin
MapItemView {
model: markerModel
delegate: routeMarkers
}
MapPolyline {
line.width: 5
path: markerModel.path
}
Component {
id: routeMarkers
MapCircle {
radius: 10
center: positionRole
}
}
MouseArea {
anchors.fill: parent
onClicked: {
var coord = parent.toCoordinate(Qt.point(mouse.x,mouse.y))
markerModel.addMarker(coord)
}
}
}
}
mapmarker.h
#ifndef MAPMARKER_H
#define MAPMARKER_H
#include <QAbstractListModel>
#include <QGeoCoordinate>
#include <QDebug>
#include <QDate>
struct gpxCoordinate {
QGeoCoordinate latlon;
float ele;
QDateTime time;
};
class MarkerModel : public QAbstractListModel {
Q_OBJECT
Q_PROPERTY(QVariantList path READ path NOTIFY pathChanged)
public:
enum MarkerRoles{positionRole = Qt::UserRole, pathRole};
MarkerModel(QObject *parent=nullptr): QAbstractListModel(parent)
{
connect(this, &QAbstractListModel::rowsInserted, this, &MarkerModel::pathChanged);
connect(this, &QAbstractListModel::rowsRemoved, this, &MarkerModel::pathChanged);
connect(this, &QAbstractListModel::dataChanged, this, &MarkerModel::pathChanged);
connect(this, &QAbstractListModel::modelReset, this, &MarkerModel::pathChanged);
connect(this, &QAbstractListModel::rowsMoved, this, &MarkerModel::pathChanged);
}
Q_INVOKABLE void addMarker(const QGeoCoordinate &coordinate, float elevation = 0, QDateTime dateTime = QDateTime::currentDateTime()) {
gpxCoordinate item;
item.latlon = coordinate;
item.ele = elevation;
item.time = dateTime;
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_coordinates.append(item);
endInsertRows();
}
int rowCount(const QModelIndex &parent = QModelIndex()) const override {
if(parent.isValid()) return 0;
return m_coordinates.count();
}
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override {
if(row + count > m_coordinates.count() || row < 0)
return false;
beginRemoveRows(parent, row, row+count-1);
for(int i = 0; i < count; ++i)
m_coordinates.removeAt(row + i);
endRemoveRows();
return true;
}
bool removeRow(int row, const QModelIndex &parent = QModelIndex()) {
return removeRows(row, 1, parent);
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override {
if (index.row() < 0 || index.row() >= m_coordinates.count())
return QVariant();
if(role == Qt::DisplayRole)
return QVariant::fromValue(index.row());
else if(role == MarkerModel::positionRole){
return QVariant::fromValue(m_coordinates[index.row()].latlon);
}
return QVariant();
}
QHash<int, QByteArray> roleNames() const override{
QHash<int, QByteArray> roles;
roles[positionRole] = "positionRole";
return roles;
}
QVariantList path() const{
QVariantList path;
for(const gpxCoordinate & coord: m_coordinates){
path << QVariant::fromValue(coord.latlon);
}
return path;
}
signals:
void pathChanged();
private:
QVector<gpxCoordinate> m_coordinates;
};
#endif // MARKERMODEL_H
I think I have made a really basic mistake somewhere as I can click on the map and draw a polyline but the MapCircles are not rendering. I see the error:-
Unable to assign [undefined] to QGeoCoordinate
When I first click on the map.
Have I misunderstood how models/roles work in Qt QML?
I have tracked this down to a build issue. I had some additional paths in my .pro file and was including some libraries that were not being used (spatialite) removing these fixed the issue completely.
I will leave the question up as it may be useful to anyone wanting to do something similar.
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 have a QML TreeView that gets data through a QStandardItemModel. When the application is running, I press a button which adds a new entry. I know the data is changing, but the QML TreeView is not updating. I've also tried beginResetModel() and endResetModel(). The data is correctly displayed in the TreeView upon loading the application, but the TreeView does not change when modifying the data in the model.
treeviewmodel.cpp
#include <QDebug>
#include <QStandardItemModel>
#include "treeviewmodel.h"
TreeViewModel::TreeViewModel(QObject *parent) :
QStandardItemModel(parent)
{
m_roleNameMapping[TreeViewModel_Role_Name] = "name_role";
QStandardItem* entry;
entry = new QStandardItem(QString("my_entry"));
entry->setData("abc", TreeViewModel_Role_Name);
auto childEntry = new QStandardItem( "my_child_entry" );
childEntry->setData( "def",TreeViewModel_Role_Name);
entry->appendRow(childEntry);
appendRow( entry );
}
TreeViewModel& TreeViewModel::Instance()
{
static TreeViewModel instance; //Guaranteed to be destroyed
return instance;
}
void TreeViewModel::addEntry()
{
qDebug () << "Adding entry...";
QStandardItem* entry;
entry = new QStandardItem(QString("my_entry"));
entry->setData("Second Entry", TreeViewModel_Role_Name);
auto childEntry = new QStandardItem( "my_child_entry" );
childEntry->setData( "Second Entry Child",TreeViewModel_Role_Name);
entry->appendRow(childEntry);
appendRow( entry );
qDebug () << rowCount(); //Increases everytime I call the function
//Data is being added
}
QHash<int, QByteArray> TreeViewModel::roleNames() const
{
return m_roleNameMapping;
}
main.qml
import treeModel 1.0
...
MyTreeModel {
id: theModel
}
//Left Tree View
Rectangle {
id: leftView
Layout.minimumWidth: 50
width: 200
//Layout.fillWidth: true
color: "white"
TreeView {
id: treeView
anchors.fill: parent
model: theModel
TableViewColumn {
role: "name_role"
title: "Name"
}
TableViewColumn {
role: "description_role"
title: "Description"
}
}
}
ToolButton {
iconSource: "lock.png"
onClicked: {
treeviewmodel.addEntry()
}
}
main.cpp
QQmlContext* treeViewModelCtx = engine.rootContext();
treeViewModelCtx->setContextProperty("treeviewmodel", &TreeViewModel::Instance());
//Register types
qmlRegisterType<TreeViewModel>("treeModel", 1, 0, "MyTreeModel" );
I hope this example helps. Unfortunately, I don't have your whole code to see where the problem is.
Maybe the keys are the data and roleNames methods.
In this example, we also have a button called addButton to add new items.
main.cpp
#include "animalmodel.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <qqmlcontext.h>
#include <qqml.h>
int main(int argc, char ** argv)
{
QGuiApplication app(argc, argv);
AnimalModel model;
model.addAnimal("Wolf", "Medium");
model.addAnimal("Polar bear", "Large");
model.addAnimal("Quoll", "Small");
QQmlApplicationEngine engine;
QQmlContext *ctxt = engine.rootContext();
ctxt->setContextProperty("myModel", &model);
engine.load(QUrl(QStringLiteral("qrc:/view.qml")));
return app.exec();
}
animalmodel.h
#ifndef ANIMALMODEL_H
#define ANIMALMODEL_H
#include <QStandardItemModel>
class AnimalModel : public QStandardItemModel
{
Q_OBJECT
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;
}