Qchar* to char* conversion

tez picture tez · Nov 21, 2012 · Viewed 7.9k times · Source

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

Answer

hyde picture hyde · Nov 21, 2012

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.