Malachi
Malachi

Reputation: 2443

Passing QAbstractItemModel* to C++ class

Consider:

class Utility : public QObject
{
    Q_OBJECT

public:
    explicit Utility(QObject* parent = nullptr) : QObject(parent) {}

    Q_INVOKABLE void test(const QAbstractItemModel* model)
    {

    }
};

Set up in main via qmlRegisterSingletonInstance("pgqt34", 1, 0, "Utility", &utility);

and QML is:

import QtQuick 2.15
import QtQuick.Window 2.15

import pgqt34 1.0

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    ListModel {
        id: listModel
    }

    onActiveChanged: {
        Utility.test(listModel);
    }
}

I consistently get Error: Unknown method parameter type: const QAbstractItemModel* for Qt 5.15.2 compilations, but Qt 6.x+ works OK. What is wrong here?

Upvotes: 0

Views: 101

Answers (1)

Alexander Dyagilev
Alexander Dyagilev

Reputation: 1425

  1. Straight way:

    Q_INVOKABLE void test(const QObject* obj) { auto model = qobject_cast(obj); }

  2. Declare QAbstractItemModel* as a metatype. But, I'm not sure if it's a working solution.

Upvotes: 1

Related Questions