views:

564

answers:

5

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

+2  A: 

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

strager
Indeed this is the answer. With QMake you would have to add the header to the HEADERS variable, but I dunno about cmake.
Max Howell
How is this accomplished on QT creator?
Petruza
@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
A: 

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

Giancarlo
And how is this achieved in QT creator?
Petruza
A: 

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

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.

Michael Bishop
And what's the solution for that? the document says it's a common problem but there's no solution
Petruza
A: 

Yes, vtable errors are a bitch.
You have to implement the code() method which is a pure virtual method too.

From http://doc.qt.nokia.com/4.6/qabstracttablemodel.html :

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.

Petruza