Note that there are some explanatory texts on larger screens.

plurals
  1. POQt5 slot being called multiple times from a single emit statement
    primarykey
    data
    text
    <p>I'm relatively new to Qt, but I have done a little searching around. I have a base class that handles UDP broadcasting, and does the connect statements in the constructor of the class like this:</p> <pre><code>NetworkConnection::NetworkConnection(QObject *parent) : QObject(parent) // Based on QObject , m_server_search( new QUdpSocket ) // Our UDP Broadcast socket , m_waiting_for_server( false ) , m_found_server( false ) { qDebug() &lt;&lt; "NetworkConnection::constructor"; connect( m_server_search, SIGNAL(readyRead()), this, SLOT(serverResponse()), Qt::UniqueConnection ); if ( m_server_search-&gt;bind( QHostAddress::AnyIPv4, (quint16)PORT_MULTICAST, QUdpSocket::ShareAddress ) ) { if ( m_server_search-&gt;joinMulticastGroup( QHostAddress( MULTICAST_GROUP ) ) ) { connect( this, SIGNAL(broadcast(NetworkMessage)), this, SLOT(broadcast_message(NetworkMessage)), Qt::UniqueConnection ); this-&gt;m_ping_timer = this-&gt;startTimer(2000); qDebug() &lt;&lt; "Ping timer id=" &lt;&lt; this-&gt;m_ping_timer; } else qDebug() &lt;&lt; "Couldn't start multicast listener"; } else qDebug() &lt;&lt; "Couldn't bind multicast to port" &lt;&lt; PORT_MULTICAST; } </code></pre> <p>I set up a signal/slot interface for broadcasting:</p> <pre><code>signals: void serverFound(); void serverNotFound(); void broadcast(NetworkMessage); private slots: void serverResponse(); void broadcast_message( NetworkMessage msg ); </code></pre> <p>And <code>broadcast_message</code> looks like this:</p> <pre><code>void NetworkConnection::broadcast_message( NetworkMessage msg ) { QByteArray raw = msg.toString(); qDebug() &lt;&lt; "NetworkConnection::broadcast_message&gt;" &lt;&lt; raw; if ( m_server_search-&gt;writeDatagram( raw.data(), raw.size(), QHostAddress(MULTICAST_GROUP), (quint16)PORT_MULTICAST ) &lt; 1 ) qDebug() &lt;&lt; "Failed broadcast last message"; } </code></pre> <p>My timer works well, and here is the code:</p> <pre><code>void NetworkConnection::timerEvent(QTimerEvent *event) { qDebug() &lt;&lt; "NetworkConnection::timerEvent with id" &lt;&lt; event-&gt;timerId() &lt;&lt; "(ping timer=" &lt;&lt; this-&gt;m_ping_timer &lt;&lt; ")"; if ( event-&gt;timerId() == this-&gt;m_ping_timer ) { qDebug() &lt;&lt; "NetworkConnection::pingForServer"; if ( m_waiting_for_server &amp;&amp; !m_found_server ) { qDebug() &lt;&lt; "Server not found!"; emit this-&gt;serverNotFound(); return; } if ( !m_found_server ) { qDebug() &lt;&lt; "Sending a ping to the server"; NetworkMessage msg( m_software_guid, get_microseconds(), QString("whoisaserver") ); emit this-&gt;broadcast( msg ); m_waiting_for_server = true; m_found_server = false; } } } </code></pre> <p>I only get the text "Sending a pint to the server" once, but my broadcast_message outputs it's <code>qDebug()</code> multiple times.</p> <p>I'm not explicitly using multiple threads, and as you can see I'm using Qt::UniqueConnection, which is apparently having no affect?</p> <p>SO why would the slot be called multiple times? I've even tried debugging it a little and just calling this->broadcast( ... ) without using emit, and it still gets called multiple times.</p> <p><strong>Edit:</strong> I just added a counter to the <code>broadcast_message</code> slot, and it gets called 340 times. Is there any significance to that?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload