Please nominate your Qt Champions for 2021!

[SOLVED] QObject::startTimer error when using QSerialPort::write()

  • Hi,
    I'm using QSerialPort to manage a RS232 connection. I connected two serial port on my PC (two serial adapter) and I'm trying to read and write data between them.
    I read the various helps and tutorials, and at now I successfull open the QSerialPort connection and read data from a common terminal (putty).
    The problems starts when I try to write data. I tried to echo a character received:
    int main(int argc, char *argv[])
    QSerialPortInfo temp;
    QList<QSerialPortInfo> lista = QSerialPortInfo::availablePorts();
    //...choose the right port.
    QSerialPort *port = new QSerialPort(temp);
    if (!port->open(QSerialPort::ReadWrite)){
    //manage the fault and close the port.
    err = port->error();
    return err;
    //set the serial port
    port->setBaudRate(QSerialPort::Baud115200); //115200
    port->setDataBits(QSerialPort::Data8); //8
    port->setParity(QSerialPort::NoParity); //N
    port->setStopBits(QSerialPort::OneStop); //1
    QByteArray dataW;

    //read from the port
        if (port->waitForReadyRead(-1));
        dataW = port->readAll();
        //show in terminal
        qDebug() << dataW;
    return 0;


    When I write a character in the terminal, it is correctly show in the qDebug and in the terminal (so the character is read and written on the terminal and the serial port), but after the write command I read:

    QObject::startTimer: Timers can only be used with threads started with QThread

    I did all the application in main() function, so I don't use threads. What's goes wrong?

  • I think you need to add waitForBytesWritten() after of flush() see
    "QSerialPort::flush()": about absent event loop for details.

    [EDIT] Of course "use waitForBytesWritten() instead of flush()". Thanks kuzulis.

  • Qt Champions 2020

    Do not use flush() at all, use waitForBytesWritten(-1) instead.

    Also, using of QCoreapplication is mandatory option.

  • Thanks andreyc and kuzulis,

    using waitForBytesWritten(-1) instead of flush() and creating a QApplication object, the problem is solved.

    Thanks for your helpful reply.

Log in to reply