Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]Linker error for custom QListModel
Forum Update on Monday, May 27th 2025

[SOLVED]Linker error for custom QListModel

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.9k Views
  • 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.
  • W Offline
    W Offline
    wild34
    wrote on 31 Jul 2013, 21:06 last edited by
    #1

    Hi, everybody!

    Ive got a disgusting error: @undefined reference to QAbstractItemModel::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.

    1 Reply Last reply
    0
    • W Offline
      W Offline
      wild34
      wrote on 31 Jul 2013, 21:17 last edited by
      #2

      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.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on 1 Aug 2013, 01:11 last edited by
        #3

        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.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wild34
          wrote on 1 Aug 2013, 08:17 last edited by
          #4

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

          1 Reply Last reply
          0

          1/4

          31 Jul 2013, 21:06

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved