New Signal Slot Syntax: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=New Signal Slot Syntax in Qt 5=
[[Category:Developing_Qt::Qt Planning::Qt Public Roadmap]]<br />[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]


This page was used to describe the new signal and slot syntax during its development. The feature is now released with Qt5.
= New Signal Slot Syntax in Qt 5 =


* [http://woboq.com/blog/new-signals-slots-syntax-in-qt5.html Blog entry introducing it] ''[woboq.com]''
This page was used to describe the new signal and slot syntax during its development. The feature is now released with Qt5.<br />* &quot;Blog entry introducing it&amp;quot;:http://woboq.com/blog/new-signals-slots-syntax-in-qt5.html<br />* &quot;How it works&amp;quot;:http://woboq.com/blog/how-qt-signals-slots-work-part2-qt5.html (implementation details)
* [http://woboq.com/blog/how-qt-signals-slots-work-part2-qt5.html How it works] ''[woboq.com]'' (implementation details)


'''Note''': This is in addition to the old string-based syntax which remains valid.
'''Note''': This is in addition to the old string-based syntax which remains valid.


==Status==
== Status ==


* Already merged in qtbase/master
* Already merged in qtbase/master


==Connecting in Qt5==
== Connecting in Qt5 ==


There will be several ways to connect a signal in Qt5.
There will be several ways to connect a signal in Qt5.


===Old syntax===
=== Old syntax ===


Qt5 will continue to support the [http://doc.qt.nokia.com/latest/qobject.html#connect old string-based syntax] ''[doc.qt.nokia.com]'' for connecting signals and slots defined in a QObject or any class that inherits from QObject (including QWidget)
Qt5 will continue to support the &quot;old string-based syntax&amp;quot;:http://doc.qt.nokia.com/latest/qobject.html#connect for connecting signals and slots defined in a QObject or any class that inherits from QObject (including QWidget)


===New: connecting to QObject member===
<code><br />connect(sender, SIGNAL (valueChanged(QString,QString)),<br /> receiver, SLOT (updateValue(QString)) );<br /></code>


Here’s a new way to connect two QObjects and pass non-string objects:
=== New: connecting to QObject member ===


====pros====
Here's a new way to connect two QObjects and pass non-string objects:
 
<code><br />connect(sender, &amp;Sender::valueChanged,<br /> receiver, &amp;Receiver::updateValue );<br /></code>
 
==== pros ====


* Compile time check of the existence of the signals and slot, of the types, or if the Q_OBJECT is missing.
* Compile time check of the existence of the signals and slot, of the types, or if the Q_OBJECT is missing.
Line 31: Line 34:
* It is possible to connect to any member function of QObject, not only slots.
* It is possible to connect to any member function of QObject, not only slots.


====cons====
==== cons ====


* More complicated syntax? (you need to specify the type of your object)
* More complicated syntax? (you need to specify the type of your object)
Line 37: Line 40:
* Default arguments in slot is not supported anymore.
* Default arguments in slot is not supported anymore.


===New: connecting to simple function===
=== New: connecting to simple function ===


The new syntax can even connect to functions, not just QObjects:
The new syntax can even connect to functions, not just QObjects:


====pro====
<code><br />connect(sender, &amp;Sender::valueChanged, someFunction);<br /></code>
 
==== pro ====


* can be used with tr1::bind
* can be used with tr1::bind
* can be used with c++11 lambda expressions
* can be used with c+''11 lambda expressions
 
<br /><code><br />connect(sender, &amp;Sender::valueChanged,<br /> tr1::bind(receiver, &amp;Receiver::updateValue, &quot;senderValue&amp;quot;, tr1::placeholder::_1) );
====cons====
<br />connect(sender, &amp;Sender::valueChanged, [=](const QString &amp;newValue) {<br /> receiver-&gt;updateValue(&quot;senderValue&amp;quot;, newValue);<br /> } );<br /></code>
 
<br />h4. cons
* There is no automatic disconnection when the ‘receiver’ is destroyed
<br />* There is no automatic disconnection when the 'receiver' is destroyed
 
<br />h2. Disconnecting in Qt5
==Disconnecting in Qt5==
<br />As you might expect, there are some changes in how connections can be terminated in Qt5, too.
 
<br />h3. Old way
As you might expect, there are some changes in how connections can be terminated in Qt5, too.
<br />You can disconnect in the old way (using SIGNAL, SLOT) but only if  
 
<br />* you connected using the old way, or<br />* if you want to disconnect all the slots from a given signal using wild card character
===Old way===
<br />h3. Symetric to the function pointer one
 
<br /><code><br />disconnect(sender, &amp;Sender::valueChanged,<br /> receiver, &amp;Receiver::updateValue );
You can disconnect in the old way (using <span class="caps">SIGNAL</span>, <span class="caps">SLOT</span>) but only if
<br /></code>
 
<br />Only works if you connected with the symmetric call, with function pointers (Or you can also use 0 for wild card)<br />In particular, does not work with static function, functors or lambda functions.
* you connected using the old way, or
<br />h3. New way using QMetaObject::Connection
* if you want to disconnect all the slots from a given signal using wild card character
<br /><code><br />QMetaObject::Connection m_connection;<br />//…<br />m_connection = QObject::connect();<br />//<br />QObject::disconnect(m_connection);<br /></code>
 
<br />Works in all cases, including lambda functions or functors.
===Symetric to the function pointer one===
 
Only works if you connected with the symmetric call, with function pointers (Or you can also use 0 for wild card)<br /> In particular, does not work with static function, functors or lambda functions.
 
===New way using QMetaObject::Connection===
 
Works in all cases, including lambda functions or functors.
 
==Asynchronous made easier.==
 
With C++11 it is possible to keep the code inline
 
Here’s a QDialog without re-entering the eventloop, and keeping the code where it belongs:
 
Another example using [http://blog.nikhilmarathe.me/2011/02/qhttpserver-web-apps-in-qt.html QHttpServer] ''[blog.nikhilmarathe.me]'' : http://pastebin.com/pfbTMqUm
 
==Error reporting==
 
Tested with <span class="caps">GCC</span>.
 
Fortunately, <span class="caps">IDE</span>s like Qt Creator simplifies the function naming
 
===forgot Q_OBJECT===
 
===Type mismatch===
 
==Open Questions==
 
===Default arguments in slot===
 
if you have code like this:
 
The old method allows you to connect that slot to a signal that does not have arguments.<br /> But I cannot know with template code if a function has default arguments or not.<br /> So this feature is disabled.
 
There was an implementation that falls back to the old method if there are more arguments in the slot than in the signal.<br /> This however is quite inconsistent, since the old method does not perform type-checking or type conversion. It was removed from the patch that has been merged.
 
===Overload===
 
As you might see in the example, connecting to QAbstractSocket::error is not really beautiful since error has an overload, and taking the address of an overloaded function requires explicit casting.
 
Some macro could help (with c++11 or ''typeof'' extensions)
 
The best thing is probably to recommend not to overload signals or slots
 
… but we have been adding overloads in past minor releases of Qt because taking the address of a function was not a use case we support. But now this would be impossible without breaking the source compatibility.
 
===Disconnect===
 
Should QMetaObject::Connection have a disconnect() function?
 
The other problem is that there is no automatic disconnection for some object in the closure if we use the syntax that take a closure.<br /> One could add a list of object in the disconnection, or a new function like QMetaObject::Connection::require
 
===Callbacks===


Function such as QHostInfo::lookupHost or QTimer::singleShot or QFileDialog::open take a QObject receiver and char* slot.<br /> This do not work for the new method.<br /> If one wants to do callback c++ way, one should use std::function (or tr1)<br /> But we cannot use <span class="caps">STL</span> types in our <span class="caps">ABI</span>, so a QFunction should be done to copy std::function.<br /> This is anyway irrelevant for QObject connections.
<br />h2. Asynchronous made easier.
<br />With C11 it is possible to keep the code inline


==History==
<br /><code><br />void doYourStuff(const QByteArray &amp;page)<br />{<br /> QTcpSocket '''socket = new QTcpSocket;<br /> socket-&gt;connectToHost(&quot;qt.nokia.com&amp;quot;, 80);<br /> QObject::connect(socket, &amp;QTcpSocket::connected, [socket, page] () {<br /> socket-&gt;write(QByteArray(&quot;GET &quot; + page + &quot;&quot;));<br /> });<br /> QObject::connect(socket, &amp;QTcpSocket::readyRead, [socket] () {<br /> qDebug()&lt;&lt; &quot;GOT DATA &quot;&lt;&lt; socket-&gt;readAll();<br /> });<br /> QObject::connect(socket, &amp;QTcpSocket::disconnected, [socket] () {<br /> qDebug()&lt;&lt; &quot;DISCONNECTED &quot;;<br /> socket-&gt;deleteLater();<br /> });
<br /> QObject::connect(socket, static_cast&amp;lt;void (QTcpSocket::''')(QAbstractSocket::SocketError)&gt;(&amp;QAbstractSocket::error), [socket] (QAbstractSocket::SocketError) {<br /> qDebug()&lt;&lt; &quot;ERROR &quot; &lt;&lt; socket-&gt;errorString();<br /> socket-&gt;deleteLater();<br /> });<br />}<br /></code>
<br />Here's a QDialog without re-entering the eventloop, and keeping the code where it belongs:
<br /><code><br />void Doc::saveDocument() {<br /> QFileDialog '''dlg = new QFileDialog();<br /> dlg-&gt;open();<br /> QObject::connect(dlg, &amp;QDialog::finished, [dlg, this](int result) {<br /> if (result) {<br /> QFile file&amp;amp;#40;dlg-&gt;selectedFiles(&amp;#41;.first());<br /> // …<br /> }<br /> dlg-&gt;deleteLater();<br /> });
<br />}<br /></code>
<br />Another example using &quot;QHttpServer&amp;quot;:http://blog.nikhilmarathe.me/2011/02/qhttpserver-web-apps-in-qt.html : http://pastebin.com/pfbTMqUm


While merged in qtbase, the patches have been squashed. For reference here is a link to the history of the code before it went to codereview: https://qt.gitorious.org/~ogoffart/qt/ogoffarts-qtbase/commits/qobject_connect_ptr


===Categories:===
<br />h2. Error reporting
<br />Tested with GCC.
<br />Fortunately, IDEs like Qt Creator simplifies the function naming
<br />h3. forgot Q_OBJECT
<br /><code><br />#include &lt;QtCore/QtCore&amp;gt;<br />class Goo : public QObject {<br /> Goo() {<br /> connect(this, &amp;Goo::someSignal, this, &amp;QObject::deleteLater);<br /> }<br />signals:<br /> void someSignal();<br />};<br /></code>
<br /><code><br />qobject.h: In member function 'void QObject::qt_check_for_QOBJECT_macro(const T&amp;amp;) const [with T = Goo]':<br />qobject.h:535:9: instantiated from 'static typename QtPrivate::QEnableIf&amp;lt;((int)(QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::ArgumentCount) &gt;= (int)(QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::ArgumentCount)), void'''&gt;::Type QObject::connect(const typename QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::Object*, Func1, const typename QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::Object*, Func2, Qt::ConnectionType) [with Func1 = void (Goo::''')(), Func2 = void (QObject::''')(), typename QtPrivate::QEnableIf&amp;lt;((int)(QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::ArgumentCount) &gt;= (int)(QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::ArgumentCount)), void*&gt;::Type = void*, typename QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::Object = Goo, typename QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::Object = QObject]'<br />main.cc:4:68: instantiated from here<br />qobject.h:353:5: error: void value not ignored as it ought to be<br />make: '''* [main.o] Error 1<br /></code>
<br />h3. Type mismatch
<br /><code>
<br />#include &lt;QtCore/QtCore&amp;gt;<br />class Goo : public QObject {<br />Q_OBJECT<br />public:<br /> Goo() {<br /> connect(this, &amp;Goo::someSignal, this, &amp;Goo::someSlot1); //error<br /> connect(this, &amp;Goo::someSignal, this, &amp;Goo::someSlot2); //works<br /> }<br />signals:<br /> void someSignal(QString);<br />public:<br /> void someSlot1(int);<br /> void someSlot2(QVariant);<br />};<br /></code>
<br /><code><br />qobject.h: In static member function 'static typename QtPrivate::QEnableIf&amp;lt;((int)(QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::ArgumentCount) &gt;= (int)(QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::ArgumentCount)), void*&gt;::Type QObject::connect(const typename QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::Object*, Func1, const typename QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::Object*, Func2, Qt::ConnectionType) [with Func1 = void (Goo::''')(QString), Func2 = void (Goo::''')(int), typename QtPrivate::QEnableIf&amp;lt;((int)(QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::ArgumentCount) &gt;= (int)(QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::ArgumentCount)), void*&gt;::Type = void*, typename QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::Object = Goo, typename QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::Object = Goo]':<br />main.cc:6:62: instantiated from here<br />qobject.h:538:163: error: no type named 'IncompatibleSignalSlotArguments' in 'struct QtPrivate::CheckCompatibleArguments&amp;lt;QtPrivate::List&amp;lt;QString, void&amp;gt;, QtPrivate::List&amp;lt;int, void&amp;gt;, true&amp;gt;'<br />qobject.h: In static member function 'static void QtPrivate::FunctionPointer&amp;lt;Ret (Obj::''')(Arg1)&gt;::call(QtPrivate::FunctionPointer&amp;lt;Ret (Obj::''')(Arg1)&gt;::Function, Obj*, void''') [with Args = QtPrivate::List&amp;lt;QString, void&amp;gt;, Obj = Goo, Ret = void, Arg1 = int, QtPrivate::FunctionPointer&amp;lt;Ret (Obj::''')(Arg1)&gt;::Function = void (Goo::''')(int)]':<br />qobject.h:501:13: instantiated from 'void QObject::QSlotObject&amp;lt;Func, Args&amp;gt;::call(QObject*, void*''') [with Func = void (Goo::''')(int), Args = QtPrivate::List&amp;lt;QString, void&amp;gt;, QObject = QObject]'<br />main.cc:14:2: instantiated from here<br />qobject.h:109:13: error: cannot convert 'QtPrivate::RemoveRef&amp;lt;QString&amp;gt;::Type' to 'int' in argument passing<br />make: ''''''* [main.o] Error 1<br /></code>
<br />h2. Open Questions
<br />h3. Default arguments in slot
<br />if you have code like this:
<br /><code><br />class A : public QObject { Q_OBJECT<br /> public slots:<br /> void someSlot(int foo = 0);<br />};<br /></code>
<br />The old method allows you to connect that slot to a signal that does not have arguments.<br />But I cannot know with template code if a function has default arguments or not.<br />So this feature is disabled.
<br />There was an implementation that falls back to the old method if there are more arguments in the slot than in the signal.<br />This however is quite inconsistent, since the old method does not perform type-checking or type conversion. It was removed from the patch that has been merged.
<br />h3. Overload
<br />As you might see in the example, connecting to QAbstractSocket::error is not really beautiful since error has an overload, and taking the address of an overloaded function requires explicit casting.
<br />Some macro could help (with c11 or ''typeof'' extensions)
<br />The best thing is probably to recommend not to overload signals or slots …
<br />… but we have been adding overloads in past minor releases of Qt because taking the address of a function was not a use case we support. But now this would be impossible without breaking the source compatibility.
<br />h3. Disconnect
<br />Should QMetaObject::Connection have a disconnect() function?
<br />The other problem is that there is no automatic disconnection for some object in the closure if we use the syntax that take a closure.<br />One could add a list of object in the disconnection, or a new function like QMetaObject::Connection::require
<br /><code><br />auto c = connect(sender, &amp;Sender::valueChanged, [=](const QString &amp;newValue) {<br /> receiver-&gt;updateValue(&quot;senderValue&amp;quot;, newValue);<br /> } , QList&amp;lt;QObject&amp;gt; { receiver } ); // solution 1<br />c.require(receiver); // solution 2<br /></code>
<br />h3. Callbacks
<br />Function such as QHostInfo::lookupHost or QTimer::singleShot or QFileDialog::open take a QObject receiver and char* slot.<br />This do not work for the new method.<br />If one wants to do callback c''+ way, one should use std::function (or tr1)<br />But we cannot use STL types in our ABI, so a QFunction should be done to copy std::function.<br />This is anyway irrelevant for QObject connections.


* [[:Category:Developing Qt|Developing_Qt]]
== History ==
** [[:Category:Developing Qt::Qt-Planning|Qt Planning]]
*** [[:Category:Developing Qt::Qt-Planning::Qt-Public-Roadmap|Qt Public Roadmap]]

Revision as of 14:10, 23 February 2015


[toc align_right="yes&quot; depth="3&quot;]

New Signal Slot Syntax in Qt 5

This page was used to describe the new signal and slot syntax during its development. The feature is now released with Qt5.
* "Blog entry introducing it&quot;:http://woboq.com/blog/new-signals-slots-syntax-in-qt5.html
* "How it works&quot;:http://woboq.com/blog/how-qt-signals-slots-work-part2-qt5.html (implementation details)

Note: This is in addition to the old string-based syntax which remains valid.

Status

  • Already merged in qtbase/master

Connecting in Qt5

There will be several ways to connect a signal in Qt5.

Old syntax

Qt5 will continue to support the "old string-based syntax&quot;:http://doc.qt.nokia.com/latest/qobject.html#connect for connecting signals and slots defined in a QObject or any class that inherits from QObject (including QWidget)

<br />connect(sender, SIGNAL (valueChanged(QString,QString)),<br /> receiver, SLOT (updateValue(QString)) );<br />

New: connecting to QObject member

Here's a new way to connect two QObjects and pass non-string objects:

<br />connect(sender, &amp;Sender::valueChanged,<br /> receiver, &amp;Receiver::updateValue );<br />

pros

  • Compile time check of the existence of the signals and slot, of the types, or if the Q_OBJECT is missing.
  • Argument can be by typedefs or with different namespace specifier, and it works.
  • Possibility to automatically cast the types if there is implicit conversion (e.g. from QString to QVariant)
  • It is possible to connect to any member function of QObject, not only slots.

cons

  • More complicated syntax? (you need to specify the type of your object)
  • Very complicated syntax in cases of overloads?
  • Default arguments in slot is not supported anymore.

New: connecting to simple function

The new syntax can even connect to functions, not just QObjects:

<br />connect(sender, &amp;Sender::valueChanged, someFunction);<br />

pro

  • can be used with tr1::bind
  • can be used with c+11 lambda expressions


<br />connect(sender, &amp;Sender::valueChanged,<br /> tr1::bind(receiver, &amp;Receiver::updateValue, &quot;senderValue&amp;quot;, tr1::placeholder::_1) );
<br />connect(sender, &amp;Sender::valueChanged, [=](const QString &amp;newValue) {<br /> receiver-&gt;updateValue(&quot;senderValue&amp;quot;, newValue);<br /> } );<br />


h4. cons
* There is no automatic disconnection when the 'receiver' is destroyed
h2. Disconnecting in Qt5
As you might expect, there are some changes in how connections can be terminated in Qt5, too.
h3. Old way
You can disconnect in the old way (using SIGNAL, SLOT) but only if
* you connected using the old way, or
* if you want to disconnect all the slots from a given signal using wild card character
h3. Symetric to the function pointer one


<br />disconnect(sender, &amp;Sender::valueChanged,<br /> receiver, &amp;Receiver::updateValue );
<br />


Only works if you connected with the symmetric call, with function pointers (Or you can also use 0 for wild card)
In particular, does not work with static function, functors or lambda functions.
h3. New way using QMetaObject::Connection


<br />QMetaObject::Connection m_connection;<br />//…<br />m_connection = QObject::connect(…);<br />//…<br />QObject::disconnect(m_connection);<br />


Works in all cases, including lambda functions or functors.


h2. Asynchronous made easier.
With C11 it is possible to keep the code inline


<br />void doYourStuff(const QByteArray &amp;page)<br />{<br /> QTcpSocket '''socket = new QTcpSocket;<br /> socket-&gt;connectToHost(&quot;qt.nokia.com&amp;quot;, 80);<br /> QObject::connect(socket, &amp;QTcpSocket::connected, [socket, page] () {<br /> socket-&gt;write(QByteArray(&quot;GET &quot; + page + &quot;&quot;));<br /> });<br /> QObject::connect(socket, &amp;QTcpSocket::readyRead, [socket] () {<br /> qDebug()&lt;&lt; &quot;GOT DATA &quot;&lt;&lt; socket-&gt;readAll();<br /> });<br /> QObject::connect(socket, &amp;QTcpSocket::disconnected, [socket] () {<br /> qDebug()&lt;&lt; &quot;DISCONNECTED &quot;;<br /> socket-&gt;deleteLater();<br /> });
<br /> QObject::connect(socket, static_cast&amp;lt;void (QTcpSocket::''')(QAbstractSocket::SocketError)&gt;(&amp;QAbstractSocket::error), [socket] (QAbstractSocket::SocketError) {<br /> qDebug()&lt;&lt; &quot;ERROR &quot; &lt;&lt; socket-&gt;errorString();<br /> socket-&gt;deleteLater();<br /> });<br />}<br />


Here's a QDialog without re-entering the eventloop, and keeping the code where it belongs:


<br />void Doc::saveDocument() {<br /> QFileDialog '''dlg = new QFileDialog();<br /> dlg-&gt;open();<br /> QObject::connect(dlg, &amp;QDialog::finished, [dlg, this](int result) {<br /> if (result) {<br /> QFile file&amp;amp;#40;dlg-&gt;selectedFiles(&amp;#41;.first());<br /> // …<br /> }<br /> dlg-&gt;deleteLater();<br /> });
<br />}<br />


Another example using "QHttpServer&quot;:http://blog.nikhilmarathe.me/2011/02/qhttpserver-web-apps-in-qt.html : http://pastebin.com/pfbTMqUm



h2. Error reporting
Tested with GCC.
Fortunately, IDEs like Qt Creator simplifies the function naming
h3. forgot Q_OBJECT


<br />#include &lt;QtCore/QtCore&amp;gt;<br />class Goo : public QObject {<br /> Goo() {<br /> connect(this, &amp;Goo::someSignal, this, &amp;QObject::deleteLater);<br /> }<br />signals:<br /> void someSignal();<br />};<br />


<br />qobject.h: In member function 'void QObject::qt_check_for_QOBJECT_macro(const T&amp;amp;) const [with T = Goo]':<br />qobject.h:535:9: instantiated from 'static typename QtPrivate::QEnableIf&amp;lt;((int)(QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::ArgumentCount) &gt;= (int)(QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::ArgumentCount)), void'''&gt;::Type QObject::connect(const typename QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::Object*, Func1, const typename QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::Object*, Func2, Qt::ConnectionType) [with Func1 = void (Goo::''')(), Func2 = void (QObject::''')(), typename QtPrivate::QEnableIf&amp;lt;((int)(QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::ArgumentCount) &gt;= (int)(QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::ArgumentCount)), void*&gt;::Type = void*, typename QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::Object = Goo, typename QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::Object = QObject]'<br />main.cc:4:68: instantiated from here<br />qobject.h:353:5: error: void value not ignored as it ought to be<br />make: '''* [main.o] Error 1<br />


h3. Type mismatch


<br />#include &lt;QtCore/QtCore&amp;gt;<br />class Goo : public QObject {<br />Q_OBJECT<br />public:<br /> Goo() {<br /> connect(this, &amp;Goo::someSignal, this, &amp;Goo::someSlot1); //error<br /> connect(this, &amp;Goo::someSignal, this, &amp;Goo::someSlot2); //works<br /> }<br />signals:<br /> void someSignal(QString);<br />public:<br /> void someSlot1(int);<br /> void someSlot2(QVariant);<br />};<br />


<br />qobject.h: In static member function 'static typename QtPrivate::QEnableIf&amp;lt;((int)(QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::ArgumentCount) &gt;= (int)(QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::ArgumentCount)), void*&gt;::Type QObject::connect(const typename QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::Object*, Func1, const typename QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::Object*, Func2, Qt::ConnectionType) [with Func1 = void (Goo::''')(QString), Func2 = void (Goo::''')(int), typename QtPrivate::QEnableIf&amp;lt;((int)(QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::ArgumentCount) &gt;= (int)(QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::ArgumentCount)), void*&gt;::Type = void*, typename QtPrivate::FunctionPointer&amp;lt;Func&amp;gt;::Object = Goo, typename QtPrivate::FunctionPointer&amp;lt;Func2&amp;gt;::Object = Goo]':<br />main.cc:6:62: instantiated from here<br />qobject.h:538:163: error: no type named 'IncompatibleSignalSlotArguments' in 'struct QtPrivate::CheckCompatibleArguments&amp;lt;QtPrivate::List&amp;lt;QString, void&amp;gt;, QtPrivate::List&amp;lt;int, void&amp;gt;, true&amp;gt;'<br />qobject.h: In static member function 'static void QtPrivate::FunctionPointer&amp;lt;Ret (Obj::''')(Arg1)&gt;::call(QtPrivate::FunctionPointer&amp;lt;Ret (Obj::''')(Arg1)&gt;::Function, Obj*, void''') [with Args = QtPrivate::List&amp;lt;QString, void&amp;gt;, Obj = Goo, Ret = void, Arg1 = int, QtPrivate::FunctionPointer&amp;lt;Ret (Obj::''')(Arg1)&gt;::Function = void (Goo::''')(int)]':<br />qobject.h:501:13: instantiated from 'void QObject::QSlotObject&amp;lt;Func, Args&amp;gt;::call(QObject*, void*''') [with Func = void (Goo::''')(int), Args = QtPrivate::List&amp;lt;QString, void&amp;gt;, QObject = QObject]'<br />main.cc:14:2: instantiated from here<br />qobject.h:109:13: error: cannot convert 'QtPrivate::RemoveRef&amp;lt;QString&amp;gt;::Type' to 'int' in argument passing<br />make: ''''''* [main.o] Error 1<br />


h2. Open Questions
h3. Default arguments in slot
if you have code like this:


<br />class A : public QObject { Q_OBJECT<br /> public slots:<br /> void someSlot(int foo = 0);<br />};<br />


The old method allows you to connect that slot to a signal that does not have arguments.
But I cannot know with template code if a function has default arguments or not.
So this feature is disabled.
There was an implementation that falls back to the old method if there are more arguments in the slot than in the signal.
This however is quite inconsistent, since the old method does not perform type-checking or type conversion. It was removed from the patch that has been merged.
h3. Overload
As you might see in the example, connecting to QAbstractSocket::error is not really beautiful since error has an overload, and taking the address of an overloaded function requires explicit casting.
Some macro could help (with c11 or typeof extensions)
The best thing is probably to recommend not to overload signals or slots …
… but we have been adding overloads in past minor releases of Qt because taking the address of a function was not a use case we support. But now this would be impossible without breaking the source compatibility.
h3. Disconnect
Should QMetaObject::Connection have a disconnect() function?
The other problem is that there is no automatic disconnection for some object in the closure if we use the syntax that take a closure.
One could add a list of object in the disconnection, or a new function like QMetaObject::Connection::require


<br />auto c = connect(sender, &amp;Sender::valueChanged, [=](const QString &amp;newValue) {<br /> receiver-&gt;updateValue(&quot;senderValue&amp;quot;, newValue);<br /> } , QList&amp;lt;QObject&amp;gt; { receiver } ); // solution 1<br />c.require(receiver); // solution 2<br />


h3. Callbacks
Function such as QHostInfo::lookupHost or QTimer::singleShot or QFileDialog::open take a QObject receiver and char* slot.
This do not work for the new method.
If one wants to do callback c+ way, one should use std::function (or tr1)
But we cannot use STL types in our ABI, so a QFunction should be done to copy std::function.
This is anyway irrelevant for QObject connections.

History