/usr/share/qt5/doc/qtdoc/moc.html is in qt5-doc-html 5.9.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- moc.qdoc -->
<title>Using the Meta-Object Compiler (moc) | Qt 5.9</title>
<link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
<script type="text/javascript">
document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
// loading style sheet breaks anchors that were jumped to before
// so force jumping to anchor again
setTimeout(function() {
var anchor = location.hash;
// need to jump to different anchor first (e.g. none)
location.hash = "#";
setTimeout(function() {
location.hash = anchor;
}, 0);
}, 0);
</script>
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<table><tr>
<td ><a href="index.html">Qt 5.9</a></td><td >Using the Meta-Object Compiler (moc)</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.5 Reference Documentation</td>
</tr></table>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#usage">Usage</a></li>
<li class="level1"><a href="#writing-make-rules-for-invoking-moc">Writing Make Rules for Invoking <code>moc</code></a></li>
<li class="level1"><a href="#command-line-options">Command-Line Options</a></li>
<li class="level1"><a href="#diagnostics">Diagnostics</a></li>
<li class="level1"><a href="#limitations">Limitations</a></li>
<li class="level2"><a href="#multiple-inheritance-requires-qobject-to-be-first">Multiple Inheritance Requires QObject to Be First</a></li>
<li class="level2"><a href="#function-pointers-cannot-be-signal-or-slot-parameters">Function Pointers Cannot Be Signal or Slot Parameters</a></li>
<li class="level2"><a href="#enums-and-typedefs-must-be-fully-qualified-for-signal-and-slot-parameters">Enums and Typedefs Must Be Fully Qualified for Signal and Slot Parameters</a></li>
<li class="level2"><a href="#nested-classes-cannot-have-signals-or-slots">Nested Classes Cannot Have Signals or Slots</a></li>
<li class="level2"><a href="#signal-slot-return-types-cannot-be-references">Signal/Slot return types cannot be references</a></li>
<li class="level2"><a href="#only-signals-and-slots-may-appear-in-the-keyword-signals-keyword-and-keyword-slots-keyword-sections-of-a-class">Only Signals and Slots May Appear in the <code>signals</code> and <code>slots</code> Sections of a Class</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Using the Meta-Object Compiler (moc)</h1>
<span class="subtitle"></span>
<!-- $$$moc.html-description -->
<div class="descr"> <a name="details"></a>
<p>The Meta-Object Compiler, <code>moc</code>, is the program that handles <a href="../qtcore/metaobjects.html">Qt's C++ extensions</a>.</p>
<p>The <code>moc</code> tool reads a C++ header file. If it finds one or more class declarations that contain the <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> macro, it produces a C++ source file containing the meta-object code for those classes. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system.</p>
<p>The C++ source file generated by <code>moc</code> must be compiled and linked with the implementation of the class.</p>
<p>If you use <a href="../qmake/qmake-manual.html">qmake</a> to create your makefiles, build rules will be included that call the moc when required, so you will not need to use the moc directly. For more background information on <code>moc</code>, see <a href="why-moc.html">Why Does Qt Use Moc for Signals and Slots?</a></p>
<a name="usage"></a>
<h2 id="usage">Usage</h2>
<p><code>moc</code> is typically used with an input file containing class declarations like this:</p>
<pre class="cpp">
<span class="keyword">class</span> MyClass : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
{
Q_OBJECT
<span class="keyword">public</span>:
MyClass(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="operator">~</span>MyClass();
<span class="keyword">signals</span>:
<span class="type">void</span> mySignal();
<span class="keyword">public</span> <span class="keyword">slots</span>:
<span class="type">void</span> mySlot();
};
</pre>
<p>In addition to the signals and slots shown above, <code>moc</code> also implements object properties as in the next example. The <a href="../qtcore/qobject.html#Q_PROPERTY">Q_PROPERTY</a>() macro declares an object property, while <a href="../qtcore/qobject.html#Q_ENUM">Q_ENUM</a>() declares a list of enumeration types within the class to be usable inside the <a href="../qtcore/properties.html">property system</a>.</p>
<p>In the following example, we declare a property of the enumeration type <code>Priority</code> that is also called <code>priority</code> and has a get function <code>priority()</code> and a set function <code>setPriority()</code>.</p>
<pre class="cpp">
<span class="keyword">class</span> MyClass : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
{
Q_OBJECT
Q_PROPERTY(Priority priority READ priority WRITE setPriority)
Q_ENUMS(Priority)
<span class="keyword">public</span>:
<span class="keyword">enum</span> Priority { High<span class="operator">,</span> Low<span class="operator">,</span> VeryHigh<span class="operator">,</span> VeryLow };
MyClass(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="operator">~</span>MyClass();
<span class="type">void</span> setPriority(Priority priority) { m_priority <span class="operator">=</span> priority; }
Priority priority() <span class="keyword">const</span> { <span class="keyword">return</span> m_priority; }
<span class="keyword">private</span>:
Priority m_priority;
};
</pre>
<p>The Q_FLAGS() macro declares enums that are to be used as flags, i.e. OR'd together. Another macro, <a href="../qtcore/qobject.html#Q_CLASSINFO">Q_CLASSINFO</a>(), allows you to attach additional name/value pairs to the class's meta-object:</p>
<pre class="cpp">
<span class="keyword">class</span> MyClass : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
{
Q_OBJECT
Q_CLASSINFO(<span class="string">"Author"</span><span class="operator">,</span> <span class="string">"Oscar Peterson"</span>)
Q_CLASSINFO(<span class="string">"Status"</span><span class="operator">,</span> <span class="string">"Active"</span>)
<span class="keyword">public</span>:
MyClass(<span class="type"><a href="../qtcore/qobject.html">QObject</a></span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="operator">~</span>MyClass();
};
</pre>
<p>The output produced by <code>moc</code> must be compiled and linked, just like the other C++ code in your program; otherwise, the build will fail in the final link phase. If you use <code>qmake</code>, this is done automatically. Whenever <code>qmake</code> is run, it parses the project's header files and generates make rules to invoke <code>moc</code> for those files that contain a <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> macro.</p>
<p>If the class declaration is found in the file <code>myclass.h</code>, the moc output should be put in a file called <code>moc_myclass.cpp</code>. This file should then be compiled as usual, resulting in an object file, e.g., <code>moc_myclass.obj</code> on Windows. This object should then be included in the list of object files that are linked together in the final building phase of the program.</p>
<a name="writing-make-rules-for-invoking-moc"></a>
<h2 id="writing-make-rules-for-invoking-moc">Writing Make Rules for Invoking <code>moc</code></h2>
<p>For anything but the simplest test programs, it is recommended that you automate running the <code>moc</code>. By adding some rules to your program's makefile, <code>make</code> can take care of running moc when necessary and handling the moc output.</p>
<p>We recommend using the <a href="../qmake/qmake-manual.html">qmake</a> makefile generation tool for building your makefiles. This tool generates a makefile that does all the necessary <code>moc</code> handling.</p>
<p>If you want to create your makefiles yourself, here are some tips on how to include moc handling.</p>
<p>For <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> class declarations in header files, here is a useful makefile rule if you only use GNU make:</p>
<pre class="cpp">
moc_<span class="operator">%</span><span class="operator">.</span>cpp: <span class="operator">%</span><span class="operator">.</span>h
moc $(DEFINES) $(INCPATH) $<span class="operator"><</span> <span class="operator">-</span>o $@
</pre>
<p>If you want to write portably, you can use individual rules of the following form:</p>
<pre class="cpp">
moc_foo<span class="operator">.</span>cpp: foo<span class="operator">.</span>h
moc $(DEFINES) $(INCPATH) $<span class="operator"><</span> <span class="operator">-</span>o $@
</pre>
<p>You must also remember to add <code>moc_foo.cpp</code> to your <code>SOURCES</code> (substitute your favorite name) variable and <code>moc_foo.o</code> or <code>moc_foo.obj</code> to your <code>OBJECTS</code> variable.</p>
<p>Both examples assume that <code>$(DEFINES)</code> and <code>$(INCPATH)</code> expand to the define and include path options that are passed to the C++ compiler. These are required by <code>moc</code> to preprocess the source files.</p>
<p>While we prefer to name our C++ source files <code>.cpp</code>, you can use any other extension, such as <code>.C</code>, <code>.cc</code>, <code>.CC</code>, <code>.cxx</code>, and <code>.c++</code>, if you prefer.</p>
<p>For <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> class declarations in implementation (<code>.cpp</code>) files, we suggest a makefile rule like this:</p>
<pre class="cpp">
foo<span class="operator">.</span>o: foo<span class="operator">.</span>moc
foo<span class="operator">.</span>moc: foo<span class="operator">.</span>cpp
moc $(DEFINES) $(INCPATH) <span class="operator">-</span>i $<span class="operator"><</span> <span class="operator">-</span>o $@
</pre>
<p>This guarantees that make will run the moc before it compiles <code>foo.cpp</code>. You can then put</p>
<pre class="cpp">
<span class="preprocessor">#include "foo.moc"</span>
</pre>
<p>at the end of <code>foo.cpp</code>, where all the classes declared in that file are fully known.</p>
<a name="command-line-options"></a>
<h2 id="command-line-options">Command-Line Options</h2>
<p>Here are the command-line options supported by the moc:</p>
<div class="table"><table class="generic">
<thead><tr class="qt-style"><th >Option</th><th >Description</th></tr></thead>
<tr valign="top" class="odd"><td ><code>-o<file></code></td><td >Write output to <code><file></code> rather than to standard output.</td></tr>
<tr valign="top" class="even"><td ><code>-f[<file>]</code></td><td >Force the generation of an <code>#include</code> statement in the output. This is the default for header files whose extension starts with <code>H</code> or <code>h</code>. This option is useful if you have header files that do not follow the standard naming conventions. The <code><file></code> part is optional.</td></tr>
<tr valign="top" class="odd"><td ><code>-i</code></td><td >Do not generate an <code>#include</code> 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 <code>#include</code> the meta-object code in the <code>.cpp</code> file.</td></tr>
<tr valign="top" class="even"><td ><code>-nw</code></td><td >Do not generate any warnings. (Not recommended.)</td></tr>
<tr valign="top" class="odd"><td ><code>-p<path></code></td><td >Makes the moc prepend <code><path>/</code> to the file name in the generated <code>#include</code> statement.</td></tr>
<tr valign="top" class="even"><td ><code>-I<dir></code></td><td >Add dir to the include path for header files.</td></tr>
<tr valign="top" class="odd"><td ><code>-E</code></td><td >Preprocess only; do not generate meta-object code.</td></tr>
<tr valign="top" class="even"><td ><code>-D<macro>[=<def>]</code></td><td >Define macro, with optional definition.</td></tr>
<tr valign="top" class="odd"><td ><code>-U<macro></code></td><td >Undefine macro.</td></tr>
<tr valign="top" class="even"><td ><code>-M<key=value></code></td><td >Append additional meta data to plugins. If a class has <a href="../qtcore/qtplugin.html#Q_PLUGIN_METADATA">Q_PLUGIN_METADATA</a> specified, the key-value pair will be added to its meta data. This will end up in the Json object that gets resolved for the plugin at run time (accessible from <a href="../qtcore/qpluginloader.html">QPluginLoader</a>). This argument is typically used for tagging static plugins with information resolved by the build system.</td></tr>
<tr valign="top" class="odd"><td ><code>@<file></code></td><td >Read additional command-line options from <code><file></code>. 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).</td></tr>
<tr valign="top" class="even"><td ><code>-h</code></td><td >Display the usage and the list of options.</td></tr>
<tr valign="top" class="odd"><td ><code>-v</code></td><td >Display <code>moc</code>'s version number.</td></tr>
<tr valign="top" class="even"><td ><code>-Fdir</code></td><td >macOS. Add the framework directory <code>dir</code> 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/</td></tr>
</table></div>
<p>You can explicitly tell the moc not to parse parts of a header file. <code>moc</code> defines the preprocessor symbol <code>Q_MOC_RUN</code>. Any code surrounded by</p>
<pre class="cpp">
<span class="preprocessor">#ifndef Q_MOC_RUN</span>
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
<span class="preprocessor">#endif</span>
</pre>
<p>is skipped by the <code>moc</code>.</p>
<a name="diagnostics"></a>
<h2 id="diagnostics">Diagnostics</h2>
<p><code>moc</code> will warn you about a number of dangerous or illegal constructs in the <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> class declarations.</p>
<p>If you get linkage errors in the final building phase of your program, saying that <code>YourClass::className()</code> is undefined or that <code>YourClass</code> lacks a vtable, something has been done wrong. Most often, you have forgotten to compile or <code>#include</code> the moc-generated C++ code, or (in the former case) include that object file in the link command. If you use <code>qmake</code>, try rerunning it to update your makefile. This should do the trick.</p>
<a name="limitations"></a>
<h2 id="limitations">Limitations</h2>
<p><code>moc</code> does not handle all of C++. The main problem is that class templates cannot have the <a href="../qtcore/qobject.html#Q_OBJECT">Q_OBJECT</a> macro. Here is an example:</p>
<pre class="cpp">
<span class="keyword">class</span> SomeTemplate<span class="operator"><</span><span class="type">int</span><span class="operator">></span> : <span class="keyword">public</span> <span class="type"><a href="../qtwidgets/qframe.html">QFrame</a></span>
{
Q_OBJECT
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
<span class="keyword">signals</span>:
<span class="type">void</span> mySignal(<span class="type">int</span>);
};
</pre>
<p>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.</p>
<a name="multiple-inheritance-requires-qobject-to-be-first"></a>
<h3 >Multiple Inheritance Requires QObject to Be First</h3>
<p>If you are using multiple inheritance, <code>moc</code> assumes that the first inherited class is a subclass of <a href="../qtcore/qobject.html">QObject</a>. Also, be sure that only the first inherited class is a <a href="../qtcore/qobject.html">QObject</a>.</p>
<pre class="cpp">
<span class="comment">// correct</span>
<span class="keyword">class</span> SomeClass : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span><span class="operator">,</span> <span class="keyword">public</span> OtherClass
{
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
};
</pre>
<p>Virtual inheritance with <a href="../qtcore/qobject.html">QObject</a> is <i>not</i> supported.</p>
<a name="function-pointers-cannot-be-signal-or-slot-parameters"></a>
<h3 >Function Pointers Cannot Be Signal or Slot Parameters</h3>
<p>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:</p>
<pre class="cpp">
<span class="keyword">class</span> SomeClass : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
{
Q_OBJECT
<span class="keyword">public</span> <span class="keyword">slots</span>:
<span class="type">void</span> apply(<span class="type">void</span> (<span class="operator">*</span>apply)(List <span class="operator">*</span><span class="operator">,</span> <span class="type">void</span> <span class="operator">*</span>)<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>); <span class="comment">// WRONG</span>
};
</pre>
<p>You can work around this restriction like this:</p>
<pre class="cpp">
<span class="keyword">typedef</span> <span class="type">void</span> (<span class="operator">*</span>ApplyFunction)(List <span class="operator">*</span><span class="operator">,</span> <span class="type">void</span> <span class="operator">*</span>);
<span class="keyword">class</span> SomeClass : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
{
Q_OBJECT
<span class="keyword">public</span> <span class="keyword">slots</span>:
<span class="type">void</span> apply(ApplyFunction<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>);
};
</pre>
<p>It may sometimes be even better to replace the function pointer with inheritance and virtual functions.</p>
<a name="enums-and-typedefs-must-be-fully-qualified-for-signal-and-slot-parameters"></a>
<h3 >Enums and Typedefs Must Be Fully Qualified for Signal and Slot Parameters</h3>
<p>When checking the signatures of its arguments, <a href="../qtcore/qobject.html#connect-4">QObject::connect</a>() compares the data types literally. Thus, <a href="../qtcore/qt.html#AlignmentFlag-enum">Alignment</a> and <a href="../qtcore/qt.html#AlignmentFlag-enum">Qt::Alignment</a> 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:</p>
<pre class="cpp">
<span class="keyword">class</span> MyClass : <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
{
Q_OBJECT
<span class="keyword">enum</span> Error {
ConnectionRefused<span class="operator">,</span>
RemoteHostClosed<span class="operator">,</span>
UnknownError
};
<span class="keyword">signals</span>:
<span class="type">void</span> stateChanged(MyClass<span class="operator">::</span>Error error);
};
</pre>
<a name="nested-classes-cannot-have-signals-or-slots"></a>
<h3 >Nested Classes Cannot Have Signals or Slots</h3>
<p>Here's an example of the offending construct:</p>
<pre class="cpp">
<span class="keyword">class</span> A
{
<span class="keyword">public</span>:
<span class="keyword">class</span> B
{
Q_OBJECT
<span class="keyword">public</span> <span class="keyword">slots</span>: <span class="comment">// WRONG</span>
<span class="type">void</span> b();
};
};
</pre>
<a name="signal-slot-return-types-cannot-be-references"></a>
<h3 >Signal/Slot return types cannot be references</h3>
<p>Signals and slots can have return types, but signals or slots returning references will be treated as returning void.</p>
<a name="only-signals-and-slots-may-appear-in-the-keyword-signals-keyword-and-keyword-slots-keyword-sections-of-a-class"></a>
<h3 >Only Signals and Slots May Appear in the <code>signals</code> and <code>slots</code> Sections of a Class</h3>
<p><code>moc</code> will complain if you try to put other constructs in the <code>signals</code> or <code>slots</code> sections of a class than signals and slots.</p>
</div>
<p><b>See also </b><a href="../qtcore/metaobjects.html">Meta-Object System</a>, <a href="exceptionsafety.html#signals-and-slots">Signals and Slots</a>, and <a href="../qtcore/properties.html">Qt's Property System</a>.</p>
<!-- @@@moc.html -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|