QAbstractTableModel inheritance vtable problem
-
11-07-2019 - |
Question
Here's another problem with qt: I extend a QAbstractTableModel, but I get a compiling error ( I'm using cmake)
// file.h
#ifndef TABLEMODEL_H
#define TABLEMODEL_H
#include <QAbstractTableModel>
class TableModel : public QAbstractTableModel
{
Q_OBJECT
public:
TableModel(QObject *parent = 0);
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
};
#endif
// file.c
#include "tableModel.h"
TableModel::TableModel(QObject *parent)
: QAbstractTableModel(parent){}
int TableModel::rowCount(const QModelIndex & ) const
{ return 1; }
int TableModel::columnCount(const QModelIndex & ) const
{ return 1;}
when I compile I get:
In function TableModel':
/partd/unusedsvn/unusedpkg/iface/tableModel.cpp:4: undefined reference to
vtable for TableModel'
/partd/unusedsvn/unusedpkg/iface/tableModel.cpp:4: undefined reference to vtable for TableModel'
collect2: ld returned 1 exit status
does anybody got the same trouble??
Solution 2
Solved adding to CMakeLists.txt the needed cpp file.
set(tutorial_SRCS app.cpp mainWin.cpp tableModel.cpp)
When I'll run cmake, the moc* will be automatically created
OTHER TIPS
Make sure you're running your header through MOC, and are linking those MOC object files.
Almost 100% percent of vtable errors are caused by either missing headers/class definitions or by typoes in those definitions, so the first thing to do is to make sure you got the headers and sources right (and included in project). I've personally cursed Qt to the lowest hell for that and missed that tiny typo in project file, not fun :)
Yes, vtable errors are a bitch.
You have to implement the code() method which is a pure virtual method too.
From the QAbstractTableModel documentation :
Subclassing
When subclassing QAbstractTableModel, you must implement rowCount(), columnCount(), and data().
I'm having a vtable problem too, and I implemented data(), so I'm missing other virtual crap but I don't know whitch one.
This is a fairly common bug when an object isn't moc'ed. I'd read the whole debugging document to save yourself some time down the road.
To resolve this problem, i've remove Q_OBJECT from TableModel, made new class TableModelController, that derived from QObject and have TableModel inside
class TableModel : public QAbstractTableModel
{
public:
TableModel(QObject *parent = 0);
// Some overrided functions
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
};
class TableModelController : public QObject
{
Q_OBJECT
public:
explicit TableModelController(QObject *parent = nullptr);
TableModelController(TableModel *m, QObject *parent = nullptr);
TableModel *getModel() {
return model;
}
public slots:
void addRow();
void deleteRows();
private:
TableModel *model;
};
Then i use TableModelController to access TableModel throw get Methond and public slots. I'm use QtCreator