[SOLVED]Linker error for custom QListModel



  • Hi, everybody!

    Ive got a disgusting error: @undefined reference toQAbstractItemModel::data(QModelIndex const&, int) const'@
    while trying to write custom model that inherits QAbstractListModel :(
    Here is the code:

    MusicListModel.h
    @#include <QAbstractListModel>
    #include <QStringList>
    #include <QBrush>
    #include <QHash>
    #include <QAbstractItemModel>
    class MusicListModel : public QAbstractListModel
    {
    Q_OBJECT
    public:
    explicit MusicListModel(QObject *parent = 0);

    bool setData(const QModelIndex &index, const QVariant &value, int role);
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    
    int rowCount(const QModelIndex&) const;
    
    void setNamesList(QStringList &lst);
    void setPathesList(QStringList &lst);
    

    private:
    QStringList namesList;
    QStringList pathesList;
    QHash<int, QBrush> color;

    };
    @

    MusicListModel.cpp
    @#include "musiclistmodel.h"

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

    bool MusicListModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
    if(index.row() < 0 || index.row() >= nameList.size())
    return false;

    switch(role)
    {
    case Qt::DisplayRole:
        nameList.replace(index.row(), value.toString());
        return true;
        break;
    
    case Qt::ToolTipRole:
        if(index.row() >= 0 && index.row() < pathList.size()) {
            pathList.replace(index.row(), value.toString());
            return true;
        }
        return false;
        break;
    
    case Qt::BackgroundRole:
        color.insert(index.row(), QBrush(value.value<QColor>(), Qt::LinearGradientPattern));
        return true;
        break;
    
    default:
        return false;
    }
    

    }

    QVariant MusicListModel::data(const QModelIndex &index, int role) const
    {
    if(index.row() < 0 || index.row() >= nameList.size())
    return QVariant();

    switch(role)
    {
    case Qt::DisplayRole:
        return namesList.at(index.row()); break;
    
    case Qt::ToolTipRole:
        return pathesList.at(index.row()) ; break;
    
    case Qt::BackgroundRole:
        return color.value(index.row(), QBrush(Qt::white)); break;
    
    default:
    

    ===> return QAbstractListModel::data(index, role);
    }
    }

    int MusicListModel::rowCount(const QModelIndex & /&parent/) const
    {
    return namesList.size();
    }
    @

    I cant understand why this subroutine isnt seen by compiler.
    Please, help me to correct it.



  • I get a blank QListView when use my model without this function (while model has data in QStringLists) and I thought ListView needs some more roles to be processed.



  • Hi, you should have note that, it's a pure virtual member of QAbstractItemModel.
    @
    QVariant QAbstractItemModel::data(const QModelIndex & index, int role = Qt::DisplayRole) const [pure virtual]
    @

    BTW, If you do not have a value to return, return an invalid QVariant.



  • Oh, it is looking like Im not so good in OOP :) And know a little about virtual methods
    Thank you!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.