Qt - reduce duplication between two QAbstractListModel subclasses - c++

I've written two QAbstractListModel subclasses:
class Model1: public QAbstractListModel {
Q_OBJECT
public:
int rowCount(const QModelIndex& parent=QModelIndex()) const;
QVariant data(const QModelIndex& index, int role) const;
void clear();
private:
QVector<Obj1*> m_items;
};
and
class Model2: public QAbstractListModel {
Q_OBJECT
public:
int rowCount(const QModelIndex& parent=QModelIndex()) const;
QVariant data(const QModelIndex& index, int role) const;
void clear();
private:
QVector<Obj2*> m_items;
};
In addition, these two classes have a slot AddObj() which is dependent on the pointer used (Obj1* or Obj2*).
The matter is, constructors and destructors are the same, and so are rowCount and clear (they do the exact same thing). The only differences are in data and in what m_items is.
What's the best strategy to reduce duplication here? I tried with a base class:
class ModelBase: public QAbstractListModel {
Q_OBJECT
ModelBase(QObject* parent=0);
~ModelBase();
public:
virtual int rowCount(const QModelIndex& parent=QModelIndex()) const;
virtual QVariant data(const QModelIndex& index, int role) const = 0;
virtual void clear();
private:
QVector<QString*> m_items; // dummy vector
};
and then have the other classes derive from it:
class ModelDerived1: public ModelBase {
Q_OBJECT
public:
ModelDerived1(QObject* parent=0);
~ModelDerived1();
QVariant data(const QModelIndex& index, int role) const = 0;
private:
QVector<Obj1*> m_items;
};
The problem is that in such a case the derived class's data function is never called when this model is attached to a view.
Nevertheless, the derived class data() is not called (I put in debug statements, and they are never executed).
What am I doing wrong here?

EDIT: The solution is not what I posted here. In fact, it was completely off. The issue is in the rowCount function and in the private m_items QVector.
rowCount, if not present, calls the superclass method, which does not use the private m_items element from the derived class, but it uses the one from the superclass, which being dummy, returns always a size of 0, and thus data never gets called.

Related

How to expose list of custom objects with Q_PROPERTY

