qtqmlqmetatype

QML, How to correctly expose c++ type - got "undefined symbol"


I have a class that returns a list of custom objescts, to view in qml

#include <QObject>
#include "networkinterface.h"

class Network : public QObject {
    Q_OBJECT

public:
    explicit Network(QObject *parent = 0);
    Q_INVOKABLE QList<NetworkInterface> getNetworkAdaptors();

private:
    QList<NetworkInterface> networkAdaptors; };

At main.qml i call this method as

model: network.getNetworkAdaptors()

It all was working when NetworkInterface was a struct, but when i converted it to a class, can't make it work.

Class NetworkInterface is inherited from QObject and got properties

class NetworkInterface : public QObject
{
    Q_OBJECT

public:
    NetworkInterface();

    QString name;
    QString description;

    const QString &getName() const;
    void setName(const QString &newName);

    const QString &getDescription() const;
    void setDescription(const QString &newDescription);

...
private:
    Q_PROPERTY(QString name READ getName CONSTANT)
    Q_PROPERTY(QString description READ getDescription CONSTANT)
};

So the error i got is :

main.cpp.o:-1: error: Undefined symbols for architecture x86_64:
  "NetworkInterface::NetworkInterface()", referenced from:
      QtPrivate::QMetaTypeForType<NetworkInterface>::getDefaultCtr()::'lambda'(QtPrivate::QMetaTypeInterface const*, void*)::operator()(QtPrivate::QMetaTypeInterface const*, void*) const in mocs_compilation.cpp.o
      Network::getNetworkAdaptors() in network.cpp.o
      _main in main.cpp.o

I suspect it is wrong type expose, as with sctruct it was working fine, how to do that correctly?

UPD: in NetworkInterface i have following constructors:

 NetworkInterface();

    NetworkInterface(const NetworkInterface &obj);
    NetworkInterface & operator=( const NetworkInterface & obj);

Without them i can't push_back(networkInterface) to the list of interfaces, as it requires copy constructor. Also list of pointers won't work for qml as a model, it must be exactly objects list.

When i copy all code in NetworkInterface and leave only above constructors, it is minimal code that gives a error.


Solution

  • Default constructor was missing, adding

    NetworkInterface::NetworkInterface(QObject *parent) : QObject(parent)
    {
    
    }
    

    Resoled a problem