Is it possible to have a model within a model accessible via qml and each model are different classes
eg modelclass 1 has a vector of a subclass of model
i.e parent model can have many sub model
I tried to return a QAbstractItemModel
as a role of QAbstractItemModel
parent but it seems thats not possible any ideas of how to do these stuff??
[Edit 1]
The code :
message.h
#include <QAbstractListModel>
#include <QStringList>
//![0]
class Animal
{
public:
Animal(const QString &type, const QString &size);
//![0]
QString type() const;
QString size() const;
private:
QString m_type;
QString m_size;
//![1]
};
class AnimalModel : public QAbstractListModel
{
Q_OBJECT
public:
enum AnimalRoles {
TypeRole = Qt::UserRole + 1,
SizeRole
};
AnimalModel(QObject *parent = 0);
//![1]
void addAnimal(const Animal &animal);
int rowCount(const QModelIndex & parent = QModelIndex()) const;
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole)
const;
protected:
QHash<int, QByteArray> roleNames() const;
private:
QList<Animal> m_animals;
//![2]
};
//![2]
model.h
#include <QAbstractListModel>
#include <QStringList>
#include <QDebug>
#include <message.h>
//![0]
//!
//!
class lister{
public:
int id;
AnimalModel *list=nullptr;
void addlist(){
list->addAnimal(Animal("Wolf", "Medium"));
list->addAnimal(Animal("Polar bear", "Large"));
list->addAnimal(Animal("Quoll", "Small"));
}
};
class TestModel : public QAbstractListModel
{
public:
enum EventRoles {
ModelRole = Qt::UserRole + 1,
IdRole = Qt::UserRole + 2
};
TestModel()
{
m_roles[ ModelRole] = "mm";
m_roles[ IdRole] = "id";
// setRoleNames(m_roles);
}
int rowCount(const QModelIndex & = QModelIndex()) const
{
return mylist.count();
}
QVariant data(const QModelIndex &index, int role) const
{
if(role == IdRole)
{
return mylist.at(index.row()).id;
}
else if(role == ModelRole)
{
return QVariant::fromValue(mylist.at(index.row()).list);
}
return 0;
}
void addData(){
static int a=0;
qDebug()<<a<<"value";
beginInsertRows(QModelIndex(), rowCount(), rowCount());
lister temp;
temp.id=a;
temp.addlist();
a++;
mylist<<temp;
temp.id=a;
temp.addlist();
a++;
mylist<<temp;
endInsertRows();
}
QHash<int, QByteArray> roleNames() const {
QHash<int, QByteArray> roles;
roles[ModelRole] = "mm";
roles[IdRole] = "id";
return roles;
}
QList<lister> mylist;
QHash<int, QByteArray> m_roles;
};
message.cpp
#include "message.h"
Animal::Animal(const QString &type, const QString &size)
: m_type(type), m_size(size)
{
}
QString Animal::type() const
{
return m_type;
}
QString Animal::size() const
{
return m_size;
}
AnimalModel::AnimalModel(QObject *parent)
: QAbstractListModel(parent)
{
}
void AnimalModel::addAnimal(const Animal &animal)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_animals << animal;
endInsertRows();
}
int AnimalModel::rowCount(const QModelIndex & parent) const {
Q_UNUSED(parent);
return m_animals.count();
}
QVariant AnimalModel::data(const QModelIndex & index, int role) const {
if (index.row() < 0 || index.row() >= m_animals.count())
return QVariant();
const Animal &animal = m_animals[index.row()];
if (role == TypeRole)
return animal.type();
else if (role == SizeRole)
return animal.size();
return QVariant();
}
//![0]
QHash<int, QByteArray> AnimalModel::roleNames() const {
QHash<int, QByteArray> roles;
roles[TypeRole] = "type";
roles[SizeRole] = "size";
return roles;
}
//![0]
main.cpp
#include "model.h"
#include <QGuiApplication>
#include <qqmlengine.h>
#include <qqmlcontext.h>
#include <qqml.h>
#include <QtQuick/qquickitem.h>
#include <QtQuick/qquickview.h>
//![0]
int main(int argc, char ** argv)
{
QGuiApplication app(argc, argv);
TestModel model;
// model.addAnimal(Animal("Wolf", "Medium"));
// model.addAnimal(Animal("Polar bear", "Large"));
// model.addAnimal(Animal("Quoll", "Small"));
model.addData();
model.addData();
model.addData();
qRegisterMetaType<AnimalModel*>("AnimalModel*" );
QQuickView view;
view.setResizeMode(QQuickView::SizeRootObjectToView);
QQmlContext *ctxt = view.rootContext();
ctxt->setContextProperty("myModel", &model);
//![0]
view.setSource(QUrl("qrc:view.qml"));
view.show();
return app.exec();
}
view.qml
import QtQuick 2.0
import QtQml 2.0
ListView {
anchors.fill: parent
model: myModel //this is your main model
delegate:
Rectangle {
height: 100
width: 100
color: "red"
Text {
id: cc
text: id
}
ListView {
anchors.fill: parent
model: mm //the internal QVariantList
delegate: Rectangle {
anchors.right: parent.right
width: 50
height: 50
color: "green"
border.color: "black"
Text {
text: type //role to get data from internal model
}
}
}
}
}