I have a very simple class with 2 properties; key and value:
KeyValue.h:
class KeyValue : public QObject
{
Q_OBJECT
Q_PROPERTY(QString key READ getKey WRITE setKey NOTIFY keyChanged)
Q_PROPERTY(QString value READ getValue WRITE setValue NOTIFY valueChanged)
public:
KeyValue(const QString& key, const QString& value, QObject* parent = 0);
signals:
void keyChanged();
void valueChanged();
private:
QString _key;
QString _value;
QString getKey() const;
QString getValue() const;
void setKey(const QString& key);
void setValue(const QString& value);
};
Q_DECLARE_METATYPE(KeyValue)
In another class I would like a property containing a list of KeyValue objects, so I can use this list as a model in QML.
Controller.h
class Controller : public QObject
{
Q_OBJECT
Q_PROPERTY(QList<KeyValue*> items READ getItems NOTIFY itemsChanged)
public:
explicit Controller(QObject* parent = 0);
signals:
void itemsChanged();
private:
QList<KeyValue*> getItems() const;
};
I want to be able to use this in QML the following way:
import QtQuick 2.7
import customqml 1.0
Item{
Controller{
id: controller
}
Repeater{
model: controller.items
Text{
text: modelData.key + ": " + modelData.value
}
}
}
Both classes are registered in my main.cpp file:
qmlRegisterType<KeyValue>("customqml", 1, 0, "KeyValue");
qmlRegisterType<Controller>("customqml", 1, 0, "Controller");
The above code does not work, bacause I apparently can't expose a QList to QML directly. I have tried using QAbstractItemModel and QQmlListProperty, but I was unable to get it to work. Can anyone point me in the right direction?
My primary issues are the type of the items property in the Controller class and the return value of the getItems method.
I'm using Qt 5.9 if that makes any difference.
Note:
The getters and setters are generally public except for exceptions so move it to the public part
The classes that inherit from QObject do not need QMetaType because when you want to transfer data of that class the pointers are used.
Not all data types are supported by QML through Q_PROPERTY, so a possible solution is to export through known classes such as
QList<QObject *>:
class Controller : public QObject
{
Q_OBJECT
Q_PROPERTY(QList<QObject *> items READ getItems NOTIFY itemsChanged)
public:
explicit Controller(QObject *parent = nullptr);
QList<QObject *> getItems() const;
signals:
void itemsChanged();
private:
QList<KeyValue *>key_values_list;
};
...
QList<QObject *> Controller::getItems() const
{
QObjectList l;
for(auto e: key_values_list)
l << e;
return l;
}
QVariantList:
class Controller : public QObject
{
Q_OBJECT
Q_PROPERTY(QVariantList items READ getItems NOTIFY itemsChanged)
public:
explicit Controller(QObject *parent = nullptr);
QVariantList getItems() const;
signals:
void itemsChanged();
private:
QList<KeyValue *>key_values_list;
};
...
QVariantList Controller::getItems() const
{
QVariantList l;
for(auto e: key_values_list)
l.append(QVariant::fromValue(e));
return l;
}
Other options is to implement a model, the following example shows only a read-only model:
keyvaluemodel.h
#ifndef KEYVALUEMODEL_H
#define KEYVALUEMODEL_H
#include "keyvalue.h"
#include <QAbstractListModel>
class KeyValueModel : public QAbstractListModel
{
Q_OBJECT
public:
explicit KeyValueModel(QObject *parent = nullptr)
: QAbstractListModel(parent)
{
key_values_list = {new KeyValue{"k", "v"}, new KeyValue{"k2", "v2"}};
}
int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
if (parent.isValid())
return 0;
return key_values_list.length();
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
{
if (!index.isValid())
return QVariant();
if(index.row() >= 0 && index.row() < rowCount())
return QVariant::fromValue(key_values_list[index.row()]);
return QVariant();
}
private:
QList<KeyValue* >key_values_list;
};
#endif // KEYVALUEMODEL_H
class Controller : public QObject
{
Q_OBJECT
Q_PROPERTY(KeyValueModel* items READ getItems NOTIFY itemsChanged)
public:
explicit Controller(QObject *parent = nullptr);
KeyValueModel* getItems() const;
signals:
void itemsChanged();
private:
KeyValueModel *model;
};
...
Text{
text: display.key + ": " + display.value
}
...
And in a similar way you can implement a QQmlListProperty, in the docs there are many examples.
If you want a sophisticated model with adding/deleting objects and altering data you should look into subclassing QAbstractListModel.
As a simple but less flexible way you can use a QVariantList and make your Controller class a value type. You need:
The macro Q_DECLARE_METATYPE(Controller) at the end of the Controller header file.
A copy constructor for Controller
A default constructor for Controller
Q_PROPERTY Type and return value are then QVariantList.

How do I interact with a model in an extended QQuickItem?

There are many good resources on dealing with models and views in the Qt doc, like: http://doc.qt.io/qt-5/model-view-programming.html, but I can't seem to find any that link to dealing with models in QtQuick. There are some basic chapters on extending qml with c++, like in http://doc.qt.io/qt-5/qtqml-tutorials-extending-qml-example.html, and on using models: http://doc-snapshots.qt.io/qt5-5.11/qtquick-modelviewsdata-modelview.html, but I can't quite find a way to use an actual model in extended qml.
Currently I have this model:
class LayoutModel : public QAbstractItemModel {
Q_OBJECT
public:
explicit LayoutModel(const QString &data, QObject *parent = 0);
~LayoutModel();
QVariant data(const QModelIndex &index, int role) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
private:
void setupModelData(const QStringList &lines, LayoutItem *parent);
LayoutItem *rootItem;
};
And I'm trying to access it from this view class:
class Layout : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(LayoutModel model READ model WRITE setModel NOTIFY modelChanged)
private:
LayoutModel & m_model;
public:
explicit Layout(QQuickItem * parent = nullptr);
LayoutModel & model() const;
void setModel(const LayoutModel & model);
void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData & value) override;
void geometryChanged(const QRectF & newGeometry, const QRectF & oldGeometry) override;
signals:
void modelChanged();
};
But I can't find a way to actual use the model. I can't even properly set up read and write to the model, since QAbstractItemModels (and models in Qt in general) have their copy constructor deleted to enforce entity singularity. Here's my current broken implementation:
Layout::Layout(QQuickItem * parent) : QQuickItem(parent) {}
LayoutList & Layout::model() const
{
return m_model;
}
void Layout::setModel(const LayoutList & model)
{
if (m_model == model)
return;
m_model = model;
emit modelChanged();
}
So, how can I make it so that I can use this extended qml class with my LayoutModel?
QObjects do not have a copy constructor, so you must use a pointer:
*.h
class Layout : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(LayoutModel *model READ model WRITE setModel NOTIFY modelChanged)
private:
LayoutModel *m_model;
public:
explicit Layout(QQuickItem * parent = nullptr);
LayoutModel *model();
void setModel(LayoutModel * model);
...
signals:
void modelChanged();
};
*.cpp
...
LayoutModel *Layout::model()
{
return m_model;
}
void Layout::setModel(LayoutModel *model)
{
if (m_model == model)
return;
m_model = model;
emit modelChanged();
}

