1

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.

Community
  • 1
  • 1
Gojir4
  • 313
  • 2
  • 17
  • 2
    Did you run `qmake`? *Build -> run qmake* – frogatto Feb 23 '16 at 16:57
  • How does your .pro file look like? Are your sure GpibDevicesModel.cpp is listed in SOURCES? – Frank Osterfeld Feb 23 '16 at 19:57
  • I already tried 'clean' -> 'run qmake' and 'build' mutliple times but without success. I confirm that GpibDeviceModel.cpp is listed in the .pro file (I have added the class from the Qt Creator wizard). – Gojir4 Feb 24 '16 at 08:19
  • @Niall This is not a duplicate question. Please read full thread before to make duplicate when it's not.... I don't find answer to my question in the link provided in "This question already has an answer here:" – Gojir4 Feb 24 '16 at 09:56
  • I believe it is still a duplicate, in particular [this answer](http://stackoverflow.com/a/12574400/3747990). From the answer you posted, the makefiles were not being updated correctly, that resulted in the error seen in the answer linked. There are also several other answers there that recommend to suggest cleaning the project and then rebuild; in your case these included a manual step as well. – Niall Feb 24 '16 at 10:27

1 Answers1

1

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.

Gojir4
  • 313
  • 2
  • 17