You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
514 lines
15 KiB
514 lines
15 KiB
13 years ago
|
/****************************************************************************
|
||
|
**
|
||
2 months ago
|
** TQMoc usage, expanded from the manual page
|
||
13 years ago
|
**
|
||
|
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
|
||
|
**
|
||
10 years ago
|
** This file is part of the TQt GUI Toolkit.
|
||
13 years ago
|
**
|
||
|
** This file may be used under the terms of the GNU General
|
||
|
** Public License versions 2.0 or 3.0 as published by the Free
|
||
|
** Software Foundation and appearing in the files LICENSE.GPL2
|
||
|
** and LICENSE.GPL3 included in the packaging of this file.
|
||
|
** Alternatively you may (at your option) use any later version
|
||
|
** of the GNU General Public License if such license has been
|
||
|
** publicly approved by Trolltech ASA (or its successors, if any)
|
||
10 years ago
|
** and the KDE Free TQt Foundation.
|
||
13 years ago
|
**
|
||
|
** Please review the following information to ensure GNU General
|
||
13 years ago
|
** Public Licensing requirements will be met:
|
||
13 years ago
|
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
|
||
|
** If you are unsure which license is appropriate for your use, please
|
||
|
** review the following information:
|
||
|
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
|
||
|
** or contact the sales department at sales@trolltech.com.
|
||
|
**
|
||
|
** This file may be used under the terms of the Q Public License as
|
||
|
** defined by Trolltech ASA and appearing in the file LICENSE.QPL
|
||
|
** included in the packaging of this file. Licensees holding valid Qt
|
||
|
** Commercial licenses may use this file in accordance with the Qt
|
||
|
** Commercial License Agreement provided with the Software.
|
||
|
**
|
||
|
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
|
||
|
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
|
||
|
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
|
||
|
** herein.
|
||
|
**
|
||
|
**********************************************************************/
|
||
|
|
||
2 months ago
|
// Note: Text in this file must also be copied to the tqmoc man page:
|
||
|
// src/tqmoc/tqmoc.1
|
||
13 years ago
|
|
||
|
/*!
|
||
2 months ago
|
\page tqmoc.html
|
||
13 years ago
|
|
||
|
\title Using the Meta Object Compiler
|
||
|
|
||
2 months ago
|
\keyword tqmoc
|
||
13 years ago
|
|
||
2 months ago
|
The Meta Object Compiler, tqmoc among friends, is the program which
|
||
|
handles TQt's \link metaobjects.html C++ extensions.\endlink
|
||
13 years ago
|
|
||
2 months ago
|
TQMoc reads a C++ source file. If it finds one or more class
|
||
13 years ago
|
declarations that contain the TQ_OBJECT macro, it produces another
|
||
13 years ago
|
C++ source file which contains the meta object code for the classes
|
||
13 years ago
|
that use the TQ_OBJECT macro. Among other things, meta object code is
|
||
13 years ago
|
required for the signal/slot mechanism, runtime type information and
|
||
13 years ago
|
the dynamic property system.
|
||
|
|
||
2 months ago
|
The C++ source file generated by tqmoc must be compiled and linked
|
||
13 years ago
|
with the implementation of the class (or it can be #included into the
|
||
|
class's source file).
|
||
|
|
||
|
If you use \link qmake-manual.book qmake\endlink to create your
|
||
2 months ago
|
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 \link templates.html Why doesn't Qt
|
||
13 years ago
|
use templates for signals and slots?\endlink.
|
||
|
|
||
|
\section1 Usage
|
||
|
|
||
2 months ago
|
TQMoc is typically used with an input file containing class declarations
|
||
13 years ago
|
like this:
|
||
|
|
||
|
\code
|
||
1 year ago
|
class MyClass : public TQObject
|
||
13 years ago
|
{
|
||
13 years ago
|
TQ_OBJECT
|
||
13 years ago
|
public:
|
||
1 year ago
|
MyClass( TQObject * parent=0, const char * name=0 );
|
||
13 years ago
|
~MyClass();
|
||
|
|
||
|
signals:
|
||
|
void mySignal();
|
||
|
|
||
|
public slots:
|
||
|
void mySlot();
|
||
|
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
2 months ago
|
In addition to the signals and slots shown above, tqmoc also
|
||
12 years ago
|
implements object properties as in the next example. The TQ_PROPERTY
|
||
|
macro declares an object property, while TQ_ENUMS declares a list of
|
||
13 years ago
|
enumeration types within the class to be usable inside the
|
||
|
\link properties.html property system\endlink. In this particular
|
||
|
case we declare a property of the enumeration type \c Priority that is
|
||
|
also called "priority" and has a get function \c priority() and a set
|
||
|
function \c setPriority().
|
||
|
|
||
|
\code
|
||
1 year ago
|
class MyClass : public TQObject
|
||
13 years ago
|
{
|
||
13 years ago
|
TQ_OBJECT
|
||
12 years ago
|
TQ_PROPERTY( Priority priority READ priority WRITE setPriority )
|
||
|
TQ_ENUMS( Priority )
|
||
13 years ago
|
public:
|
||
1 year ago
|
MyClass( TQObject * parent=0, const char * name=0 );
|
||
13 years ago
|
~MyClass();
|
||
|
|
||
|
enum Priority { High, Low, VeryHigh, VeryLow };
|
||
|
void setPriority( Priority );
|
||
|
Priority priority() const;
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
12 years ago
|
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
|
||
13 years ago
|
attach additional name/value-pairs to the class' meta object:
|
||
|
|
||
|
\code
|
||
1 year ago
|
class MyClass : public TQObject
|
||
13 years ago
|
{
|
||
13 years ago
|
TQ_OBJECT
|
||
12 years ago
|
TQ_CLASSINFO( "Author", "Oscar Peterson")
|
||
|
TQ_CLASSINFO( "Status", "Active")
|
||
13 years ago
|
public:
|
||
1 year ago
|
MyClass( TQObject * parent=0, const char * name=0 );
|
||
13 years ago
|
~MyClass();
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
|
The three concepts, signals and slots, properties and class
|
||
|
meta-data, can be combined.
|
||
|
|
||
2 months ago
|
The output produced by tqmoc must be compiled and linked, just like
|
||
13 years ago
|
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:
|
||
|
|
||
|
<dl>
|
||
|
|
||
|
<dt><b>Method A: The class declaration is found in a header
|
||
|
(\e .h) file</b>
|
||
|
|
||
|
<dd>If the class declaration above is found in the file
|
||
2 months ago
|
\e myclass.h, tqmoc output should be put in a file called
|
||
|
\e tqmoc_myclass.cpp. This file should then be compiled as
|
||
|
usual, resulting in an object file \e tqmoc_myclass.o (on Unix)
|
||
|
or \e tqmoc_myclass.obj (on Windows). This object should then be
|
||
13 years ago
|
included in the list of object files that are linked together in the
|
||
|
final building phase of the program.
|
||
|
|
||
|
|
||
|
<dt><b>Method B: The class declaration is found in an implementation
|
||
|
(\e .cpp) file</b>
|
||
|
|
||
|
<dd>If the class declaration above is found in the file
|
||
2 months ago
|
\e myclass.cpp, tqmoc output should be put in a file called
|
||
13 years ago
|
\e myclass.moc. This file should be #included in the
|
||
|
implementation file, i.e. \e myclass.cpp should contain the
|
||
|
line
|
||
|
\code
|
||
|
#include "myclass.moc"
|
||
|
\endcode
|
||
2 months ago
|
at the end. This will cause the tqmoc-generated code to be compiled and
|
||
13 years ago
|
linked together with the normal class definition in \e myclass.cpp, so
|
||
|
it is not necessary to compile and link it separately, as in Method A.
|
||
|
|
||
|
</dl>
|
||
|
|
||
|
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
|
||
13 years ago
|
the TQ_OBJECT class is implementation-internal and thus should not be
|
||
13 years ago
|
visible in the header file.
|
||
|
|
||
|
|
||
2 months ago
|
\section1 Automating tqmoc Usage with Makefiles
|
||
13 years ago
|
|
||
|
For anything but the simplest test programs, it is recommended that
|
||
2 months ago
|
you automate running the tqmoc. By adding some rules to your program's
|
||
|
Makefile, \e make can take care of running tqmoc when necessary and
|
||
|
handling tqmoc output.
|
||
13 years ago
|
|
||
|
We recommend using Trolltech's free makefile generation tool, \link
|
||
|
qmake-manual.book qmake\endlink, for building your Makefiles. This tool
|
||
|
recognizes both Method A and B style source files, and generates a
|
||
2 months ago
|
Makefile that does all the necessary tqmoc handling.
|
||
13 years ago
|
|
||
|
If you want to create your Makefiles yourself, here are some tips on
|
||
2 months ago
|
how to include tqmoc handling.
|
||
13 years ago
|
|
||
13 years ago
|
For TQ_OBJECT class declarations in header files, here is a useful
|
||
13 years ago
|
makefile rule if you only use GNU make:
|
||
|
|
||
|
\code
|
||
2 months ago
|
tqmoc_%.cpp: %.h
|
||
|
tqmoc $< -o $@
|
||
13 years ago
|
\endcode
|
||
|
|
||
|
If you want to write portably, you can use individual rules with the
|
||
|
following form:
|
||
|
|
||
|
\code
|
||
2 months ago
|
tqmoc_NAME.cpp: NAME.h
|
||
|
tqmoc $< -o $@
|
||
13 years ago
|
\endcode
|
||
|
|
||
2 months ago
|
You must also remember to add \e tqmoc_NAME.cpp to your SOURCES
|
||
|
(substitute your favorite name) variable and \e tqmoc_NAME.o or
|
||
|
\e tqmoc_NAME.obj to your OBJECTS variable.
|
||
13 years ago
|
|
||
2 months ago
|
(While we prefer to name our C++ source files .cpp, tqmoc doesn't
|
||
13 years ago
|
care, so you can use .C, .cc, .CC, .cxx or even .c++ if you
|
||
|
prefer.)
|
||
|
|
||
13 years ago
|
For TQ_OBJECT class declarations in implementation (.cpp) files, we
|
||
13 years ago
|
suggest a makefile rule like this:
|
||
|
|
||
|
\code
|
||
|
NAME.o: NAME.moc
|
||
|
|
||
|
NAME.moc: NAME.cpp
|
||
2 months ago
|
tqmoc -i $< -o $@
|
||
13 years ago
|
\endcode
|
||
|
|
||
2 months ago
|
This guarantees that make will run tqmoc before it compiles
|
||
13 years ago
|
\e NAME.cpp. You can then put
|
||
|
|
||
|
\code
|
||
|
#include "NAME.moc"
|
||
|
\endcode
|
||
|
|
||
|
at the end of \e NAME.cpp, where all the classes declared in
|
||
|
that file are fully known.
|
||
|
|
||
|
|
||
2 months ago
|
\section1 Invoking tqmoc
|
||
13 years ago
|
|
||
2 months ago
|
Here are the command-line options supported by tqmoc:
|
||
13 years ago
|
|
||
|
\table
|
||
|
\header \i Option \i Meaning
|
||
|
\row
|
||
|
\i -o \e file
|
||
|
\i Write output to \e file rather than to stdout.
|
||
|
\row
|
||
|
\i -f
|
||
|
\i 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.
|
||
|
\row
|
||
|
\i -i
|
||
|
\i Do not generate an #include statement in the output.
|
||
2 months ago
|
This may be used to run tqmoc on on a C++ file containing one or
|
||
13 years ago
|
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.
|
||
|
\row
|
||
|
\i -nw
|
||
|
\i Do not generate any warnings. Not recommended.
|
||
|
\row
|
||
|
\i -ldbg
|
||
|
\i Write a flood of lex debug information to stdout.
|
||
|
\row
|
||
|
\i -p \e path
|
||
2 months ago
|
\i Makes tqmoc prepend \e {path}/ to
|
||
13 years ago
|
the file name in the generated #include statement (if one is
|
||
|
generated).
|
||
|
\row
|
||
|
\i -q \e path
|
||
2 months ago
|
\i Makes tqmoc prepend \e {path}/ to
|
||
13 years ago
|
the file name of qt #include files in the generated code.
|
||
|
\endtable
|
||
|
|
||
2 months ago
|
You can explicitly tell tqmoc not to parse parts of a header
|
||
13 years ago
|
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
|
||
2 months ago
|
can have several levels of them. The net result as seen by tqmoc is
|
||
13 years ago
|
as if you had removed all lines between a MOC_SKIP_BEGIN and a
|
||
|
MOC_SKIP_END.
|
||
|
|
||
|
|
||
|
\section1 Diagnostics
|
||
|
|
||
2 months ago
|
TQMoc will warn you about a number of dangerous or illegal
|
||
13 years ago
|
constructs in the TQ_OBJECT class declarations.
|
||
13 years ago
|
|
||
|
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,
|
||
2 months ago
|
you have forgotten to compile or #include the tqmoc-generated C++ code, or
|
||
13 years ago
|
(in the former case) include that object file in the link command.
|
||
|
|
||
|
|
||
|
\section1 Limitations
|
||
|
|
||
2 months ago
|
TQMoc does not expand #include or #define, it simply skips any
|
||
13 years ago
|
preprocessor directives it encounters. This is regrettable, but is
|
||
|
not usually a problem in practice.
|
||
|
|
||
2 months ago
|
TQMoc does not handle all of C++. The main problem is that class
|
||
13 years ago
|
templates cannot have signals or slots. Here is an example:
|
||
|
|
||
|
\code
|
||
4 months ago
|
class SomeTemplate<int> : public TQFrame {
|
||
13 years ago
|
TQ_OBJECT
|
||
13 years ago
|
...
|
||
|
signals:
|
||
|
void bugInMocDetected( int );
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
|
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.
|
||
|
|
||
1 year ago
|
\section2 Multiple inheritance requires TQObject to be first
|
||
13 years ago
|
|
||
2 months ago
|
If you are using multiple inheritance, tqmoc assumes that the \e first
|
||
1 year ago
|
inherited class is a subclass of TQObject. Also, be sure that \e only
|
||
|
the first inherited class is a TQObject.
|
||
13 years ago
|
|
||
|
\code
|
||
1 year ago
|
class SomeClass : public TQObject, public OtherClass {
|
||
13 years ago
|
...
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
2 months ago
|
(This limitation is almost impossible to remove; since tqmoc does not expand
|
||
13 years ago
|
#include or #define, it cannot find out which one of the base classes
|
||
1 year ago
|
is a TQObject.)
|
||
13 years ago
|
|
||
|
\section2 Function pointers cannot be arguments to signals or slots
|
||
|
|
||
|
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:
|
||
|
|
||
|
\code
|
||
1 year ago
|
class SomeClass : public TQObject {
|
||
13 years ago
|
TQ_OBJECT
|
||
13 years ago
|
...
|
||
|
public slots:
|
||
|
// illegal
|
||
|
void apply( void (*apply)(List *, void *), char * );
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
|
You can work around this restriction like this:
|
||
|
\code
|
||
|
typedef void (*ApplyFunctionType)( List *, void * );
|
||
|
|
||
1 year ago
|
class SomeClass : public TQObject {
|
||
13 years ago
|
TQ_OBJECT
|
||
13 years ago
|
...
|
||
|
public slots:
|
||
|
void apply( ApplyFunctionType, char * );
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
|
It may sometimes be even better to replace the function pointer with
|
||
|
inheritance and virtual functions, signals or slots.
|
||
|
|
||
|
\section2 Friend declarations cannot be placed in signals or slots sections
|
||
|
|
||
|
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:
|
||
|
|
||
|
\code
|
||
1 year ago
|
class SomeClass : public TQObject {
|
||
13 years ago
|
TQ_OBJECT
|
||
13 years ago
|
...
|
||
|
signals:
|
||
|
friend class ClassTemplate<char>; // WRONG
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
|
\section2 Signals and slots cannot be upgraded
|
||
|
|
||
|
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:
|
||
|
|
||
|
\code
|
||
4 months ago
|
class Whatever : public TQButtonGroup {
|
||
13 years ago
|
...
|
||
|
public slots:
|
||
4 months ago
|
TQButtonGroup::buttonPressed; // WRONG
|
||
13 years ago
|
...
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
4 months ago
|
The TQButtonGroup::buttonPressed() slot is protected.
|
||
13 years ago
|
|
||
12 years ago
|
C++ quiz: What happens if you try to upgrade a protected member
|
||
13 years ago
|
function which is overloaded?
|
||
|
\list 1
|
||
|
\i All the functions are overloaded.
|
||
|
\i That is not legal C++.
|
||
|
\endlist
|
||
|
|
||
|
\omit
|
||
|
C++ ARM, section r.11.3
|
||
|
\endomit
|
||
|
|
||
|
\section2 Type macros cannot be used for signal and slot parameters
|
||
|
|
||
2 months ago
|
Since tqmoc does not expand #define, type macros that take an argument
|
||
13 years ago
|
will not work in signals and slots. Here is an illegal example:
|
||
|
|
||
|
\code
|
||
|
#ifdef ultrix
|
||
|
#define SIGNEDNESS(a) unsigned a
|
||
|
#else
|
||
|
#define SIGNEDNESS(a) a
|
||
|
#endif
|
||
|
|
||
1 year ago
|
class Whatever : public TQObject {
|
||
13 years ago
|
...
|
||
|
signals:
|
||
|
void someSignal( SIGNEDNESS(int) );
|
||
|
...
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
|
A #define without parameters will work as expected.
|
||
|
|
||
|
\section2 Nested classes cannot be in the signals or slots sections nor have
|
||
|
signals or slots
|
||
|
|
||
|
Here's an example:
|
||
|
|
||
|
\code
|
||
|
class A {
|
||
13 years ago
|
TQ_OBJECT
|
||
13 years ago
|
public:
|
||
|
class B {
|
||
|
public slots: // WRONG
|
||
|
void b();
|
||
|
...
|
||
|
};
|
||
|
signals:
|
||
|
class B { // WRONG
|
||
|
void b();
|
||
|
...
|
||
|
}:
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
|
\section2 Constructors cannot be used in signals or slots sections
|
||
|
|
||
|
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:
|
||
|
|
||
|
\code
|
||
1 year ago
|
class SomeClass : public TQObject {
|
||
13 years ago
|
TQ_OBJECT
|
||
13 years ago
|
public slots:
|
||
1 year ago
|
SomeClass( TQObject *parent, const char *name )
|
||
|
: TQObject( parent, name ) { } // WRONG
|
||
13 years ago
|
...
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
|
\section2 Properties need to be declared before the public section that
|
||
|
contains the respective get and set functions
|
||
|
|
||
|
Declaring the first property within or after the public section that
|
||
|
contains the type definition and the respective get and set functions
|
||
2 months ago
|
does not work as expected. TQMoc will complain that it can neither
|
||
13 years ago
|
find the functions nor resolve the type. Here is an example of the
|
||
|
illegal syntax:
|
||
|
|
||
|
\code
|
||
1 year ago
|
class SomeClass : public TQObject {
|
||
13 years ago
|
TQ_OBJECT
|
||
13 years ago
|
public:
|
||
|
...
|
||
12 years ago
|
TQ_PROPERTY( Priority priority READ priority WRITE setPriority ) // WRONG
|
||
|
TQ_ENUMS( Priority ) // WRONG
|
||
13 years ago
|
enum Priority { High, Low, VeryHigh, VeryLow };
|
||
|
void setPriority( Priority );
|
||
|
Priority priority() const;
|
||
|
...
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
|
Work around this limitation by declaring all properties at the
|
||
13 years ago
|
beginning of the class declaration, right after TQ_OBJECT:
|
||
13 years ago
|
|
||
|
\code
|
||
1 year ago
|
class SomeClass : public TQObject {
|
||
13 years ago
|
TQ_OBJECT
|
||
12 years ago
|
TQ_PROPERTY( Priority priority READ priority WRITE setPriority )
|
||
|
TQ_ENUMS( Priority )
|
||
13 years ago
|
public:
|
||
|
...
|
||
|
enum Priority { High, Low, VeryHigh, VeryLow };
|
||
|
void setPriority( Priority );
|
||
|
Priority priority() const;
|
||
|
...
|
||
|
};
|
||
|
\endcode
|
||
|
|
||
|
*/
|