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

QAbstractListModel in ListView, mouse area signals not working



  • Hi everyone,

    I have created simple ListModel class that inherits from QAbstractListModel:

    @class ListModel : public QAbstractListModel
    {
    Q_OBJECT
    public:
    explicit ListModel(QObject *parent = 0);
    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;

    QModelIndex indexFromItem(const ListModelEntry* item) const;
    Q_INVOKABLE void appendRow(ListModelEntry *item);
    Q_INVOKABLE void clear();
    Q_INVOKABLE void populate();
    Q_INVOKABLE void update(int index);
    

    signals:
    void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);

    private slots:
    void handleItemChange();

    private:
    QList<ListModelEntry*> _entries;
    };@

    exported in main with:
    @qmlRegisterType<ListModel>("ListModel", 1, 0, "ListModel");@

    and used it in qml document:

    @import QtQuick 1.1
    import ListModel 1.0

    Item{
    width: 300
    height: 400

    ListModel{
        id: listModel
    }
    
    Component{
        id: myDelegate
    
        Item{
            width: parent.width
            height: 40
            Rectangle{
                anchors.fill: parent
                color: "yellow"
                border{
                    width: 2
                    color: "black"
                }
    
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        console.log("index " + index) // <--- not working
                    }
                }
    

    ...
    }
    }
    }

    ListView{
        anchors.fill: parent
        model: listModel
        delegate: myDelegate
        focus: true
    }
    

    }@

    sadly the MouseArea element associated with the delegate does not react in any way. I checked analogical situation using ListModel with ListItems as a model in qml, everything works as it should. But with cpp model accessed from qml there's a problem.
    By the way: here I am using method 'update' that is calling update method on specified entry object. Is there a way to omit ListModel interface and access ListModelEntry object itself?

    Thank you for any help


Log in to reply