Qt World Summit: Register Today!

Signals are not triggering common slot

  • Hi!

    I am using QUdpSocket from multicast. m_listUDP is a QList<QUdpSocket*>.
    Each one is connected to a common signal : connect(socketToAppend, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));

    The problem : readPendingDatagrams is never triggered. If I use a just a QUdpSocket* it is working. It seems the connection is not working cause to the QList<QUdpSocket*>.

    Maybe I forgot something?

        udpSocketSend = new QUdpSocket(this);
        bcast = new QHostAddress("");
        m_listHost = QNetworkInterface::allInterfaces();
        for(int i(0); i < m_listHost.count(); i++)
                    && m_listHost.at(i).flags().testFlag(QNetworkInterface::IsRunning)
                    && m_listHost.at(i).flags().testFlag(QNetworkInterface::CanBroadcast)
                    && m_listHost.at(i).flags().testFlag(QNetworkInterface::CanMulticast) )
    			//Get IP address of interface
                QNetworkInterface iface = QNetworkInterface::interfaceFromName(m_listHost.at(i).name());
                QList<QNetworkAddressEntry> entries = iface.addressEntries();
                QNetworkAddressEntry entry = entries.at(1);
                QHostAddress ip = entry.ip();
    	    QUdpSocket *socketToAppend = new QUdpSocket(this);
                socketToAppend->bind(ip, udpSocketSend->localPort());
                connect(socketToAppend, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
        udpSocketSend->connectToHost(*bcast, 1950);
        QByteArray datagram;
        udpSocketSend->writeDatagram(datagram.data(), datagram.size(),
                                     QHostAddress::Broadcast, 1950);
    void Classic::readPendingDatagrams()
        QUdpSocket* socketToAppend = qobject_cast<QUdpSocket*>(sender());
        while (socketToAppend->hasPendingDatagrams())
            QByteArray datagram;
            QHostAddress sender;
            quint16 senderPort;
            socketToAppend->readDatagram(datagram.data(), datagram.size(),
                                        &sender, &senderPort);
            qDebug() << datagram;

    Thank you

  • connectToHost is asynchronous, either you add udpSocketSend->waitForConnected(); after udpSocketSend->connectToHost(*bcast, 1950); or you write the datagram in a slot connected to the connected() signal of udpSocketSend

    if your compiler supports C++11 the sender() monster should be always avoidable:

    add the sender as argument
    void Classic::readPendingDatagrams(QUdpSocket* sender)

    and connect using a lambda:
    connect(socketToAppend, &QUdpSocket::readyRead, [socketToAppend,this]()->void{readPendingDatagrams(socketToAppend);});

  • Qt Champions 2017

    in addition previous post, it is not issue with connect. If your loop is executing, it connect works perfectly. However issue may be other end of the UDP socket may not be sending the data at all to you end point. So readyRead() signal may not be emitted at all. Please check whether other udp end is sending the data.

Log in to reply