c++qtqmlsignalsqabstractlistmodel

How create CurrentIndexChanged signal for QAbstractListModel in qt?


I have a class that inherited from QAbstractListModel. now i want to create a signal for this class Liken below

BookListModel.h

signals:
    void currentIndexChanged(int i);

now i have want to use this in below method

QVariant BookListModel::data(const QModelIndex &index, int role) const{

    int i=index.row();
    emit currentIndexChanged();
}

but got this error:

error: C2662: 'void BookListModel::currentIndexChanged(int)': cannot convert 'this' pointer from 'const BookListModel' to 'BookListModel &'

How can i create current index changed signal for this class?


Solution

  • Its because the data function is const. Just declare your singal const as well and it should work:

    signals:
        void currentIndexChanged(int i) const;
    

    See this answer: Is it possible to emit a Qt signal from a const method?