iSa
iSa

Reputation: 1153

Undefined reference to QAbstractListModel::postion

I'm getting this error when implementing QAbstractListModel.

./debug\moc_ObjectModel.o:moc_ObjectModel.cpp:(.rdata$_ZTV12ObjectModel[__ZTV12ObjectModel]+0x38): undefined reference to `QAbstractListModel::position(int, int, QModelIndex const&) const'
collect2.exe: error: ld returned 1 exit status

Building the project was successful before I add insert and remove method to my model.

First, I add the insert and remove method but forgot to call begin* and end* method, so I got an error when rebuilding the project.

After that, I add begin* and end* as stated in the docs. But, when I build the project, I'm getting the error stated above. Then, I remove the insert and remove method and the error still remains.

I have tried to delete the build directory of the project, clean, run qmake, and then build the project but it was no good.

ObjectModel.h

#ifndef OBJECTMODEL_H
#define OBJECTMODEL_H

#include "ProjectCoreGlobal.h"
#include "Data/MyObject.h"

#include <QAbstractListModel>

class PROJECTCORESHARED_EXPORT ObjectModel : public QAbstractListModel
{
    Q_OBJECT

public:
    explicit ObjectModel(QObject *parent = 0);

    int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE;
    QVariant data(const QModelIndex &position, int role) const Q_DECL_OVERRIDE;

    // void insertObject(MyObject *object);
    // void removeObject(int position);

    MyObjectList objects() const;

    MyObject *objectById(const int &id);
    MyObject *objectByName(const QString &name);

private:
    MyObjectList mObjects;
};

#endif // OBJECTMODEL_H

ObjectModel.cpp

#include "ObjectModel.h"

ObjectModel::ObjectModel(QObject *parent) :
    QAbstractListModel(parent)
{
}

int ObjectModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    else
        return mObjects.size();
}

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

    MyObject *obj = mObjects.at(index.row());

    if (role == Qt::DisplayRole) {
        return obj->name();
    }

    return QVariant();
}

// void ObjectModel::insertObject(MyObject *object)
// {
//    beginInsertRows(QModelIndex(), mObjects.count(), mObjects.count());
//    mObjects.append(object);
//    endInsertRows();
// }

// void ObjectModel::removeObject(int position)
// {
//     beginRemoveRows(QModelIndex(), position, position);
//     mObjects.removeAt(position);
//     endRemoveRows();
// }

MyObjectList ObjectModel::objects() const
{
    return mObjects;
}

MyObject *ObjectModel::objectById(const int &id)
{
    foreach (MyObject *obj, mObjects)
    {
        if (obj->id() == id)
        {
            return obj;
        }
    }

    return NULL;
}

MyObject *ObjectModel::objectByNme(const QString &name)
{
    foreach (MyObject *obj, mObjects)
    {
        if (obj->name() == name)
        {
            return object;
        }
    }

    return NULL;
}

Upvotes: 1

Views: 1433

Answers (1)

alexisdm
alexisdm

Reputation: 29896

The only QAbstractListModel class member that matches the signature from the error message is QAbstractListModel::index.

It most certainly means that there is a #define index position somewhere before the include for that class, either in the 2 headers files, or in the project settings (DEFINES += index=position in the .pro file).

Try putting #include <QAbstractListModel> first in ObjectModel.h.

You should also check that the Qt header file for the QAbstractListModel class wasn't inadvertently modified by a search/replace in the wrong window (compare with the original class here).

Upvotes: 1

Related Questions