-1

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 tovtable 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??

JuanDeLosMuertos
  • 4,532
  • 15
  • 55
  • 87

6 Answers6

3

Make sure you're running your header through MOC, and are linking those MOC object files.

strager
  • 88,763
  • 26
  • 134
  • 176
  • Indeed this is the answer. With QMake you would have to add the header to the HEADERS variable, but I dunno about cmake. – mxcl Nov 27 '08 at 18:33
  • How is this accomplished on QT creator? – Petruza Jun 18 '10 at 20:48
  • @Petruza, I don't know, as I don't use the Qt creator. If you can edit your qmake (.pro) file, I'd check there first. – strager Jun 19 '10 at 01:23
0

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.

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Petruza
  • 11,744
  • 25
  • 84
  • 136
0

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

JuanDeLosMuertos
  • 4,532
  • 15
  • 55
  • 87
0

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 :)

Tuminoid
  • 9,445
  • 7
  • 36
  • 51
-1

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.

Christophe Weis
  • 2,518
  • 4
  • 28
  • 32
Michael Bishop
  • 4,240
  • 3
  • 37
  • 41
  • And what's the solution for that? the document says it's a common problem but there's no solution – Petruza Jun 18 '10 at 20:47
-1

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

Octoslav
  • 1
  • 1