Important: Please read the Qt Code of Conduct -

QTcpSocket QIODevice::readyRead signal not emitted.

  • Hello guys

    I made a client library tpcip in QT, when I import this library into csharp console project, client connects to the server successfully (connected slot is emit) and when the server sends data to the client, the client does not receive the data, does not trigger signal ReadyRead is emitted.. If I make test in the QT console application, then it works!

    Git hub repositories:

    My library in QT

    csharp console project

    QT Creator version 4.9.0
    QT version 5.12.2 (MSVC 2017, 32 bit)

    Microsoft Visual Studio Professional 2017
    Version 15.9.11
    Microsoft .NET Framework
    Version 4.7.03062

    How to fix this?



  • The answer is you don't have an event loop.
    The a.exec(); in the C++ project starts an event loop that takes care of signals and slots. The C# project never calls that method

  • @VRonin


    but in the C# anothers signals work fine, eg.: When the client C# send a data to server, the signal bytesWritten is trigger, see on figure console print "bytesWritten 13", and every send data, the message it's printed,
    where C# receives the signal bytesWritten. Thank you.

    In Qt

    // Constructor Class
    TcpClient::TcpClient(QObject *parent)
    : QObject(parent)
    , socket(new QTcpSocket(this))
    connect(socket, &QIODevice::readyRead, this, &TcpClient::readData);
    connect(socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));

    // This signal is trigger to C#
    void TcpClient::bytesWritten(qint64 bytes)
    qDebug() << "bytesWritten " << bytes;

    // But it's not!!!
    void TcpClient::readData()

  • Lifetime Qt Champion


    I can only quote @VRonin again:

    The answer is you don't have an event loop.

    Even your example may not work on every O/S or Qt version. Not running the event loop is undefined behavior for most Qt classes.


  • signals and slots living on the same thread are executed as they were direct calls to the slot, they don't require an event loop. The socket system, instead, requires the loop to run to be able to work

  • @VRonin and @aha_1980,

    Thanks a lot!

Log in to reply