views:

46

answers:

1

Hello folks,
I am trying to declare my Class as Metatype for Qt but figuring out some problems. It seems that after the MetaType declaration he wants to get access to a copy constructor or something like this which is explicitly not allowed for QObjects as I thought.
This is my header:

#include <QtCore/QObject>
#include <QtCore/QMetaType>

class Message : private QObject
{
  Q_OBJECT

public:
  Message();

  int sourceId;
  int targetId;

private:
  Q_DISABLE_COPY(Message)
};

Q_DECLARE_METATYPE(Message)
Q_DECLARE_METATYPE(Message*)

Message::Message() :
  QObject()
{
}  

So, I get the following errors:

Message::Message : cannot access private member declared in class Message see reference to function template instantiation 'void *qMetaTypeConstructHelper(const T *)' being compiled

and some more, but I think they are nearly the same and because of the same problem.
Someone knows what I am doing wrong?

+1  A: 

You have to publicly inherit from QObject:

class Message : public QObject

By doing that, you don't need to declare metatype for class Message. Only for the pointer.

Cătălin Pitiș
Hello, I tried inheriting public QObject. If I remove the Q_DECLARE_METATYPE(Message) and only declare the pointer, there is another problem: qt_metatype_id : is not a member of QMetaTypeId<T>, see reference to function template instantiation 'int qMetaTypeId<Message>(T *)' being compiled -- because I am using: scriptInterpreter.setDefaultPrototype(qMetaTypeId<Message>(), this->qs_msg_prototype);
Tobias
Is it also possible to declare a metatype for a class which does not inherit QObject? Because if I am trying to do that, I get the error that the staticMetaObject cannot be found in my metatype class ..
Tobias
Yes, it is (ex. declaring meta types for pointers).
Cătălin Pitiș
hmm, i cannot get it working .. if I omit the QObject inheritance, I cannot declare a pointer as metatype, if I inherit QObject and declare the pointer, Qt cannot find the qt_metatype_id() function for the non-pointer metatype .. But I need pointer and also the normal class as metatype. I try to expose a Qt class to QtScript. I am following an example out of "C++ GUI Programming with Qt4" but cant get it to work in my use case ..
Tobias