Search code examples
c++qtundefined-referenceqabstracttablemodel

Qt: undefined reference on constructor of customer class


I'm using a custom class, GpibDevicesModel, inheriting from QAbstractTableModel, and the compiler says that there is an undefined reference on the constructor. But I don't understand why.

Here is the code:

MainWindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT    
public: 
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    //...
private:
    GpibDevicesModel *m_gpibDevicesModel;
};

MainWindow.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //...
    //undefined reference is located here 
    m_gpibDevicesModel = new GpibDevicesModel;
    //...
}

GpibDevicesModel.h

#ifndef GPIBDEVICESMODEL_H
#define GPIBDEVICESMODEL_H

#include <QAbstractTableModel>
class MeasureDevice;

class GpibDevicesModel : public QAbstractTableModel
{
    Q_OBJECT
public:    
    enum Columns{
        NameCol = 0,
        AddressCol,
        IdCol,
        TypeCol,
        ConnectedCol,
        EndCol //Should always be at end
    };

    //constructor and destructor
    explicit GpibDevicesModel(QObject *parent = 0);
    ~GpibDevicesModel();
protected:
    //Reimplemented from QAbstractTableModel
    QVariant data(const QModelIndex &index, int role) const;
    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;

    //Data list getter and setter
    QList<MeasureDevice *> measureDevices() const;
    void setMeasureDevices(const QList<MeasureDevice *> &measureDevices);

private:
    QList<MeasureDevice *> m_measureDevices;
};

#endif // GPIBDEVICESMODEL_H

GpibDevicesModel.cpp

#include "gpibdevicesmodel.h"
#include "measuredevice.h"

#include <QIcon>


GpibDevicesModel::GpibDevicesModel(QObject *parent)
    : QAbstractTableModel(parent)
{}

GpibDevicesModel::~GpibDevicesModel()
{}

QVariant GpibDevicesModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid() || index.row() >= m_measureDevices.size())
        return QVariant();

    switch(role){
    //This is roles for ComboBox
    case Qt::DisplayRole:
        switch(index.column()){
        case NameCol: return m_measureDevices.at(index.row())->displayName();
        case AddressCol:return m_measureDevices.at(index.row())->gpibName();
        case IdCol: return m_measureDevices.at(index.row())->property("internal_id").toString();
        case TypeCol: return m_measureDevices.at(index.row())->typeString();
        }
        break;
    case Qt::DecorationRole:
        switch(index.column()){
        case ConnectedRole: {
            MeasureDevice *md = m_measureDevices.at(index.row());
            if(md->isConnected()) return QIcon(":/Icons/green_led.png");
            else return QIcon(":/Icons/red_led.png");
        }
        }
        break;

    }
    return QVariant();
}

int GpibDevicesModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_measureDevices.size();
}

int GpibDevicesModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return EndCol;
}

QVariant GpibDevicesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    static QStringList headers = QStringList() << tr("Name") << tr("Address") << tr("Id")
                                               << tr("Type") << tr("Connected") ;

    if(role == Qt::DisplayRole && orientation == Qt::Vertical)
        return QVariant(); //return section; //uncomment here to displayed row number

    if(section >= headers.size())
        return QVariant();

    switch(role){
    case Qt::DisplayRole: return headers.at(section);
    }
    return QVariant();
}

QList<MeasureDevice *> GpibDevicesModel::measureDevices() const
{
    return m_measureDevices;
}

void GpibDevicesModel::setMeasureDevices(const QList<MeasureDevice *> &measureDevices)
{
    beginResetModel();
    m_measureDevices = measureDevices;
    endResetModel();
}

And here is the error message from the compiler

mainwindow.cpp:1430: undefined reference to  GpibDevicesModel::GpibDevicesModel(QObject*)'

I think that will be a stupid thing, as always... But I cannot figure out this error. I'm using another custom model in the same way and I don't have any problem.


Solution

  • As I guessed, it was stupid... I resolved the problem by removing manually the build folder and the Makefiles (makefile, makefile.debug and makefile.release), then I run again qmake -> build and it was ok. So this was not a problem from code, maybe the makefiles was in read-only for an unknown reason.

    Thanks for your help and your time Frogatto and Frank Osterfeld.