c++qtserial-portqchar

Qchar* to char* conversion


I'm building a Qt Gui application for monitoring data from serial port.I'm using qextserialport library. Here's the problem I ran into.

void MainWindow::onDataAvailable()
{
    QString data_ser,data_trimmed;
    port->readLine(data_ser.data(),0);
    data_trimmed = data_ser.trimmed();
    ui->textEdit->append(data_trimmed);
}

readLine method's first argument should be of type char*.How to convert QChar* returned by data_ser.data() into char*.I could have used std::string instead of QString but qt gui objects are better compatible with QString and I need the trimmed method too.

Here's the error i'm getting :

no matching member function for call to 'readLine'. no known conversion from 'QChar *' to 'char *' for 1st argument.

How to solve this??


Solution

  • This is a little tricky because you need space to have been allocated for readline() to copy into.

    You can create a QString with an initial size but personally I would allocate a char* with a fixed reasonable buffer size and then create the QString from that - it makes it a lot clearer when you are debugging.