Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Weird of TCP client read function
Forum Updated to NodeBB v4.3 + New Features

Weird of TCP client read function

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phong
    wrote on last edited by
    #1

    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()

    if(handler->Connect()){
        qDebug() << "success";
    }else{
         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.

    thx~

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      P 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        P 1 Reply Last reply
        1
        • mrjjM mrjj

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

          P Offline
          P Offline
          Phong
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

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

            P Offline
            P Offline
            Phong
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved