Two QAbstractListModels in One QAbstracListModel?
-
I have created two different QAbstractListModels and now I want to put those two QAbstractListModels into one QAbstractListModel and be able to access my two QAbstractListModels from my one model in QML. Is this possible or is there a better way to do this?
-
Sure that is possible, but there is no pre-defined way. Create a new subclass of QAbstractListModel on which you can set two (n?) list models, return the total number of rows as your row count. Then, make sure that you connect to the signals of your underlying models to propagate them in the right way. Last, reimplement the data and the flags methods to return the data and the flags from the right row from the right underlying model.
Basically, row r of model 2 becomes row model1.rowCount() + r in your combined model.
-
Thanks for the reply. I now think that this method won't solve my problem. What I really want to do is create a model with a nested model in C++. Something like this.
@ ListModel {
id: fruitModelListElement { name: "Apple" cost: 2.45 attributes: [ ListElement { description: "Core" }, ListElement { description: "Deciduous" } ] }
}
@So far I have this.
fruit.h
@class FruitModel;
class Fruit: public QObject
{
Q_OBJECT
public:
Fruit(const QString& name);
~Fruit();public: Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
QString name() const{return _name;}
void setName(const QString& name);
private: QString _name;
signals: void nameChanged();public: Q_PROPERTY(FruitModel* description READ description NOTIFY descriptionChanged())
FruitModel* description() const {return description;}
private: FruitModel* description;
signals: void descriptionChanged();
};
//_______________________________________________
class FruitModel: public QAbstractListModel
{
Q_OBJECT
public:
FruitModel(QObject *parent = 0);enum Roles {NameRole = Qt::UserRole, DescriptionRole}; QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const; int rowCount( const QModelIndex & parent = QModelIndex()) const; QHash<int,QByteArray> roleNames() const;
public: Q_PROPERTY(int count READ count NOTIFY countChanged)
int count() const;
signals: void countChanged();public slots:
void insert(int index, const QString& name);
void append(const QString& name);
void remove(int index);
QObject* get(int index);
void setProperty(int index, const QString& property, const QVariant& value);private:
QList<Fruit*> fruitmodel;
};@fruit.cpp
@Fruit::Fruit(const QString& name): _name(name), _description(new FruitModel)
{
}Fruit::~Fruit()
{
delete _description;
}void Fruit::setName(const QString& name)
{
if(name != name)
{
name = name;
emit nameChanged();
}
}
//_______________________________________________
FruitModel::FruitModel(QObject *parent): QAbstractListModel(parent)
{}
QHash<int, QByteArray> FruitModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
roles[DescriptionRole] = "description";
return roles;
}int FruitModel::rowCount(const QModelIndex &) const
{
return fruitmodel.count();
}QVariant FruitModel::data(const QModelIndex & index, int role) const
{
//qDebug() << "FruitModel::data" << index.row() << role;
if (index.row() < 0 || index.row() >= fruitmodel.count()) return QVariant();if(role == NameRole) return fruitmodel[index.row()]->name();
// else if(role == DescriptionRole) return QVariant::fromValue(static_cast<QObject*>(fruitmodel[index.row()]->description()));
return QVariant();
}void FruitModel::append(const QString& name)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
fruitmodel << new Fruit(name);
endInsertRows();
emit countChanged();
}QObject* FruitModel::get(int index)
{
if (index < 0 || index >= fruitmodel.count()) return 0;
else return fruitmodel[index];
}
int FruitModel::count() const
{
return fruitmodel.count();
}
@
This only allows me to nest more fruit objects with the name role though. I can't seem to figure out how to be able to nest a description object with a description role. What would be the best method for doing this?