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. Updating a textbox with a text sent from client
Forum Updated to NodeBB v4.3 + New Features

Updating a textbox with a text sent from client

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.7k Views 1 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.
  • M Offline
    M Offline
    mansi
    wrote on last edited by
    #1

    Hello,
    I am trying to create a simple chat between client and server using socket programming.
    I want to update the text in server dialog box to a message sent from client.
    I am not able to figure out how the message should go to the textbox in server dialog box.
    Here is my function to send data from client : .cpp

    void Client::senddata() //----------------------------------------------
    {

    send->setText("clicked");
    
    textinput = messageedit->text();
    
    if(!textinput.isEmpty())
    

    {

    tcpSocket->write(QString(textinput).toUtf8());
      //  tcpSocket->write((textinput).toUtf8());
        tcpSocket->flush();
    

    }
    } #

    and server function to read data :

    void Server::on_readdata()
    {

    //while(tcpSocket->canReadLine())
    

    // {

     getbutton->setText("recieved");
     QByteArray ba = tcpSocket->readLine();
    
     //QString textinput = ba;
     QString textinput(ba);
    /* if(strcmp(ba.constData(), "!exit\n") == 0)
     {
         tcpSocket->disconnectFromHost();
         break;
     } */
    
     message->setText((const QString)textinput);
    

    } #

    Thanks a lot in advance,
    Mansi

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mansi
      wrote on last edited by
      #2

      client code :

      @void Client::senddata() //———————————————————————
      {
      send->setText(“clicked”); textinput = messageedit->text(); if(!textinput.isEmpty()) { tcpSocket->write(QString(textinput).toUtf8()); // tcpSocket->write((textinput).toUtf8()); tcpSocket->flush(); } } #

      and server function to read data :@

      server code
      @void Server::on_readdata() {
      //while(tcpSocket->canReadLine()) // { getbutton->setText(“recieved”); QByteArray ba = tcpSocket->readLine(); //QString textinput = ba; QString textinput(ba); /* if(strcmp(ba.constData(), “!exit\n”) == 0) { tcpSocket->disconnectFromHost(); break; } */ message->setText((const QString)textinput); }@

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        check out the "Fortune example":http://qt-project.org/doc/qt-4.8/network-fortuneserver.html , it should clear things up for you.
        In the example the clients reads the data from the server.

        Basically you need to listen to the readyRead() signal which gets triggered every time new data is available. Until there is no more data which was sent.

        I don't know how/when you trigger on_readdata(), but i think you call it too early and thus it wont do anything at the end.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mansi
          wrote on last edited by
          #4

          thanks a lot raven :) ... with little changes in the code , able to send messages bi directionally :)

          1 Reply Last reply
          0

          • Login

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