kontiki
kontiki

Reputation: 216

Error: no matching function for call to QSortFilterProxyModel::setSourceModel(NavaidsModel&

I'm facing a compilation problem using QSortFilterProxyModel when trying to setSourceModel.

The error message is : no matching function for call to QSortFilterProxyModel::setSourceModel(NavaidsModel&)

and the candidate is : virtual void SortFilterProxyModel::setSourceModel(QAbstractItemModel*)

Here is my code :

main.cpp

#include "navaidsmodel.h"
#include <QListView>
#include <QApplication>
#include <QSortFilterProxyModel>

int main(int c, char **v)
{
    QApplication a(c, v);

    //Model
    NavaidsModel model;
    model.readFromCSV(QCoreApplication::applicationDirPath() + "/files/data.csv");

    //Proxy
    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel;
    proxyModel->setSourceModel(model);                              //Here is the trick

    //Filter
    QRegExp rx("ROBU");
    rx.setCaseSensitivity(Qt::CaseInsensitive);
    rx.setPatternSyntax(QRegExp::Wildcard);
    proxyModel->setFilterRegExp(rx);
    proxyModel->setFilterKeyColumn(0);

    //View
    QListView view;
    view.setUniformItemSizes(true);
    view.setModel(proxyModel);
    view.show();

    return a.exec();
}

and here is the constructor for navaidsmodel :

class NavaidsModel : public QAbstractListModel
{
    Q_OBJECT
public:

    NavaidsModel(QObject *parent = Q_NULLPTR):QAbstractListModel(parent){
    }
    enum NavaidsRoles {
        PositionRole = Qt::UserRole + 1, 
        OACICodeRole,
        CountryCodeRole
    };

So, I understand setSourceModel request a QAbstractItemModel.

For me, NavaidsModel inherit from QAbstractListModel which inherit from QAbstractItemModel.

So, it should be OK, but it isn't.

Could you explain me what is the root cause of the problem ?

Thanks for help.

Upvotes: 0

Views: 232

Answers (1)

Felix
Felix

Reputation: 7146

setSourceModel wants a pointer to a model, not a value or reference. So all you have to do is change the line

proxyModel->setSourceModel(model);

to

proxyModel->setSourceModel(&model);

Alternatively, you could also create your model with new instead, i.e.

NavaidsModel *model = new NavaidsModel();

EDIT:

When working with pointers, you should always either prefer smart pointer or use the Qt parent-child mechanisms to automatically delete the models with their view. You should make your two models children of the view:

int main(int c, char **v)
{
    QApplication a(c, v);

    // create the view first
    QListView view;

    //Model
    NavaidsModel *model = new NavaidsModel(&view);
    model->readFromCSV(QCoreApplication::applicationDirPath() + "/files/data.csv");

    //Proxy
    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(&view);
    proxyModel->setSourceModel(model);

    // ...

Upvotes: 2

Related Questions