<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- moc.qdoc --> <title>Qt 4.8: Using the Meta-Object Compiler (moc)</title> <link rel="stylesheet" type="text/css" href="style/style.css" /> <script src="scripts/jquery.js" type="text/javascript"></script> <script src="scripts/functions.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="style/superfish.css" /> <link rel="stylesheet" type="text/css" href="style/narrow.css" /> <!--[if IE]> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="no"> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie6.css"> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie7.css"> <![endif]--> <!--[if IE 8]> <link rel="stylesheet" type="text/css" href="style/style_ie8.css"> <![endif]--> <script src="scripts/superfish.js" type="text/javascript"></script> <script src="scripts/narrow.js" type="text/javascript"></script> </head> <body class="" onload="CheckEmptyAndLoadList();"> <div class="header" id="qtdocheader"> <div class="content"> <div id="nav-logo"> <a href="index.html">Home</a></div> <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a> <div id="narrowsearch"></div> <div id="nav-topright"> <ul> <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li> <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li> <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/"> DOC</a></li> <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li> </ul> </div> <div id="shortCut"> <ul> <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li> <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li> </ul> </div> <ul class="sf-menu" id="narrowmenu"> <li><a href="#">API Lookup</a> <ul> <li><a href="classes.html">Class index</a></li> <li><a href="functions.html">Function index</a></li> <li><a href="modules.html">Modules</a></li> <li><a href="namespaces.html">Namespaces</a></li> <li><a href="qtglobal.html">Global Declarations</a></li> <li><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </li> <li><a href="#">Qt Topics</a> <ul> <li><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li><a href="supported-platforms.html">Supported Platforms</a></li> <li><a href="technology-apis.html">Qt and Key Technologies</a></li> <li><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </li> <li><a href="#">Examples</a> <ul> <li><a href="all-examples.html">Examples</a></li> <li><a href="tutorials.html">Tutorials</a></li> <li><a href="demos.html">Demos</a></li> <li><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </li> </ul> </div> </div> <div class="wrapper"> <div class="hd"> <span></span> </div> <div class="bd group"> <div class="sidebar"> <div class="searchlabel"> Search index:</div> <div class="search" id="sidebarsearch"> <form id="qtdocsearch" action="" onsubmit="return false;"> <fieldset> <input type="text" name="searchstring" id="pageType" value="" /> <div id="resultdialog"> <a href="#" id="resultclose">Close</a> <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p> <ul id="resultlist" class="all"> </ul> </div> </fieldset> </form> </div> <div class="box first bottombar" id="lookup"> <h2 title="API Lookup"><span></span> API Lookup</h2> <div id="list001" class="list"> <ul id="ul001" > <li class="defaultLink"><a href="classes.html">Class index</a></li> <li class="defaultLink"><a href="functions.html">Function index</a></li> <li class="defaultLink"><a href="modules.html">Modules</a></li> <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li> <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li> <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </div> </div> <div class="box bottombar" id="topics"> <h2 title="Qt Topics"><span></span> Qt Topics</h2> <div id="list002" class="list"> <ul id="ul002" > <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li> <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </div> </div> <div class="box" id="examples"> <h2 title="Examples"><span></span> Examples</h2> <div id="list003" class="list"> <ul id="ul003"> <li class="defaultLink"><a href="all-examples.html">Examples</a></li> <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li> <li class="defaultLink"><a href="demos.html">Demos</a></li> <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </div> </div> </div> <div class="wrap"> <div class="toolbar"> <div class="breadcrumb toolblock"> <ul> <li class="first"><a href="index.html">Home</a></li> <!-- Breadcrumbs go here --> <li>Using the Meta-Object Compiler (moc)</li> </ul> </div> <div class="toolbuttons toolblock"> <ul> <li id="smallA" class="t_button">A</li> <li id="medA" class="t_button active">A</li> <li id="bigA" class="t_button">A</li> <li id="print" class="t_button"><a href="javascript:this.print();"> <span>Print</span></a></li> </ul> </div> </div> <div class="content mainContent"> <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">Writing Make Rules for Invoking <tt>moc</tt></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="#type-macros-cannot-be-used-for-signal-and-slot-parameters">Type Macros Cannot Be Used 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-and-sections-of-a-class">Only Signals and Slots May Appear in the <tt>signals</tt> and <tt>slots</tt> Sections of a Class</a></li> </ul> </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> <a name="moc"></a><p>The Meta-Object Compiler, <tt>moc</tt>, is the program that handles <a href="metaobjects.html#meta-object-system">Qt's C++ extensions</a>.</p> <p>The <tt>moc</tt> tool reads a C++ header file. If it finds one or more class declarations that contain the <a href="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 <tt>moc</tt> must be compiled and linked with the implementation of the class.</p> <p>If you use <a href="qmake-manual.html#qmake">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 <tt>moc</tt>, see <a href="templates.html">Why Doesn't Qt Use Templates for Signals and Slots?</a></p> <a name="usage"></a> <h2>Usage</h2> <p><tt>moc</tt> 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="qobject.html">QObject</a></span> { Q_OBJECT <span class="keyword">public</span>: MyClass(<span class="type"><a href="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, <tt>moc</tt> also implements object properties as in the next example. The <a href="qobject.html#Q_PROPERTY">Q_PROPERTY</a>() macro declares an object property, while <a href="qobject.html#Q_ENUMS">Q_ENUMS</a>() declares a list of enumeration types within the class to be usable inside the <a href="properties.html#qt-s-property-system">property system</a>.</p> <p>In the following example, we declare a property of the enumeration type <tt>Priority</tt> that is also called <tt>priority</tt> and has a get function <tt>priority()</tt> and a set function <tt>setPriority()</tt>.</p> <pre class="cpp"> <span class="keyword">class</span> MyClass : <span class="keyword">public</span> <span class="type"><a href="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="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 <a href="qobject.html#Q_FLAGS">Q_FLAGS</a>() macro declares enums that are to be used as flags, i.e. OR'd together. Another macro, <a href="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="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="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 <tt>moc</tt> 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 <tt>qmake</tt>, this is done automatically. Whenever <tt>qmake</tt> is run, it parses the project's header files and generates make rules to invoke <tt>moc</tt> for those files that contain a <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> macro.</p> <p>If the class declaration is found in the file <tt>myclass.h</tt>, the moc output should be put in a file called <tt>moc_myclass.cpp</tt>. This file should then be compiled as usual, resulting in an object file, e.g., <tt>moc_myclass.obj</tt> 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"></a> <h2>Writing Make Rules for Invoking <tt>moc</tt></h2> <p>For anything but the simplest test programs, it is recommended that you automate running the <tt>moc</tt>. By adding some rules to your program's makefile, <tt>make</tt> can take care of running moc when necessary and handling the moc output.</p> <p>We recommend using the <a href="qmake-manual.html#qmake">qmake</a> makefile generation tool for building your makefiles. This tool generates a makefile that does all the necessary <tt>moc</tt> 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="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_%.cpp: %.h moc $(DEFINES) $(INCPATH) $< -o $@</pre> <p>If you want to write portably, you can use individual rules of the following form:</p> <pre class="cpp"> moc_foo.cpp: foo.h moc $(DEFINES) $(INCPATH) $< -o $@</pre> <p>You must also remember to add <tt>moc_foo.cpp</tt> to your <tt>SOURCES</tt> (substitute your favorite name) variable and <tt>moc_foo.o</tt> or <tt>moc_foo.obj</tt> to your <tt>OBJECTS</tt> variable.</p> <p>Both examples assume that <tt>$(DEFINES)</tt> and <tt>$(INCPATH)</tt> expand to the define and include path options that are passed to the C++ compiler. These are required by <tt>moc</tt> to preprocess the source files.</p> <p>While we prefer to name our C++ source files <tt>.cpp</tt>, you can use any other extension, such as <tt>.C</tt>, <tt>.cc</tt>, <tt>.CC</tt>, <tt>.cxx</tt>, and <tt>.c++</tt>, if you prefer.</p> <p>For <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> class declarations in implementation (<tt>.cpp</tt>) files, we suggest a makefile rule like this:</p> <pre class="cpp"> foo.o: foo.moc foo.moc: foo.cpp moc $(DEFINES) $(INCPATH) -i $< -o $@</pre> <p>This guarantees that make will run the moc before it compiles <tt>foo.cpp</tt>. You can then put</p> <pre class="cpp"> <span class="preprocessor">#include "foo.moc"</span></pre> <p>at the end of <tt>foo.cpp</tt>, where all the classes declared in that file are fully known.</p> <a name="command-line-options"></a> <h2>Command-Line Options</h2> <p>Here are the command-line options supported by the moc:</p> <table class="generic"> <thead><tr class="qt-style"><th >Option</th><th >Description</th></tr></thead> <tr valign="top" class="odd"><td ><tt>-o<file></tt></td><td >Write output to <tt><file></tt> rather than to standard output.</td></tr> <tr valign="top" class="even"><td ><tt>-f[<file>]</tt></td><td >Force the generation of an <tt>#include</tt> statement in the output. This is the default for header files whose extension starts with <tt>H</tt> or <tt>h</tt>. This option is useful if you have header files that do not follow the standard naming conventions. The <tt><file></tt> part is optional.</td></tr> <tr valign="top" class="odd"><td ><tt>-i</tt></td><td >Do not generate an <tt>#include</tt> 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 <tt>#include</tt> the meta-object code in the <tt>.cpp</tt> file.</td></tr> <tr valign="top" class="even"><td ><tt>-nw</tt></td><td >Do not generate any warnings. (Not recommended.)</td></tr> <tr valign="top" class="odd"><td ><tt>-p<path></tt></td><td >Makes the moc prepend <tt><path>/</tt> to the file name in the generated <tt>#include</tt> statement.</td></tr> <tr valign="top" class="even"><td ><tt>-I<dir></tt></td><td >Add dir to the include path for header files.</td></tr> <tr valign="top" class="odd"><td ><tt>-E</tt></td><td >Preprocess only; do not generate meta-object code.</td></tr> <tr valign="top" class="even"><td ><tt>-D<macro>[=<def>]</tt></td><td >Define macro, with optional definition.</td></tr> <tr valign="top" class="odd"><td ><tt>-U<macro></tt></td><td >Undefine macro.</td></tr> <tr valign="top" class="even"><td ><tt>@<file></tt></td><td >Read additional command-line options from <tt><file></tt>. 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="odd"><td ><tt>-h</tt></td><td >Display the usage and the list of options.</td></tr> <tr valign="top" class="even"><td ><tt>-v</tt></td><td >Display <tt>moc</tt>'s version number.</td></tr> <tr valign="top" class="odd"><td ><tt>-Fdir</tt></td><td >Mac OS X. Add the framework directory <tt>dir</tt> 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> <p>You can explicitly tell the moc not to parse parts of a header file. <tt>moc</tt> defines the preprocessor symbol <tt>Q_MOC_RUN</tt>. 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 <tt>moc</tt>.</p> <a name="diagnostics"></a> <h2>Diagnostics</h2> <p><tt>moc</tt> will warn you about a number of dangerous or illegal constructs in the <a href="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 <tt>YourClass::className()</tt> is undefined or that <tt>YourClass</tt> lacks a vtable, something has been done wrong. Most often, you have forgotten to compile or <tt>#include</tt> the moc-generated C++ code, or (in the former case) include that object file in the link command. If you use <tt>qmake</tt>, try rerunning it to update your makefile. This should do the trick.</p> <a name="limitations"></a> <h2>Limitations</h2> <p><tt>moc</tt> does not handle all of C++. The main problem is that class templates cannot have signals or slots. 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="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>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 <a href="qobject.html">QObject</a>.</p> <p>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.</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, <tt>moc</tt> assumes that the first inherited class is a subclass of <a href="qobject.html">QObject</a>. Also, be sure that only the first inherited class is a <a href="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="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="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="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="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="qobject.html#connect">QObject::connect</a>() compares the data types literally. Thus, <a href="qt.html#AlignmentFlag-enum">Alignment</a> and <a href="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="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="type-macros-cannot-be-used-for-signal-and-slot-parameters"></a> <h3>Type Macros Cannot Be Used for Signal and Slot Parameters</h3> <p>Since <tt>moc</tt> doesn't expand <tt>#define</tt>s, type macros that take an argument will not work in signals and slots. Here is an illegal example:</p> <pre class="cpp"> <span class="preprocessor">#ifdef ultrix</span> <span class="preprocessor">#define SIGNEDNESS(a) unsigned a</span> <span class="preprocessor">#else</span> <span class="preprocessor">#define SIGNEDNESS(a) a</span> <span class="preprocessor">#endif</span> <span class="keyword">class</span> Whatever : <span class="keyword">public</span> <span class="type"><a href="qobject.html">QObject</a></span> { Q_OBJECT <span class="keyword">signals</span>: <span class="type">void</span> someSignal(SIGNEDNESS(<span class="type">int</span>)); };</pre> <p>A macro without parameters will work.</p> <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-and-sections-of-a-class"></a> <h3>Only Signals and Slots May Appear in the <tt>signals</tt> and <tt>slots</tt> Sections of a Class</h3> <p><tt>moc</tt> will complain if you try to put other constructs in the <tt>signals</tt> or <tt>slots</tt> sections of a class than signals and slots.</p> </div> <p><b>See also </b><a href="metaobjects.html#meta-object-system">Meta-Object System</a>, <a href="signalsandslots.html#signals-and-slots">Signals and Slots</a>, and <a href="properties.html#qt-s-property-system">Qt's Property System</a>.</p> <!-- @@@moc.html --> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2013 Digia Plc and/or its subsidiaries. Documentation contributions included herein are the copyrights of their respective owners.</p> <br /> <p> 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.</p> <p> Documentation sources may be obtained from <a href="http://www.qt-project.org"> www.qt-project.org</a>.</p> <br /> <p> Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. <a title="Privacy Policy" href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p> </div> <script src="scripts/functions.js" type="text/javascript"></script> </body> </html>