Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QAbstractListModel return a QObject and use it in qml?



  • Hi,

    I want to use the QAbstractListModel to fill a QML ListView and to set some properties of the ListView. But I also want to access the properties of the elements in my AbstractListModel.

    For Example:

    @class MyClass : public QObject
    {
    Q_OBJECT
    Q_PROPERTY(int number READ Number WRITE SetNumber NOTIFY NumberChanged)
    }@

    @class MyClassListModel : public QAbstractListModel
    {
    Q_OBJECT

    public:
    MyClassListModel(QObject *parent = 0)
    : QAbstractListModel(parent){}

    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;
    

    private:
    QList<MyClass*> mMyClasses
    }@

    @QVariant MyClassListModel::data(const QModelIndex &index, int role) const
    {
    if (!index.isValid())
    return QVariant();

    if (index.row() >= stringList.size())
        return QVariant();
    
    if (role == Qt::DisplayRole)
        return mMyClasses.at(index.row());  
    else
        return QVariant();
    

    }@

    return mMyClasses.at(index.row()); >>>>>>>>> Compiler tells me I cannot return this because it's not a QVariant. Not even when I register this class with Q_DECLARE_METATYPE(MyClass).

    How is this possible?

    This is the situation I want to archieve:

    I have read somewhere you can "return" a (pointer) QObject from data() which then is set in the QML Listview (as a delegate).

    If you then notify a property of that c++ QObject (MyClass number) the property will change in the QML ListView delegate.

    If the property of one of my c++ objects in the list changes I don't want to refresh the whole QML ListView (or add/remove rows) but only update the properties of that object.

    The QML object can then redraw itself instead of redrawing the whole list.



  • What do you expect? You are returning a pointer to a MyClass object. You can wrap that in a QVariant if you want. You can not put an actual QObject in a QVariant, as that would require copying and QObjects can not be copied.



  • This is the situation I want to archieve:

    I have read somewhere you can “return” a (pointer) QObject from data() which then is set in the QML Listview (as a delegate).

    If you then notify a property of that c++ QObject (MyClass number) the property will change in the QML ListView delegate.

    If the property of one of my c++ objects in the list changes I don’t want to refresh the whole QML ListView (or add/remove rows) but only update the properties of that object.

    The QML object can then redraw itself instead of redrawing the whole list.



  • Hi,

    I'd suggest trying https://qt.gitorious.org/qt-labs/qml-object-model -- it has all of the underlying model stuff already implemented for you -- you just need to provide it with the list of objects.

    Regards,
    Michael


Log in to reply