0
votes

I'm having problems emitting my own struct from a signal to a slot. The struct looks as following:

WorldObjectChange.h

#pragma once

    struct WorldObjectChange  {
        WorldObjectChange() {}
        ~WorldObjectChange() {}
        double x;
    };        
    Q_DECLARE_METATYPE(WorldObjectChange)

I called qRegisterMetaType to make the type known in the signal slot method

main.c

QApplication a(argc, argv); 
qRegisterMetaType<WorldObjectChange>();

The connection is queued since signal and slot inhabit different threads. The connect happens in the constructor of a class that initializes and starts the thread for object1.

InitClass::InitClass(Object2 *object2) {
  Object1* object1 = new Object1();
  connect(object1, SIGNAL(updateObjects(WorldObjectChange)), object2, SLOT(updateObjects(WorldObjectChange)));
}

object1.h

#include "WorldObjectChange.h"

class object1 : public QObject{
    Q_OBJECT
    public:
        object1();
    public signals:
        void updateObjects(WorldObjectChange);
};

object2.h

#include "WorldObjectChange.h"

class object2: public QLabel {
    Q_OBJECT

public:
    explicit object2(QWidget * parent = 0);
    public slots:
        void updateObjects(WorldObjectChange worldChangeVector);
};

object2.cpp

void updateObjects(WorldObjectChange worldChangeVector) { }

The object seems to work fine in object1, but whenever I add the line for the slot in object 2 I get the following error:

Error LNK2019 unresolved external symbol "public: void __cdecl worldOutputGrid::updateObjects(struct WorldObjectChange)" (?updateObjects@worldOutputGrid@@QEAAXUWorldObjectChange@@@Z) referenced in function "private: static void __cdecl worldOutputGrid::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@worldOutputGrid@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)

Thanks in advance for the help

1
Has function object2::updateObjects() a definition?vahancho
WorldObjectChange doesn't have members: y, width, or height. You've obviously gotta fix that to compile. If that solves your problem I can post it as an answer, but I think fixing that will help get us to the actual issues.Jonathan Mee
I forgot to delete the parameters and the the definition in object2 exists. Changed it in the questiontodayihateprogramming
OK next question, where is the connect happening? You're not specifying the classes on the updateObjects so I'd assume that your signal and slot are both using either the signal or the slot's definition.Jonathan Mee
Linker error is indicating worldOutputGrid namespace or class .. where is that in your code?Mohammad Kanan

1 Answers

0
votes

connect takes a QObject* as it's 1st and 3rd arguments. In your code:

connect(object1, SIGNAL(updateObjects(WorldObjectChange)), object2, SLOT(updateObjects(WorldObjectChange)));

object1 is not a QObject*. You need to use instead:

connect(&object1, SIGNAL(updateObjects(WorldObjectChange)), object2, SLOT(updateObjects(WorldObjectChange)));