The problem goes away once you declare classes containing Q_OBJECT macro in header files. Whenever qmake is run, it parses the project's header files and generates make rules to invoke moc for those files that contain a Q_OBJECT macro.
producer.h
{
Q_OBJECT
public:
void run();
};
class Producer : public QThread
{
Q_OBJECT
public:
void run();
};
To copy to clipboard, switch view to plain text mode
producer.cpp
void Producer::run()
{
cout<<"Bye!"<<endl;
}
void Producer::run()
{
cout<<"Bye!"<<endl;
}
To copy to clipboard, switch view to plain text mode
This ensures that moc gets run as required. Notice that corresponding files must be listed in .pro file and you must always re-run qmake after adding Q_OBJECT macro to any class. Notice also that you need Q_OBJECT only if you declare custom signals and/or slots, or use other meta object stuff.
Now, if you want to put everything in a .cpp file (which is useful for minimal examples etc.) you must include the moc file by hand (and once again re-run qmake so that qmake detects it and creates proper make rules for moc):
main.cpp
{
Q_OBJECT
public:
void run()
{
cout<<"Bye!"<<endl;
}
};
int main(int argc, char *argv[])
{
...
}
#include "main.moc"
class Producer : public QThread
{
Q_OBJECT
public:
void run()
{
cout<<"Bye!"<<endl;
}
};
int main(int argc, char *argv[])
{
...
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
For more details about moc, please consult moc docs.
Originally Posted by
panos
By the way I would like to ask you how can I write something in the console (like when using the "cout" in C++)
On windows, you must add
to the .pro file (and rebuild the project) to be able to output to console. Oh, and there is qDebug() which is aware of most Qt data types, see debugging techniques for more details.
Bookmarks