Important: Please read the Qt Code of Conduct -

Weird of TCP client read function

  • Hello , I was successfully build a TCP client with QTcpSocket and it works fine both read and write data.
    However when I move my connect tcp code from mainwindow constructor to the pushbutton clicked slot function
    I can't read the data from server end anymore !! I have no idea what's happened...
    I wanna switch tcp connection on and off and also update IP address when I clicked the button.

    Here is the code :
    in mainwindow constructor or on_clicked_connectBtn()

        qDebug() << "success";
         qDebug() << "fail";

    here is the code in handler->connect()

    bool Handler::connect{
        this->Socket = new QTcpSocket();
        this->Socket->ConnectToHost(ip, port);
        connect(this->Socket, SIGNAL(readyRead()), this, SLOT(Read));
        return Socket->waitForConnect();

    and just

    qDebug() << this->Socket->readAll();

    in the Read() function.


  • Lifetime Qt Champion

    But does it connect?
    qDebug() << "success";
    Should not make any difference when moved from constructor :)

  • Lifetime Qt Champion


    Maybe a silly question but did you lose a set of parenthesis on your slot when moving your code in your connect statement ?

  • @mrjj
    Yes, It will show sucess, so that's why I confused...

  • @SGaist
    Yup , I found the problem, It's a silly question, but not cuz the SLOT not connect.
    I new the TcpSocket every time when I clicked the button, so it just disconnect with TCP...
    now I solve it, thanks a lot.

Log in to reply