moc - generate Qt meta object support code
In brief, the meta object system is a structure used by Qt (see http://doc.trolltech.com) for component programming and run time type information. It adds properties and inheritance information to (some) classes and provides a new type of communication between those instances of those classes, signal-slot connections.
You can explicitely tell the moc to not parse parts of a header file. It recognizes any C++ comment (//) that contains the substrings MOC_SKIP_BEGIN or MOC_SKIP_END. They work as you would expect and you can have several levels of them. The net result as seen by the moc is as if you had removed all lines between a MOC_SKIP_BEGIN and a MOC_SKIP_END
moc is typically used with an input file containing class declarations like this skeleton:
class YourClass: public QObject { Q_OBJECT Q_PROPERTY( ... ) Q_CLASSINFO( ... ) public: YourClass( QObject * parent=0, const char * name=0 ); ~YourClass(); signals: public slots: };
Here is a useful makefile rule if you only use GNU make:
m%.cpp: %.h moc $< -o $@
If you want to write portably, you can use individual rules of the following form:
mNAME.cpp: NAME.h moc $< -o $@
You must also remember to add mNAME.cpp to your SOURCES (substitute your favorite name) variable and mNAME.o to your OBJECTS variable.
(While we prefer to name our C++ source files .cpp, the moc doesn't know that, so you can use .C, .cc, .CC, .cxx or even .c++ if you prefer.)
If you have class declarations in C++ files, we recommend that you use a makefile rule like this:
NAME.o: mNAME.cpp mNAME.cpp: NAME.cpp moc -i $< -o $@
This guarantees that make(1) will run the moc before it compiles NAME.cpp. You can then put
#include "nNAME.cpp"
at the end of NAME.cpp, where all the classes declared in that file are fully known.
The moc will warn you about a number of dangerous or illegal constructs.
The moc does not expand #include or #define, it simply skips any preprocessor directives it encounters. This is regrettable, but is normally not a problem in practice.
The moc does not handle all of C++. The main problem is that class templates cannot have signals or slots. This is an important bug. Here is an example:
class SomeTemplate<int>: public QFrame { Q_OBJECT [...] signals: void bugInMocDetected( int ); };
Less importantly, the following constructs are illegal. All of them have have alternatives which we think are usually better, so removing these limitations is not a high priority for us.
class SomeClass: public QObject, public OtherClass { [...] };
This bug is almost impossible to fix; since the moc does not expand #include or #define, it cannot find out which one of the base classes is a QObject.
class Wrong: virtual public QObject, virtual public Other { [...] }; class Right: public QObject, virtual public Other { [...] };
The following example shows one wrong and two correct slot definitions.
class BaseClass { [...] virtual void setValue( int ); }; class SubClass: public QObject, public BaseClass { [...] public slots: void setValue( int ); // illegal void slotSetValue( int i ) { setValue(i); } // ok void setName( const char* ); // ok };
(For those interested in C++ internals: The cause of this problem is that a slot is internally represented as a function pointer, and invoked on a QObject pointer. )
class someClass: public QObject { Q_OBJECT [...] public slots: void apply(void (*ApplyFunction)(QList*, void*)); // illegal };
You can work around this restriction like this:
typedef void (*ApplyFunctionType)(QList*, void*); class someClass: public QObject { Q_OBJECT [...] public slots: void apply( ApplyFunctionType ); };
(It may sometimes be even better to replace the function pointer with inheritance and virtual functions, signals or slots.)
class someClass: public QObject { Q_OBJECT [...] signals: friend class ClassTemplate<char>; // illegal };
class Whatever: public QButtonGroup { [...] public slots: void QButtonGroup::buttonPressed(); // illegal };
The QButtonGroup::buttonPressed() slot is protected.
C++ quiz: What happens if you try to upgrade a protected member function which is overloaded?
Since the moc does not expand #define, type macros that take an argument will not work in signals and slots. Here is an illegal example:
#ifdef ultrix #define SIGNEDNESS(a) unsigned a #else #define SIGNEDNESS(a) a #endif class Whatever: public QObject { [...] signals: void someSignal( SIGNEDNESS(a) ); // illegal }; A #define without arguments works.
class A { Q_OBJECT public: class B { public slots: // illegal void b(); [....] }; signals: class B { // illegal void b(); [....] }: };
class SomeClass: public QObject { Q_OBJECT public slots: SomeClass( QObject *parent, const char *name ) : QObject( parent, name ) {} // illegal [...] };
class SomeClass: public QObject { Q_OBJECT public slots: void someSlot(int x=100); // illegal };
Declaring signals and slots with template-type parameters will not work as expected, even though the moc will not complain. Connecting the signal to the slot in the following example, the slot will not get executed when the signal is emitted:
public slots: void MyWidget::setLocation(pair<int,int> location); // illegal [...] public signals: void MyObject::moved(pair<int,int> location); // illegal
However, you can work around this limitation by explicitly typedef'ing the parameter types, like this:
typedef pair<int,int> IntPair; public slots: void MyWidget::setLocation(IntPair location); [...] public signals: void MyObject::moved(IntPair location);
This will work as expected.
In the following example, classes x::A and x::B are defined:
namespace x { class A : public QObject { Q_OBJECT public: ... }; } namespace x { class B : public A { Q_OBJECT public: ... }; }
Unfortunately, moc will not understand the
class B : public A {
line. You have either to write
class B : public x::A {
or define classes A and B in the same namespace block.
This limitation will disappear with Qt 3.0.
Declaring the first property within or after the public section that contains the type definition and the respective get and set functions does not work as expected. The moc will complain that it can neither find the functions nor resolve the type. Here is an example of the illegal syntax:
class SomeClass: public QObject { Q_OBJECT public: [...] // illegal Q_PROPERTY( Priority priority READ priority WRITE setPriority ) Q_ENUMS( Priority ) enum Priority { High, Low, VeryHigh, VeryLow }; void setPriority( Priority ); Priority priority() const; [...] };
Work around this limitation by declaring all properties at the beginning of the class declaration, right after Q_OBJECT:
class SomeClass: public QObject { Q_OBJECT Q_PROPERTY( Priority priority READ priority WRITE setPriority ) Q_ENUMS( Priority ) public: [...] enum Priority { High, Low, VeryHigh, VeryLow }; void setPriority( Priority ); Priority priority() const; [...] };
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |