Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

You can't, or at least you do not want to (of course in C++ you can cast QChar* to char* but that will not make it work). Just read data into QByteArray, then convert that to QString.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top