[Qt-interest] undefined reference to `vtable for StringListModel'

phil prentice philp.cheer at talktalk.net
Mon Mar 16 13:44:33 CET 2009


Hi
  Sorry if this is more of a C++ question then QT.  I was going through a QT 
Help example and was trying to build it but get the error message:-
 
main.o(.gnu.linkonce.t._ZN15StringListModelC1ERK11QStringListP7QObject[StringListModel::StringListModel(QStringList 
const&, QObject*)]+0x1a): In function 
`StringListModel::StringListModel(QStringList const&, QObject*)':
/home/philp/MODEL_VIEW/QAbstractListModelExample/stringListModel.h:7: 
undefined reference to `vtable for StringListModel'

The definition

class StringListModel : public QAbstractListModel
{
  Q_OBJECT

    public:
      StringListModel(const QStringList &strings, QObject *parent = 0)
                          : QAbstractListModel(parent), stringList(strings) {}

      int rowCount(const QModelIndex &parent = QModelIndex()) const;
      QVariant data(const QModelIndex &index, int role) const;
      QVariant headerData(int section, Qt::Orientation orientation,
                                        int role = Qt::DisplayRole) const;

      Qt::ItemFlags flags(const QModelIndex &index) const;
      bool setData(const QModelIndex &index, const QVariant &value,
                                                int role = Qt::EditRole);
      bool insertRows(int position, int rows,
                                const QModelIndex &index = QModelIndex());
      bool removeRows(int position, int rows,
                                const QModelIndex &index = QModelIndex());
    private:
      QStringList stringList;
};

The implementation

#include <QtGui>
#include "stringListModel.h"

/* How many data items? */
int StringListModel::rowCount(const QModelIndex &parent) const
{
  return stringList.count();
}

/* Use model to obtain data. */
QVariant StringListModel::data(const QModelIndex &index, int role) const
{
  if (!index.isValid())
    return QVariant();

  if (index.row() >= stringList.size())
    return QVariant();

  if (role == Qt::DisplayRole)
    return stringList.at(index.row());
  else
    return QVariant();
}


/* Optional allows headers to be displayed. */
QVariant StringListModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
  if (role != Qt::DisplayRole)
    return QVariant();

  if (orientation == Qt::Horizontal)
    return QString("Column %1").arg(section);
  else
    return QString("Row %1").arg(section);
}

/* Allows data to be edited. */
Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
{
  if (!index.isValid())
    return Qt::ItemIsEnabled;

  return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

bool StringListModel::setData(const QModelIndex &index,
                                        const QVariant &value, int role)
{
  if (index.isValid() && role == Qt::EditRole)
  {
    stringList.replace(index.row(), value.toString());
    emit dataChanged(index, index);
        /* Let the views know of changes..data changed signal emitted. */
    return true;
  }
  return false;
}

bool StringListModel::insertRows(int position, int rows,
                                                const QModelIndex &parent)
{
  beginInsertRows(QModelIndex(), position, position+rows-1);
                /* Always called before data added. */

  for (int row = 0; row < rows; ++row)
  {
    stringList.insert(position, "");
  }

  endInsertRows(); /* Allways at end. */
  return true;
}

bool StringListModel::removeRows(int position, int rows,
                                                const QModelIndex &parent)
{
  beginRemoveRows(QModelIndex(), position, position+rows-1);
                /* Always called before data removed. */

  for (int row = 0; row < rows; ++row)
  {
    stringList.removeAt(position);
  }

  endRemoveRows(); /* Allways at end. */
  return true;
}

Main

#include <QtGui>
#include "stringListModel.h"

int main(int argc, char *argv[])
{
  QApplication app(argc, argv);
  QStringList numbers;
  numbers << "One" << "Two" << "Three" << "Four" << "Five";

  QAbstractItemModel *model = new StringListModel(numbers);

  return app.exec();
}

I am trying to learn as much as I can about C++ & QT but I dont find this 
error message very helpful...can anybody explain what I am doing wrong.

Thanks for your help

Phil




More information about the Qt-interest-old mailing list