I have subclassed the QAbstractTableModel
. Now I would like to pass to to the QML side. All examples I found expose the override methods of the class using Q_INVOKABLE
, ie data
or setData
. Can the whole QAbstractTableModel
object be passed as Q_INVOKABLE
? If yes, how to do that exactly?

- 5,324
- 1
- 27
- 40

- 2,725
- 5
- 38
- 74
-
You can pass your model from C++ to QML using [setContextProperty](https://stackoverflow.com/questions/32044270/qt-qml-qmlregistertype-vs-setcontextproperty-difference/32045102). You can find thousands example over the internet – folibis Jun 03 '19 at 17:20
-
Hi thabks for answer. I know that and I would like to avoid it, since I am passing only 1 global object using this method. My model is insode the object I pass through context property – Łukasz Przeniosło Jun 03 '19 at 17:35
2 Answers
Q_INVOKABLE
is meant for exposing methods of QObject derived types to QML. You can use Qt property system
for exposing your QAbstractTableModel from your "global object" which you have made available to QML through QML context (as you commented under your question).
You can read from the documentation more about exposing Attributes of C++ Types to QML.
MyTableModel deriving from QAbstractTableModel:
class MyTableModel : public QAbstractTableModel
{
Q_OBJECT
};
MyGlobalObject exposing MyTableModel member variable through the property system:
class MyGlobalObject : public QObject
{
Q_OBJECT
Q_PROPERTY(QAbstractTableModel* myTableModel READ myTableModel CONSTANT)
public:
MyGlobalObject(QObject *parent = nullptr) : QObject(parent), m_myTableModel(new MyTableModel) { }
MyTableModel *myTableModel() { return m_myTableModel.data(); }
private:
QScopedPointer<MyTableModel> m_myTableModel;
};
MyGlobalObject instance set as a context property in main:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
MyGlobalObject model;
engine.rootContext()->setContextProperty("myGlobalObject", &model);
}
MyTableModel used as model for QML TableView:
import QtQuick 2.12
TableView {
model: myGlobalObject.myTableModel
}

- 5,324
- 1
- 27
- 40
-
Thanks for answer. Can the property be Read/Write as well when it comes to `QAbstractTableModel`? Also, this `m_myTableModel.data();` would return `QVariant`. I have shown my whole override class here: https://stackoverflow.com/questions/56434973/connect-qabstracttablemodel-with-qml-tableview – Łukasz Przeniosło Jun 04 '19 at 06:52
-
You are misunderstanding the data() method to your model's data method! It's there because I used smart pointer for a member variable: https://doc.qt.io/qt-5/qscopedpointer.html#data . You return the pointer to your model object. Then you can read and also manipulate the object in QML side. The property itself shouldn't be writeable in this case. You are not changing your model property itself, just data inside the model object – talamaki Jun 04 '19 at 08:25
It is not safe to pass pointers from C++ to Qml via Q_INVOKABLE as the Javascript garbage collecter will try to delete those objects from QML even though they are supposed to be owned by C++. Instead, you should pass your objects (pointers) as properties using Q_PROPERTY macro.

- 1
- 2