Koos
Koos

Reputation: 91

How do you delete a sub-string in a string(qstring) in Qt

Is there a function in Qt similar to delete() and copy() in Delphi.

I am reading data from a device connected to my computer via USB and storing it as a QString. Every line that is read is not the same (or is cut short, even while using readyRead). I created a buffer sting to add these "half sting" (eg. string = "This" instead of "This is a string#") to and now I want to copy the string up until the '#' and then delete the string so if new "half stings" get added I can do the same with them. The code below is what I tried

void MainWindow::readSerial()
{
    QByteArray serialData = port->readAll();
    serialBuffer += serialData;

    QByteArray serialString = serialBuffer.

    qDebug() << serialString;
    ui -> textEdit ->append(serialString);

    //serialBuffer.replace(serialString,"");
}

The above code only returns an empty string.

Upvotes: 1

Views: 3090

Answers (1)

Koos
Koos

Reputation: 91

void MainWindow::readSerial()
{
    QByteArray serialData = port->readAll();
    serialBuffer += serialData;

    QString serialString = serialBuffer.mid(serialBuffer.indexOf("$"),serialBuffer.indexOf("\r\n"));

    qDebug()<< "index of \r\n" << serialBuffer.indexOf("\r\n");
    qDebug() << "SerialString" <<serialString;
    ui -> textEdit ->append(serialString);
    qDebug() << "SerialBuffer: " << serialBuffer;

    serialBuffer.replace(serialString + "\r\n","");

}

the above code works. Thanks all.

regards

Upvotes: 1

Related Questions