Using the Meta-Object Compiler (moc)
|
Option | Description |
---|---|
-o<file> | Write output to <file> rather than to standard output. |
-f[<file>] | Force the generation of an #include statement in the output. This is the default for header files whose extension starts with H or h. This option is useful if you have header files that do not follow the standard naming conventions. The <file> part is optional. |
-i | Do not generate an #include statement in the output. This may be used to run the moc on on a C++ file containing one or more class declarations. You should then #include the meta-object code in the .cpp file. |
-nw | Do not generate any warnings. (Not recommended.) |
-p<path> | Makes the moc prepend <path>/ to the file name in the generated #include statement. |
-I<dir> | Add dir to the include path for header files. |
-E | Preprocess only; do not generate meta-object code. |
-D<macro>[=<def>] | Define macro, with optional definition. |
-U<macro> | Undefine macro. |
@<file> | Read additional command-line options from <file>. Each line of the file is treated as a single option. Empty lines are ignored. Note that this option is not supported within the options file itself (i.e. an options file can't "include" another file). |
-h | Display the usage and the list of options. |
-v | Display moc's version number. |
-Fdir | Mac OS X. Add the framework directory dir to the head of the list of directories to be searched for header files. These directories are interleaved with those specified by -I options and are scanned in a left-to-right order (see the manpage for gcc). Normally, use -F /Library/Frameworks/ |
You can explicitly tell the moc not to parse parts of a header file. moc defines the preprocessor symbol Q_MOC_RUN. Any code surrounded by
#ifndef Q_MOC_RUN ... #endif
is skipped by the moc.
moc will warn you about a number of dangerous or illegal constructs in the Q_OBJECT class declarations.
If you get linkage errors in the final building phase of your program, saying that YourClass::className() is undefined or that YourClass lacks a vtable, something has been done wrong. Most often, you have forgotten to compile or #include the moc-generated C++ code, or (in the former case) include that object file in the link command. If you use qmake, try rerunning it to update your makefile. This should do the trick.
moc does not handle all of C++. The main problem is that class templates cannot have signals or slots. Here is an example:
class SomeTemplate<int> : public QFrame { Q_OBJECT ... signals: void mySignal(int); };
Another limitation is that moc does not expand macros, so you for example cannot use a macro to declare a signal/slot or use one to define a base class for a QObject.
Less importantly, the following constructs are illegal. All of them have alternatives which we think are usually better, so removing these limitations is not a high priority for us.
If you are using multiple inheritance, moc assumes that the first inherited class is a subclass of QObject. Also, be sure that only the first inherited class is a QObject.
// correct class SomeClass : public QObject, public OtherClass { ... };
Virtual inheritance with QObject is not supported.
In most cases where you would consider using function pointers as signal or slot parameters, we think inheritance is a better alternative. Here is an example of illegal syntax:
class SomeClass : public QObject { Q_OBJECT public slots: void apply(void (*apply)(List *, void *), char *); // WRONG };
You can work around this restriction like this:
typedef void (*ApplyFunction)(List *, void *); class SomeClass : public QObject { Q_OBJECT public slots: void apply(ApplyFunction, char *); };
It may sometimes be even better to replace the function pointer with inheritance and virtual functions.
When checking the signatures of its arguments, QObject::connect() compares the data types literally. Thus, Alignment and Qt::Alignment are treated as two distinct types. To work around this limitation, make sure to fully qualify the data types when declaring signals and slots, and when establishing connections. For example:
class MyClass : public QObject { Q_OBJECT enum Error { ConnectionRefused, RemoteHostClosed, UnknownError }; signals: void stateChanged(MyClass::Error error); };
Since moc doesn't expand #defines, 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 { Q_OBJECT signals: void someSignal(SIGNEDNESS(int)); };
A macro without parameters will work.
Here's an example of the offending construct:
class A { public: class B { Q_OBJECT public slots: // WRONG void b(); }; };
Signals and slots can have return types, but signals or slots returning references will be treated as returning void.
moc will complain if you try to put other constructs in the signals or slots sections of a class than signals and slots.
See also Meta-Object System, Signals and Slots, and Qt's Property System.
Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) | Trademarks | Qt 4.5.2 |
Попытка перевода Qt документации. Если есть желание присоединиться, или если есть замечания или пожелания, то заходите на форум: Перевод Qt документации на русский язык... Люди внесшие вклад в перевод: Команда переводчиков |