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. why 《gui qt4 pro 》 wirte such a code?
Forum Updated to NodeBB v4.3 + New Features

why 《gui qt4 pro 》 wirte such a code?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.2k 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.
  • ArchieveA Offline
    ArchieveA Offline
    Archieve
    wrote on last edited by
    #1

    as we usually do:
    tcpsocket->write(str); tcpsocket->flush();
    the the str can be send to socket(not the buffer);

    while the "gui qt4 pro" in the 15 tcp pro write such code:

    `
    connect(&tcpSocket, SIGNAL(connected()), this, SLOT(sendRequest()));

    void TripPlanner::sendRequest()
    {
    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_3);
    out << quint16(0) << quint8('S') << fromComboBox->currentText()
    << toComboBox->currentText() << dateEdit->date()
    << timeEdit->time();

    if (departureRadioButton->isChecked()) {
        out << quint8('D');
    } else {
        out << quint8('A');
    }
    out.device()->seek(0);
    out << quint16(block.size() - sizeof(quint16));
    tcpSocket.write(block);
    
    statusLabel->setText(tr("Sending request..."));
    

    }
    ·

    i had look it a long time,but can't find the "flush()" or "waitforbyteswritten(), so why the str can
    be send and what does the function do?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Hi and welcome to devnet forum

      Certainly you can use also flush(), but it is not mandatory. The same is for waitForBytesWritten(). It may depended on your application. If you need to know that the information is transferred before you continue with some activitiy you may call either function.

      When your application may continue with its activities without ensuring that the information has been written, you basically can leave in to OS.

      I gues in most cases there is not really a difference and therefore there is no need to use flush and waitForBytesWritten. If you have a very slow communication line this may be different.

      Vote the answer(s) that helped you to solve your issue(s)

      ArchieveA 1 Reply Last reply
      0
      • K koahnig

        Hi and welcome to devnet forum

        Certainly you can use also flush(), but it is not mandatory. The same is for waitForBytesWritten(). It may depended on your application. If you need to know that the information is transferred before you continue with some activitiy you may call either function.

        When your application may continue with its activities without ensuring that the information has been written, you basically can leave in to OS.

        I gues in most cases there is not really a difference and therefore there is no need to use flush and waitForBytesWritten. If you have a very slow communication line this may be different.

        ArchieveA Offline
        ArchieveA Offline
        Archieve
        wrote on last edited by
        #3

        @koahnig yes, yout meanings i know, but in the codes, the “blcok” is writed to the socket,and when i use write ,because the app continue to activities , the data can not be sent. i want to know the two codes differents, why i can't ,but it can?

        K 1 Reply Last reply
        0
        • ArchieveA Archieve

          @koahnig yes, yout meanings i know, but in the codes, the “blcok” is writed to the socket,and when i use write ,because the app continue to activities , the data can not be sent. i want to know the two codes differents, why i can't ,but it can?

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @Archieve
          Not exactly. Assuming that you have threading through your operation system, there should be a different thread for sending the information in the background. This may take some time, but it is typically not really a problem.

          If you like to be sure that the information is being transmitted directly, you should use flush.

          Vote the answer(s) that helped you to solve your issue(s)

          ArchieveA 1 Reply Last reply
          0
          • K koahnig

            @Archieve
            Not exactly. Assuming that you have threading through your operation system, there should be a different thread for sending the information in the background. This may take some time, but it is typically not really a problem.

            If you like to be sure that the information is being transmitted directly, you should use flush.

            ArchieveA Offline
            ArchieveA Offline
            Archieve
            wrote on last edited by
            #5

            @koahnig OK, thank you , now i'll use the flush as much as possible

            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