<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Cannot connect QSignalMapper to an own object]]></title><description><![CDATA[<p dir="auto">Hello there</p>
<p dir="auto">A few days ago I rewrote my application and came to a point, where I need parameters in a SIGNAL/SLOT connection. I found a very helpful article here:  "Mapping":<a href="http://doc.qt.digia.com/qq/qq10-signalmapper.html" target="_blank" rel="noopener noreferrer nofollow ugc">http://doc.qt.digia.com/qq/qq10-signalmapper.html</a></p>
<p dir="auto">I made a little test with the subclassing approach, but it's get messy. I cannot use sender(), because the signal is emited by the QTcpSocket and not the object itself (which contains the QTcpSocket)</p>
<p dir="auto">This is my server class . It contains the common information about a server connection and the socket:</p>
<p dir="auto">@Server::Server(QString hostname, int port, QString username, QObject *parent) : QObject(parent)<br />
{<br />
dataHostname = hostname;<br />
dataPort = port;<br />
dataUsername = username;<br />
networkSocket = new QTcpSocket(this);<br />
}</p>
<p dir="auto">QString Server::getHostname()<br />
{<br />
return dataHostname;<br />
}</p>
<p dir="auto">int Server::getPort()<br />
{<br />
return dataPort;<br />
}</p>
<p dir="auto">QString Server::getUsername()<br />
{<br />
return dataUsername;<br />
}</p>
<p dir="auto">QTcpSocket *Server::getSocket()<br />
{<br />
return networkSocket;<br />
}<br />
@</p>
<p dir="auto">Now the plan is to know: When is a socket connected and which object is connected ?</p>
<p dir="auto">@// Setup the QSignalMapper<br />
Network::Network(QObject <em>parent): QObject(parent)<br />
{<br />
instanceMapper = new QSignalMapper(this);<br />
connect(instanceMapper, SIGNAL(mapped(Server</em>)), this, SLOT(slotconnected(Server*)));<br />
}</p>
<p dir="auto">// After a server object is created, we call initServer to add the SIGNAL/SLOT connection (QTcpSocket::connected signal)<br />
void Network::initSocket(Server *server)<br />
{<br />
instanceMapper-&gt;setMapping(server-&gt;getSocket(), server);<br />
connect(server-&gt;getSocket(), SIGNAL(connected()), instanceMapper, SLOT(map()));<br />
}</p>
<p dir="auto">// Slot with the server as parameter<br />
void Network::slotconnected(Server *server)<br />
{<br />
qDebug() &lt;&lt; "The server " &lt;&lt; server-&gt;getHostname() &lt;&lt; " is now in connected state";<br />
}@</p>
<p dir="auto">This does compile, but after startup:<br />
Object::connect: No such signal QSignalMapper::mapped(Server*) in /home/simon/Downloads/Client_Qt4/src/libserver/network/Network.cpp:25</p>
<ul>
<li>Exited normally -</li>
</ul>
<p dir="auto">So how can I achieve this ? The QTcpSocket should emit the connected() signal and with the QSignalMapper I should be able to get the server object ?</p>
<p dir="auto">Thank you &amp; Nice weekend</p>
]]></description><link>https://forum.qt.io/topic/29404/cannot-connect-qsignalmapper-to-an-own-object</link><generator>RSS for Node</generator><lastBuildDate>Tue, 12 May 2026 10:28:28 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/29404.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 14 Jul 2013 11:31:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Cannot connect QSignalMapper to an own object on Tue, 16 Jul 2013 16:08:32 GMT]]></title><description><![CDATA[<p dir="auto">Hello</p>
<p dir="auto">I solved the problem. Thery are 2 solutiuons <strong>(Please see the question for solution 2 at the end)</strong>:</p>
<p dir="auto"><em>Solution 1</em>: Connecting a QObject to a QSignalMapper and cast back to Server with qobject_cast - I thought something is not possible, but it is:</p>
<p dir="auto">@ServerManager::ServerManager(QObject <em>parent) : QObject(parent)<br />
{<br />
instanceSignalMapperConnect = new QSignalMapper(this);<br />
connect(instanceSignalMapperConnect, SIGNAL(mapped(QObject</em>)), this, SLOT(serverConnected(QObject*)));<br />
}</p>
<p dir="auto">Server *ServerManager::addServer(QString hostname, int port, QString username)<br />
{<br />
Server *server = new Server(hostname, port, username, this);</p>
<p dir="auto">instanceSignalMapperConnect-&gt;setMapping(server-&gt;getSocket(), server);<br />
connect(server-&gt;getSocket(), SIGNAL(connected()), instanceSignalMapperConnect, SLOT(map()));<br />
listServers.append(server);</p>
<p dir="auto">return server;<br />
}</p>
<p dir="auto">void ServerManager::serverConnected(QObject *object)<br />
{<br />
Server *server = qobject_cast&lt;Server *&gt;(object);<br />
if(server)<br />
{<br />
qDebug() &lt;&lt; server-&gt;getHostname() &lt;&lt; "is now in connected state";<br />
}<br />
}@</p>
<p dir="auto"><em>Solution 2</em>: The first solution generates an overhead (Connect/Disconnect/ReadyRead/Error etc connection) - things get messy, so I switched over to lambdas and C++11:</p>
<p dir="auto">@Server *server = new Server("localhost", 9000, "User", this);</p>
<p dir="auto">QObject::connect(server-&gt;getSocket(), &amp;QTcpSocket::connected, [this, server] ()<br />
{<br />
qDebug() &lt;&lt; server-&gt;getHostname() &lt;&lt; "is now connected";<br />
emit this-&gt;signalInThisClass(server);<br />
});@</p>
<p dir="auto"><em>But I have one question</em>:Do I have to unconnect the Signal in the lambda connection --&gt; Memory leak ? Or can I leave it that way ? I read something about that, but for me it's not clear</p>
<p dir="auto">Thanks</p>
]]></description><link>https://forum.qt.io/post/186688</link><guid isPermaLink="true">https://forum.qt.io/post/186688</guid><dc:creator><![CDATA[Threadcore]]></dc:creator><pubDate>Tue, 16 Jul 2013 16:08:32 GMT</pubDate></item><item><title><![CDATA[Reply to Cannot connect QSignalMapper to an own object on Sun, 14 Jul 2013 13:56:12 GMT]]></title><description><![CDATA[<p dir="auto">Well, the class Server should act as a data object - this means no signals/emits or worker-functions inside this class - they are handled by other classes (like Network)</p>
<p dir="auto">How can I understand your second way ?</p>
]]></description><link>https://forum.qt.io/post/186350</link><guid isPermaLink="true">https://forum.qt.io/post/186350</guid><dc:creator><![CDATA[Threadcore]]></dc:creator><pubDate>Sun, 14 Jul 2013 13:56:12 GMT</pubDate></item><item><title><![CDATA[Reply to Cannot connect QSignalMapper to an own object on Sun, 14 Jul 2013 13:46:06 GMT]]></title><description><![CDATA[<p dir="auto">There are probably more than one solution.<br />
However, you can connect the signal of QTcpSocket to a signal of your object hosting QTcpSocket.<br />
Also you can connect the signal to a hosting object's slot and emit again. This may make the QSignalMapper obsolete.</p>
]]></description><link>https://forum.qt.io/post/186349</link><guid isPermaLink="true">https://forum.qt.io/post/186349</guid><dc:creator><![CDATA[koahnig]]></dc:creator><pubDate>Sun, 14 Jul 2013 13:46:06 GMT</pubDate></item></channel></rss>