Update copyright headers
[qt:qt.git] / config.profiles / harmattan / manpages / moc.1
1 .TH moc 1 "24 June 2001" "Nokia Corporation and/or its subsidiary(-ies)" \" -*- nroff -*-
2 .\"
3 .\" Copyright (C) 2015 The Qt Company Ltd.
4 .\" Contact: http://www.qt.io/licensing/
5 .\"
6 .\" This file is part of the QtGui module of the Qt Toolkit.
7 .\"
8 .\" $QT_BEGIN_LICENSE:LGPL$
9 .\" Commercial License Usage
10 .\" Licensees holding valid commercial Qt licenses may use this file in
11 .\" accordance with the commercial license agreement provided with the
12 .\" Software or, alternatively, in accordance with the terms contained in
13 .\" a written agreement between you and The Qt Company. For licensing terms
14 .\" and conditions see http://www.qt.io/terms-conditions. For further
15 .\" information use the contact form at http://www.qt.io/contact-us.
16 .\"
17 .\" GNU Lesser General Public License Usage
18 .\" Alternatively, this file may be used under the terms of the GNU Lesser
19 .\" General Public License version 2.1 as published by the Free Software
20 .\" Foundation and appearing in the file LICENSE.LGPL included in the
21 .\" packaging of this file.  Please review the following information to
22 .\" ensure the GNU Lesser General Public License version 2.1 requirements
23 .\" will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 .\"
25 .\" As a special exception, The Qt Company gives you certain additional
26 .\" rights. These rights are described in The Qt Company LGPL Exception
27 .\" version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 .\"
29 .\" GNU General Public License Usage
30 .\" Alternatively, this file may be used under the terms of the GNU
31 .\" General Public License version 3.0 as published by the Free Software
32 .\" Foundation and appearing in the file LICENSE.GPL included in the
33 .\" packaging of this file.  Please review the following information to
34 .\" ensure the GNU General Public License version 3.0 requirements will be
35 .\" met: http://www.gnu.org/copyleft/gpl.html.
36 .\"
37 .\"
38 .\" $QT_END_LICENSE$
39 .\"
40 .nh
41 .SH NAME
42 moc \- generate Qt meta object support code
43 .SH SYNOPSIS
44 .B moc
45 [\-o file] [\-i] [\-f] [\-k] [\-ldbg] [\-nw] [\-p path] [\-q path] [\-v] file
46 .SH DESCRIPTION
47 This page documents the
48 .B Meta Object Compiler
49 for the Qt GUI application framework. The
50 .B moc
51 reads one or more C++ class declarations from a C++ header or source
52 file and generates one C++ source file containing meta object
53 information for the classes. The C++ source file generated by the
54 .B moc
55 must be compiled and linked with the implementation of the class (or it
56 can be #included into the class's source file).
57 .PP
58 If you use 
59 .B qmake
60 to create your Makefiles, build rules will be included that call the
61 .B moc
62 when required, so you will not need to use the 
63 .B moc
64 directly.
65 .PP
66 In brief, the meta object system is a structure used by Qt (see
67 .BR http://doc.trolltech.com ")"
68 for component programming and run time type information.  It adds
69 properties and inheritance information to (some) classes and
70 provides a new type of communication between those instances of those
71 classes, signal\-slot
72 connections.
73 .SH OPTIONS
74 .TP
75 .I "\-o file"
76 Write output to
77 .I file
78 rather than to stdout.
79 .TP
80 .I \-f
81 Force the generation of an #include statement in the output.
82 This is the default for files whose name matches the regular
83 expression .[hH][^.]* (i.e. the extension starts with
84 .B H
85 or
86 .B h
87 ). This
88 option is only useful if you have header files that do not follow the
89 standard naming conventions.
90 .TP
91 .I "\-i"
92 Do not generate an #include statement in the output.  This may be used
93 to run
94 .B moc
95 on a C++ file containing one or more class declarations. You should then
96 #include the meta object code in the .cpp file (see USAGE below).  If both
97 .I \-f
98 and
99 .I \-i
100 are present, the last one wins.
101 .TP
102 .I "\-nw"
103 Do not generate any warnings. Not recommended.
104 .TP
105 .I "\-ldbg"
106 Write a flood of lex debug information to stdout.
107 .TP
108 .I "\-p path"
109 Makes
110 .B moc
111 prepend
112 .IR path /
113 to the file name in the generated #include statement (if one is generated).
114 .TP
115 .I "\-q path"
116 Makes
117 .B moc
118 prepend
119 .IR path /
120 to the file name of qt #include files in the generated code.
121 .TP
122 .I "\-v"
123 Displays the version of
124 .B moc
125 and Qt.
126 .PP
127 You can explicitly tell the
128 .B moc
129 not to parse parts of a header
130 file. It recognizes any C++ comment (//) that contains the substrings
131 MOC_SKIP_BEGIN or MOC_SKIP_END. They work as you would expect and you
132 can have several levels of them. The net result as seen by the
133 .B moc
134 is as if you had removed all lines between a MOC_SKIP_BEGIN and a
135 MOC_SKIP_END
136 .SH USAGE
137 .B moc
138 is almost always invoked by
139 .BR make (1),
140 not by hand.
141 .PP
142 .B moc
143 is typically used with an input file containing class declarations
144 like this:
145 .PP
146 .in +4
147 .nf
148 class YourClass : public QObject {
149     Q_OBJECT
150     Q_PROPERTY( ... )
151     Q_CLASSINFO( ... )
152
153 public:
154     YourClass( QObject * parent=0, const char * name=0 );
155     ~YourClass();
156
157 signals:
158
159 public slots:
160
161 };
162 .fi
163 .in -4
164 .PP
165 Here is a useful makefile rule if you only use GNU make:
166 .PP
167 .in +4
168 .nf
169 m%.cpp: %.h
170         moc $< -o $@
171 .fi
172 .in -4
173 .PP
174 If you want to write portably, you can use individual rules of the
175 following form:
176 .PP
177 .in +4
178 .nf
179 mNAME.cpp: NAME.h
180         moc $< -o $@
181 .fi
182 .in -4
183 .PP
184 You must also remember to add
185 .I mNAME.cpp
186 to your SOURCES (substitute your favorite name) variable and
187 .I mNAME.o
188 to your OBJECTS variable.
189 .PP
190 (While we prefer to name our C++ source files .cpp, the
191 .B moc
192 doesn't know that, so you can use .C, .cc, .CC, .cxx or even .c++ if
193 you prefer.)
194 .PP
195 If you have class declarations in C++ files, we recommend that you use
196 a makefile rule like this:
197 .PP
198 .in +4
199 .nf
200 NAME.o: mNAME.cpp
201
202 mNAME.cpp: NAME.cpp
203         moc -i $< -o $@
204 .fi
205 .in -4
206 .PP
207 This guarantees that
208 .BR make (1)
209 will run the
210 .B moc
211 before it compiles
212 .IR NAME.cpp .
213 You can then put
214 .PP
215 .ti +4
216 #include "nNAME.cpp"
217 .PP
218 at the end of
219 .IR NAME.cpp ,
220 where all the classes declared in that file are fully known.
221 .SH DIAGNOSTICS
222 Sometimes you may get linkage errors, saying that
223 YourClass::className() is undefined or that YourClass lacks a vtbl.
224 Those errors happen most often when you forget to compile the
225 moc-generated C++ code or include that object file in the link
226 command.
227 .PP
228 The
229 .B moc
230 will warn you about a number of dangerous or illegal constructs.
231 .SH BUGS
232
233 The
234 .B moc
235 does not expand #include or #define, it simply skips any preprocessor
236 directives it encounters. This is regrettable, but is normally not a
237 problem in practice.
238
239 The
240 .B moc
241 does not handle all of C++.  The main problem is that class templates
242 cannot have signals or slots.  This is an important bug.  Here is an
243 example:
244 .PP
245 .in +4
246 .nf
247 class SomeTemplate<int> : public QFrame {
248     Q_OBJECT
249     ....
250 signals:
251     void bugInMocDetected( int );
252 };
253 .fi
254 .in -4
255 .PP
256 Less importantly, the following constructs are illegal.  All of them
257 have have alternatives which we think are usually better, so removing
258 these limitations is not a high priority for us.
259 .SS "Multiple inheritance requires QObject to be first."
260 If you are using multiple inheritance, 
261 .B moc 
262 assumes that the
263 .B first
264 inherited class is a subclass of QObject.  Also, be sure that
265 .B only
266 the first inherited class is a QObject.
267 .PP
268 .in +4
269 .nf
270 class SomeClass : public QObject, public OtherClass {
271     ...
272 };
273 .fi
274 .in -4
275 .PP
276 This bug is almost impossible to fix; since the
277 .B moc
278 does not expand
279 #include or #define, it cannot find out which one of the base classes is a
280 QObject.
281 .SS "Function pointers cannot be arguments to signals or slots."
282 In most cases where you would consider that, we think inheritance is a
283 better alternative.  Here is an example of illegal syntax:
284 .PP
285 .in +4
286 .nf
287 class SomeClass : public QObject {
288     Q_OBJECT
289     ...
290 public slots:
291     // illegal
292     void apply( void (*apply)(List *, void *), void * );
293 };
294 .fi
295 .in -4
296 .PP
297 You can work around this restriction like this:
298 .PP
299 .in +4
300 .nf
301 typedef void (*ApplyFunctionType)( List *, void * );
302
303 class SomeClass : public QObject {
304     Q_OBJECT
305     ...
306 public slots:
307     void apply( ApplyFunctionType, char * );
308 };
309 .fi
310 .in -4
311 .PP
312 It may sometimes be even better to replace the function pointer with
313 inheritance and virtual functions, signals or slots.
314 .SS "Friend declarations cannot be placed in signals or slots sections"
315 Sometimes it will work, but in general, friend declarations cannot be
316 placed in
317 .B signals
318 or
319 .B slots
320 sections.  Put them in the good old
321 .BR private ", " protected
322 or
323 .B public
324 sections instead.  Here is an example of the illegal syntax:
325 .PP
326 .in +4
327 .nf
328 class SomeClass : public QObject {
329     Q_OBJECT
330     ...
331 signals:
332     friend class ClassTemplate<char>; // illegal
333 };
334 .fi
335 .in -4
336 .SS "Signals and slots cannot be upgraded"
337 The C++ feature of upgrading an inherited member function to
338 .B public
339 status is not extended to cover signals and slots.  Here is an illegal
340 example:
341 .PP
342 .in +4
343 .nf
344 class Whatever : public QButtonGroup {
345     ...
346 public slots:
347     QButtonGroup::buttonPressed; // illegal
348     ...
349 };
350 .fi
351 .in -4
352 .PP
353 The QButtonGroup::buttonPressed() slot is protected.
354 .PP
355 C++ quiz: What happens if you try to upgrade a protected member
356 function which is overloaded?
357 .IP
358 - All the functions are upgraded.
359 .IP
360 - That is not legal C++.
361 .\" Good idea, but look in the SEE ALSO section...
362 .SS "Type macros cannot be used for signal and slot arguments"
363
364 Since the
365 .B moc
366 does not expand #define, type macros that take an argument
367 will not work in signals and slots. Here is an illegal example:
368 .PP
369 .in +4
370 .nf
371 #ifdef ultrix
372 #define SIGNEDNESS(a) unsigned a
373 #else
374 #define SIGNEDNESS(a) a
375 #endif
376 class Whatever : public QObject {
377     ...
378 signals:
379     void someSignal( SIGNEDNESS(int) ); // illegal
380 };
381 .PP
382 A #define without arguments works.
383 .fi
384 .in -4
385 .SS  "Nested classes cannot be in the signals or slots sections nor have signals or slots"
386 Here's an example:
387 .PP
388 .in +4
389 .nf
390 class A {
391     Q_OBJECT
392 public:
393     class B {
394     public slots: // illegal
395         void b();
396         ...
397     };
398 signals:
399     class B {  // illegal
400         void b();
401         ...
402     }:
403 };
404 .fi
405 .in -4
406 .PP
407 .SS "Constructors cannot be used in signals or slots sections"
408 It is a mystery to us why anyone would put a constructor on either the
409 .B signals
410 or
411 .B slots
412 sections.  You can't, anyway (except that it happens to work in some
413 cases).  Put them in
414 .BR private ", " protected
415 or
416 .B public
417 sections, where they belong.  Here is an example of the illegal syntax:
418 .PP
419 .in +4
420 .nf
421 class SomeClass : public QObject {
422     Q_OBJECT
423 public slots:
424     SomeClass( QObject *parent, const char *name )
425         : QObject( parent, name ) {} // illegal
426     ...
427 };
428 .fi
429 .in -4
430 .SS "Properties need to be declared before the public section that contains the respective get and set functions"
431 .PP
432 Declaring the first property within or after the public section that
433 contains the type definition and the respective get and set functions
434 does not work as expected. The
435 .B moc
436 will complain that it can neither
437 find the functions nor resolve the type. Here is an example of the
438 illegal syntax:
439 .PP
440 .in +4
441 .nf
442 class SomeClass : public QObject {
443     Q_OBJECT
444 public:
445     ...
446     // illegal
447     Q_PROPERTY( Priority priority READ priority WRITE setPriority )
448     Q_ENUMS( Priority )
449     enum Priority { High, Low, VeryHigh, VeryLow };
450     void setPriority( Priority );
451     Priority priority() const;
452     ...
453 };
454 .fi
455 .in -4
456 .PP
457 Work around this limitation by declaring all properties at the
458 beginning of the class declaration, right after Q_OBJECT:
459 .PP
460 .in +4
461 .nf
462 class SomeClass : public QObject {
463     Q_OBJECT
464     Q_PROPERTY( Priority priority READ priority WRITE setPriority )
465     Q_ENUMS( Priority )
466 public:
467     ...
468     enum Priority { High, Low, VeryHigh, VeryLow };
469     void setPriority( Priority );
470     Priority priority() const;
471     ...
472 };
473 .fi
474 .in -4
475 .PP
476 .SH "SEE ALSO"
477 .BR http://www.trolltech.com ", "
478 .BR "C++ ARM, section r.11.3" " (for the answer to the quiz), and"
479 .BR http://doc.trolltech.com " (for complete Qt documentation)."