Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The TQt Meta Object Compiler, tqmoc among friends, is the program which handles TQt's C++ extensions.
TQMoc reads a C++ source file. If it finds one or more class declarations that contain the TQ_OBJECT macro, it produces another C++ source file which contains the meta object code for the classes that use the TQ_OBJECT macro. Among other things, meta object code is required for the signal/slot mechanism, runtime type information and the dynamic property system.
The C++ source file generated by tqmoc must be compiled and linked with the implementation of the class (or it can be #included into the class's source file).
If you use qmake to create your Makefiles, build rules will be included that call tqmoc when required, so you will not need to use tqmoc directly. For more background information on tqmoc, see Why doesn't TQt use templates for signals and slots?.
TQMoc is typically used with an input file containing class declarations like this:
class MyClass : public TQObject { TQ_OBJECT public: MyClass( TQObject * parent=0, const char * name=0 ); ~MyClass(); signals: void mySignal(); public slots: void mySlot(); };
In addition to the signals and slots shown above, tqmoc also implements object properties as in the next example. The TQ_PROPERTY macro declares an object property, while TQ_ENUMS declares a list of enumeration types within the class to be usable inside the property system. In this particular case we declare a property of the enumeration type Priority that is also called "priority" and has a get function priority() and a set function setPriority().
class MyClass : public TQObject { TQ_OBJECT TQ_PROPERTY( Priority priority READ priority WRITE setPriority ) TQ_ENUMS( Priority ) public: MyClass( TQObject * parent=0, const char * name=0 ); ~MyClass(); enum Priority { High, Low, VeryHigh, VeryLow }; void setPriority( Priority ); Priority priority() const; };
Properties can be modified in subclasses with the TQ_OVERRIDE macro. The TQ_SETS macro declares enums that are to be used as sets, i.e. OR'ed together. Another macro, TQ_CLASSINFO, can be used to attach additional name/value-pairs to the class' meta object:
class MyClass : public TQObject { TQ_OBJECT TQ_CLASSINFO( "Author", "Oscar Peterson") TQ_CLASSINFO( "Status", "Active") public: MyClass( TQObject * parent=0, const char * name=0 ); ~MyClass(); };
The three concepts, signals and slots, properties and class meta-data, can be combined.
The output produced by tqmoc must be compiled and linked, just like the other C++ code in your program; otherwise the build will fail in the final link phase. By convention, this is done in one of the following two ways:
#include "myclass.moc"at the end. This will cause the tqmoc-generated code to be compiled and linked together with the normal class definition in myclass.cpp, so it is not necessary to compile and link it separately, as in Method A.
Method A is the normal method. Method B can be used in cases where you want the implementation file to be self-contained, or in cases where the TQ_OBJECT class is implementation-internal and thus should not be visible in the header file.
For anything but the simplest test programs, it is recommended that you automate running tqmoc. By adding some rules to your program's Makefile, make can take care of running tqmoc when necessary and handling tqmoc output.
We recommend using Trolltech's free makefile generation tool, qmake, for building your Makefiles. This tool recognizes both Method A and B style source files, and generates a Makefile that does all the necessary tqmoc handling.
If you want to create your Makefiles yourself, here are some tips on how to include tqmoc handling.
For TQ_OBJECT class declarations in header files, here is a useful makefile rule if you only use GNU make:
tqmoc_%.cpp: %.h tqmoc $< -o $@
If you want to write portably, you can use individual rules with the following form:
tqmoc_NAME.cpp: NAME.h tqmoc $< -o $@
You must also remember to add tqmoc_NAME.cpp to your SOURCES (substitute your favorite name) variable and tqmoc_NAME.o or tqmoc_NAME.obj to your OBJECTS variable.
(While we prefer to name our C++ source files .cpp, tqmoc doesn't care, so you can use .C, .cc, .CC, .cxx or even .c++ if you prefer.)
For TQ_OBJECT class declarations in implementation (.cpp) files, we suggest a makefile rule like this:
NAME.o: NAME.moc NAME.moc: NAME.cpp tqmoc -i $< -o $@
This guarantees that make will run tqmoc before it compiles NAME.cpp. You can then put
#include "NAME.moc"
at the end of NAME.cpp, where all the classes declared in that file are fully known.
Here are the command-line options supported by tqmoc:
Option | Meaning |
---|---|
-o file | Write output to file rather than to stdout. |
-f | Force the generation of an #include statement in the output. This is the default for files whose name matches the regular expression \.[hH][^.]* (i.e. the extension starts with H or h). This option is only useful if you have header files that do not follow the standard naming conventions. |
-i | Do not generate an #include statement in the output. This may be used to run tqmoc on on a C++ file containing one or more class declarations. You should then #include the meta object code in the .cpp file. If both -i and -f are present, the last one wins. |
-nw | Do not generate any warnings. Not recommended. |
-ldbg | Write a flood of lex debug information to stdout. |
-p path | Makes tqmoc prepend path/ to the file name in the generated #include statement (if one is generated). |
-q path | Makes tqmoc prepend path/ to the file name of qt #include files in the generated code. |
You can explicitly tell tqmoc not to 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 tqmoc is as if you had removed all lines between a MOC_SKIP_BEGIN and a MOC_SKIP_END.
TQMoc will warn you about a number of dangerous or illegal constructs in the TQ_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 vtbl, something has been done wrong. Most often, you have forgotten to compile or #include the tqmoc-generated C++ code, or (in the former case) include that object file in the link command.
TQMoc does not expand #include or #define, it simply skips any preprocessor directives it encounters. This is regrettable, but is not usually a problem in practice.
TQMoc 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 TQFrame { TQ_OBJECT ... signals: void bugInMocDetected( int ); };
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, tqmoc assumes that the first inherited class is a subclass of TQObject. Also, be sure that only the first inherited class is a TQObject.
class SomeClass : public TQObject, public OtherClass { ... };
(This limitation is almost impossible to remove; since tqmoc does not expand #include or #define, it cannot find out which one of the base classes is a TQObject.)
In most cases where you would consider using function pointers as signal/slot arguments, we think inheritance is a better alternative. Here is an example of illegal syntax:
class SomeClass : public TQObject { TQ_OBJECT ... public slots: // illegal void apply( void (*apply)(List *, void *), char * ); };
You can work around this restriction like this:
typedef void (*ApplyFunctionType)( List *, void * ); class SomeClass : public TQObject { TQ_OBJECT ... public slots: void apply( ApplyFunctionType, char * ); };
It may sometimes be even better to replace the function pointer with inheritance and virtual functions, signals or slots.
Sometimes it will work, but in general, friend declarations cannot be placed in signals or slots sections. Put them in the private, protected or public sections instead. Here is an example of the illegal syntax:
class SomeClass : public TQObject { TQ_OBJECT ... signals: friend class ClassTemplate<char>; // WRONG };
The C++ feature of upgrading an inherited member function to public status is not extended to cover signals and slots. Here is an illegal example:
class Whatever : public TQButtonGroup { ... public slots: TQButtonGroup::buttonPressed; // WRONG ... };
The TQButtonGroup::buttonPressed() slot is protected.
C++ quiz: What happens if you try to upgrade a protected member function which is overloaded?
Since tqmoc 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 TQObject { ... signals: void someSignal( SIGNEDNESS(int) ); ... };
A #define without parameters will work as expected.
Here's an example:
class A { TQ_OBJECT public: class B { public slots: // WRONG void b(); ... }; signals: class B { // WRONG void b(); ... }: };
It is a mystery to us why anyone would put a constructor in either the signals or slots sections. You can't anyway (except that it happens to work in some cases). Put them in private, protected or public sections, where they belong. Here is an example of the illegal syntax:
class SomeClass : public TQObject { TQ_OBJECT public slots: SomeClass( TQObject *parent, const char *name ) : TQObject( parent, name ) { } // WRONG ... };
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. TQMoc will complain that it can neither find the functions nor resolve the type. Here is an example of the illegal syntax:
class SomeClass : public TQObject { TQ_OBJECT public: ... TQ_PROPERTY( Priority priority READ priority WRITE setPriority ) // WRONG TQ_ENUMS( Priority ) // WRONG 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 TQ_OBJECT:
class SomeClass : public TQObject { TQ_OBJECT TQ_PROPERTY( Priority priority READ priority WRITE setPriority ) TQ_ENUMS( Priority ) public: ... enum Priority { High, Low, VeryHigh, VeryLow }; void setPriority( Priority ); Priority priority() const; ... };
Copyright © 2007 Trolltech | Trademarks | TQt 3.3.8
|