Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QAbstractListModel in ListView, mouse area signals not working
Qt 6.12 LTS is out! See what's new in the release blog

QAbstractListModel in ListView, mouse area signals not working

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt
    • Unsolved