0

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

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
tez
  • 4,990
  • 12
  • 47
  • 67

2 Answers2

3

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.

hyde
  • 60,639
  • 21
  • 115
  • 176
  • Is there any real advantage of a QByteArray over a plain char[] in this case - doesn't it just obsfurcate the code? (ps Not a flame - genuine question) – Martin Beckett Nov 21 '12 at 19:28
  • QByteArray puts the real data into heap, and the data is implicitly shared, so you can just return QByteArray from a method without needing to worry about who will delete/free the data. QByteArray can be easily resized. Many Qt methods take QByteArray, in which case you do not need to have extra parameter for buffer size, and usually you do not need to separately keep track of size, QByteArray does that for you. So, as soon as you start implementing any of this on top of char[], it's usually better to switch to QByteArray. But when you want just simple local buffer in stack, then use char[]. – hyde Nov 22 '12 at 08:57
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.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • char data_ser[1024]; port->readLine(data_ser,sizeof(data_ser)); QString data_new(data_ser); Like this? – tez Nov 21 '12 at 18:37
  • Yes, there's probably a parameter to readline to limit the size of data being read but 1K should be safe (famous last words!) – Martin Beckett Nov 21 '12 at 18:47
  • Andddd its working like a charm ;) ... thanx to you I'm able to make my own serial data monitor :) – tez Nov 21 '12 at 19:00
  • ...and you want to explicitly specify the encoding when going from char*/QByteArray to QString, e.g. using fromUtf8, fromLatin1, etc., and not the implicit conversion. – Frank Osterfeld Nov 21 '12 at 19:23
  • @FrankOsterfeld - Good point - although if you are reading from a data logger on a serial port it's good bet that's it's pretty plain ascii – Martin Beckett Nov 21 '12 at 19:27
  • Important detail about `QString` is, it does not store bytes (C++ type `char`), it stores unicode characters of unspecified encoding (ok, in practice UTF-16 I think, but you should not rely on that). So never read bytes to `QString`, always use a byte buffer (char[], char*, QByteArray) and approripate encoding/decoding methods. – hyde Nov 22 '12 at 09:02