Problem with QAbstractTableModel inheritance vtable

Here's another problem with qt: I am extending QAbstractTableModel but getting compilation error (I am 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 compiling, I get:

In TableModel': /partd/unusedsvn/unusedpkg/iface/tableModel.cpp:4: undefined reference to

vtable function for TableModel '/partd/unusedsvn/unusedpkg/iface/tableModel.cpp:4: undefined reference tovtable for TableModel' collect2: ld returned 1 exit status

Has anyone had the same problem?

0


source to share


6 answers


Almost 100% of vtable errors are caused either by missing headers / class definitions or typographies in those definitions, so the first thing to do is make sure you are using headers and sources correctly and correctly (and included in the project). I personally cursed Qt to the lowest hell for this and missed this tiny typo in the project file, not fun :)



0


source


Make sure you run your header through MOC and link these MOC object files.



+3


source


Decided to add the required cpp file to CMakeLists.txt.

set(tutorial_SRCS app.cpp mainWin.cpp tableModel.cpp)

      

When I run cmake, moc * will be automatically created

0


source


It is a fairly common mistake when the object is not moc'ed . I have read the entire debug doc to save some time along the way.

0


source


Yes, vtable errors are a bitch.
You must implement the code () method, which is also a pure virtual method.

From Documentation QAbstractTableModel :

Subclasses
When subclassing QAbstractTableModel, you must implement rowCount (), columnCount (), and data () .

I have a problem with vtable too and I have implemented data (), so I am missing other virtual crap, but I don't know what that means.

0


source


To fix this problem, I remove the Q_OBJECT from the TableModel, created a new TableModelController class derived from the QObject and has a 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 the TableModel throw get Methond and public slots. I am using QtCreator

0


source







All Articles