PDA

View Full Version : Q_PROPERTY: the example doesn't work!



YaK
7th July 2009, 06:40
I am trying to build the example from documentation, but it fails:

#include <QtCore>

#include <iostream>
using namespace std;

class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(Priority priority READ priority WRITE setPriority)
Q_ENUMS(Priority)

public:
MyClass(QObject *parent = 0)
:QObject(parent)
{

};
~MyClass()
{
cout << "MyClass Destructor"<<endl;
};

enum Priority { High, Low, VeryHigh, VeryLow };

void setPriority(Priority priority);
Priority priority() const;
};


int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyClass megaInst;
return a.exec();
}

My VS writes me:

1>main.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall MyClass::metaObject(void)const " (?metaObject@MyClass@@UBEPBUQMetaObject@@XZ)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall MyClass::qt_metacast(char const *)" (?qt_metacast@MyClass@@UAEPAXPBD@Z)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall MyClass::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MyClass@@UAEHW4Call@QMetaObject@@HPA PAX@Z)
How can it be fixed?

gsmiko
7th July 2009, 07:26
Hi!

Exactly, it's just an example, to give you an idea to how it might work.
To make this code compile, put the class in a header file and provide definition for the setPriority and priority functions.
It's not required but it's a good practice to separate declaration from definition, to put the class definition in a separate source file ;)

wysota
7th July 2009, 11:05
If you're using QMake then place a "#include main.moc" entry just before the main function, rerun qmake and recompile. By default qmake only scans header files for Q_OBJECT macros.

savostin
7th July 2009, 12:28
Hello All,

Could anybody answer me also. Is the Q_PARAMETER the way to do the following:

megaInst.priority = MyClass::VeryHigh;
or it always have to be
megaInst.setPriority(MyClass::VeryHigh); (I mean a variable imitation but not function call). If not, what a purpose of Q_PROPERTY if you have to reach property as function?

Or is there any other way to do that? :confused:

Thanks