why 《gui qt4 pro 》 wirte such a code?



  • 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?


  • Moderators

    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.



  • @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?


  • Moderators

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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.