Why I get an "use of delete function" from a class that inherits from QAbstractListModel?

I am trying to implement a ListView. So far, I have implemented a class named PacientModel that inherits of QAbstractListModel.
#ifndef PACIENTMODEL_H
#define PACIENTMODEL_H
#include <QAbstractListModel>
class PacientModel : public QAbstractListModel {
Q_OBJECT
public:
enum PacientRole {
CIRole,
NameRole,
LastNameRole
};
Q_ENUM(PacientRole)
PacientModel(QObject * parent = nullptr);
int rowCount(const QModelIndex & = QModelIndex()) const;
QVariant data (const QModelIndex &index, int role = Qt::DisplayRole) const;
QHash<int, QByteArray> roleNames() const;
Q_INVOKABLE QVariantMap get(int row) const;
Q_INVOKABLE void append (const QString &CI, const QString &name, const QString &lastName);
Q_INVOKABLE void set (int row, const QString & CI, const QString &name, const QString &lastName);
Q_INVOKABLE void remove (int row);
private:
struct Pacient {
QString CI;
QString name;
QString lastName;
};
QList<Pacient> m_pacients; };
#endif // PACIENTMODEL_H
I also have the implementation of the ListView, but when I compiled the code, I got this error.
C:\Qt\Qt5.8.0\5.8\android_armv7\include\QtCore\qmetatype.h:765: error: use of deleted function 'PacientModel::PacientModel(const
PacientModel&)'
return new (where) T(*static_cast(t));
How can I resolve this?
Per this Q&A QAbstractListModel's copy constructor is marked as private. That means that it is not copyable and therefore, as you inherited from it, your derived class is also not copyable by default.
If you want to make copies of your class then you need to manually define a copy constructor for the class instead of relying on the compiler doing it for you (since it won't).
After several days I discovered that the error wasn't in the class declaration, It was in the way I assigned the data to the listview... I was using this:
objetoLP->setProperty("modelList", QVariant::fromValue(pacientes));
instead of:
ctxt->setContextProperty("modelList", &pacientes);

QML,How to dynamicaly change Item of Repeater from C++

I have QML Repeater on Grid, when i clicked on item i emit signal which processed by C++ class, and then changed array in C++ which then assign as model of QML Repeater. Is there a way to change just two elements of C++ model, not whole model as i did?
that's my qml file
Grid{
height:width
rows:8
columns: 8
Repeater{
id: chessPiecesRptr
...
signal chessfigureSelected(int index)
delegate: Item{
id:chessPiecesItm
Image{
...
}
MouseArea
{
anchors.fill:parent
onClicked: {
chessPiecesRptr.chessfigureSelected(index)
}
}
}
}
C++ method which update model of QML Repeater
void ChessFiguresClass::changeModel()
{
QStringList dataList;
for(int i=0;i<64;i++)
dataList.append(QChar(posArray[i]));
QQmlProperty::write(chessPiecesRptr, "model", QVariant::fromValue(dataList));
}
Contrary to the accepted answer it is indeed possible without going all the way to implement an entire QAbstractListModel.
It is true that QStringList doesn't have any way to notify for changes, but all you have to do is wrap it up in a property, something like this:
Q_PROPERTY(QVariant myModel READ myModel NOTIFY myModelChanged)
...
QVariant myModel() { return QVariant::fromValue(myStringList); }
...
void myModelChanged(); // signal
And just emit myModelChanged every time you want to reflect a change in the model, be that replacing it with a different model or changing it internally. And use the myModel property for the Repeater's model.
However, if you have a lot of items in the model or the delegates are complex, it is always a good idea to implement your own QAbstractListModel, because with the approach above, the repeater will recreate the entire model every time it "changes", whereas the QAbstractListModel will only update the actual changes.
I'm afraid it is not possible. QList (and QStringList) does not have inner mechanisms to notify Qml items about its changes. Only when model property from QML item is changed, the whole list is read again.
I had faced the same problem before and I implemented a string list using QAbstractListModel as base class. The header looks like this:
#ifndef _SIMPLEMODEL_H_
#define _SIMPLEMODEL_H_
#include <QtCore>
class SimpleStringModel : public QAbstractListModel
{
Q_PROPERTY(int count READ count NOTIFY countChanged)
Q_DISABLE_COPY(SimpleStringModel)
Q_OBJECT
public:
explicit SimpleStringModel(QObject* parent = 0);
SimpleStringModel(const QList<QString>& initList, QObject* parent = 0);
virtual ~SimpleStringModel();
private:
enum Roles{
ModelDataRole = Qt::UserRole+1
};
public:
int count() const;
public:
void append(const QString& item);
void insert(int index, const QString& item);
void append(const QList<QString>& items);
void insert(int index, const QList<QString>& items);
bool removeOne(const QString& item);
int removeAll(const QString& item);
void removeAt(int index);
QList<QString> list() const;
signals:
void countChanged();
// QAbstractItemModel interface
public:
virtual int rowCount(const QModelIndex &parent) const;
virtual QVariant data(const QModelIndex &index, int role) const;
virtual QHash<int, QByteArray> roleNames() const;
private:
QList<QString> m_data;
};
#endif //_SIMPLEMODEL_H_
You can get all the code here. I hope this help you.

QAbstractTableModel inheritance vtable problem

Here's another problem with qt:
I extend a QAbstractTableModel, but I get a compiling error ( I'm using cmake)
// file.h
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractTableModel>
class TableModel : public QAbstractTableModel
{
Q_OBJECT
public:
TableModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
};
#endif
// file.c
#include "tableModel.h"
TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent){}
int TableModel::rowCount(const QModelIndex & ) const
{ return 1; }
int TableModel::columnCount(const QModelIndex & ) const
{ return 1;}
when I compile I get:
In function TableModel':
/partd/unusedsvn/unusedpkg/iface/tableModel.cpp:4: undefined reference tovtable for TableModel'
/partd/unusedsvn/unusedpkg/iface/tableModel.cpp:4: undefined reference to vtable for TableModel'
collect2: ld returned 1 exit status
does anybody got the same trouble??
Make sure you're running your header through MOC, and are linking those MOC object files.
Solved adding to CMakeLists.txt the needed cpp file.
set(tutorial_SRCS app.cpp mainWin.cpp tableModel.cpp)
When I'll run cmake, the moc* will be automatically created
Almost 100% percent of vtable errors are caused by either missing headers/class definitions or by typoes in those definitions, so the first thing to do is to make sure you got the headers and sources right (and included in project). I've personally cursed Qt to the lowest hell for that and missed that tiny typo in project file, not fun :)
Yes, vtable errors are a bitch.
You have to implement the code() method which is a pure virtual method too.
From the QAbstractTableModel documentation :
Subclassing
When subclassing QAbstractTableModel, you must implement rowCount(), columnCount(), and data().
I'm having a vtable problem too, and I implemented data(), so I'm missing other virtual crap but I don't know whitch one.
This is a fairly common bug when an object isn't moc'ed. I'd read the whole debugging document to save yourself some time down the road.
To resolve this problem, i've remove Q_OBJECT from TableModel, made new class TableModelController, that derived from QObject and have TableModel inside
class TableModel : public QAbstractTableModel
{
public:
TableModel(QObject *parent = 0);
// Some overrided functions
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;
};
class TableModelController : public QObject
{
Q_OBJECT
public:
explicit TableModelController(QObject *parent = nullptr);
TableModelController(TableModel *m, QObject *parent = nullptr);
TableModel *getModel() {
return model;
}
public slots:
void addRow();
void deleteRows();
private:
TableModel *model;
};
Then i use TableModelController to access TableModel throw get Methond and public slots. I'm use QtCreator