Results 1 to 7 of 7

Thread: Issue with qDebug() showing only the first character of a QString

  1. #1
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Issue with qDebug() showing only the first character of a QString

    Hi i'm trying to convert a Qstring to double but i only receive a zero.

    Qt Code:
    1. std::string message(usb_data.constData(), 60); //usb_data.length()
    2. QString qmessage = QString::fromStdString(message);
    3. QString latitude = qmessage.mid(0,21);
    4. QString longitude = qmessage.mid(23,25);
    5. QString height = qmessage.mid(50);
    6.  
    7. QString lat_mid = qmessage.mid(0,18);
    8. QString lon_mid = qmessage.mid(23,21);
    9.  
    10.  
    11.  
    12.  
    13. double lat = lat_mid.toDouble();
    14. double lon = lon_mid.toDouble();
    15.  
    16. qDebug() << lat << latitude;
    To copy to clipboard, switch view to plain text mode 

    my output is 0 "4

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,387
    Thanks
    247
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Issue with qDebug() showing only the first character of a QString

    So when you look at the contents of message, qmessage, latitude, longitude, height, and the others in the debugger, what do you see? How do you know that usb_data is ASCII and not binary? What does usb_data look like in the debugger? How do you know that usb_data is actually 60 bytes long? How do you know that qmessage is long enough to contain all of the characters you are trying to split out of it?

  3. #3
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Issue with qDebug() showing only the first character of a QString

    The data from the usb_data are NMEA data so it's ascii. The usb_data on debugger is "4 where it supposed to be a line of numbers. If i use qDebug() << lat_mid.toLatin1().toHex();
    the output is “34003000340035002e003800390032003100” but if i use qDebug() << lat_mid.toLatin1(); the output is “4. I know that the bytes are 60 byte long because i'm
    sending them from a uC. And i'm using a Qtextedit to show these variables, something like a console.

    On the console i'm seeing
    latitude 40.705465,E
    longitude -74.026566,S
    lat_mid 40.705465
    lon_mid -74.026566


    but the qDebug is showing only the "4 on the latitude variable. There must be non-printable characters because what i'm trying to do is to convert a string to double.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issue with qDebug() showing only the first character of a QString

    “34003000340035002e003800390032003100” in hex clearly indicates this is not ascii data as the second byte is null (0x00). Considering the fact that every second byte of the string seems to be 0x00, I would guess you have utf-16 encoded data, similar to what is stored internally in QString. So I'm guessing that either your data was never ASCII or it was but then you converted it to something that wasn't ASCII anymore and then converted it to binary data in an incorrect way.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Issue with qDebug() showing only the first character of a QString

    This is the flow of my program.

    Qt Code:
    1. void HID_PnP::PollUSB()
    2. {
    3. buf[0] = 0x00;
    4. memset((void*)&buf[2], 0x00, sizeof(buf) - 2);
    5.  
    6. if (isConnected == false) {
    7. device = hid_open(0x04D8, 0x003F, NULL);
    8.  
    9. if (device) {
    10. isConnected = true;
    11. hid_set_nonblocking(device, true);
    12. timer->start(1000); //check every two seconds
    13. }
    14. }
    15. else {
    16. if (hid_write(device, buf, sizeof(buf)) == -1)
    17. {
    18. CloseDevice();
    19. return;
    20. }
    21. if(hid_read(device, buf, sizeof(buf)) == -1)
    22. {
    23. CloseDevice();
    24. return;
    25. }
    26.  
    27. }
    28. QByteArray usb_data(reinterpret_cast<char*>(buf), sizeof(buf));
    29. hid_comm_update(usb_data, isConnected);
    30. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form)
    2. {
    3.  
    4. ui->setupUi(this);
    5. plugNPlay = new HID_PnP();
    6.  
    7. connect(plugNPlay, SIGNAL(hid_comm_update(QByteArray,bool)), this, SLOT(update_gui(QByteArray,bool)));
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Form::update_gui(QByteArray usb_data, bool isConnected)
    2. {
    3.  
    4. if(isConnected){
    5. std::string message(usb_data.constData(), 60); //usb_data.length()
    6. QString qmessage = QString::fromStdString(message);
    7. QString latitude = qmessage.mid(0,21);
    8. QString longitude = qmessage.mid(23,25);
    9. QString height = qmessage.mid(50);
    10.  
    11. QString lat_mid = qmessage.mid(0,18);
    12. QString lon_mid = qmessage.mid(23,21);
    13.  
    14.  
    15.  
    16.  
    17. double lat = lat_mid.toDouble();
    18. double lon = lon_mid.toDouble();
    19.  
    20. qDebug() << usb_data;
    21.  
    22. ui->textEdit->append("| " + QTime::currentTime().toString("<i>hh:mm:ss</i>") + " | <b>Latitude:</b> "
    23. + latitude + " <b>Longtitude:</b> " +longitude + " <b>Height:</b> " + height);
    To copy to clipboard, switch view to plain text mode 

    I need the lat and lon variables to store a double acquired from a string.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,387
    Thanks
    247
    Thanked 687 Times in 677 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Issue with qDebug() showing only the first character of a QString

    “34003000340035002e003800390032003100”
    If I treat this as 16-bit ASCII (i.e. ignore the 00 characters), this translates to "4045.8921" and it is only 36 bytes long, not 60.

    As wysota said, this looks like 16-bit ASCII, not 8-bit char. std::string is for 8-bit char, std::wstring is for 16-bit wchar. So try replacing std::string in line 5 above with std::wstring, and replace QString::fromStdString() with QString::fromStdWString() in line 6.

  7. #7
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Issue with qDebug() showing only the first character of a QString

    It was indeed 16bit ascii. I used
    Qt Code:
    1. usb_data = QString::fromUtf16(reinterpret_cast<const unsigned short*>(buf), sizeof(buf));
    To copy to clipboard, switch view to plain text mode 
    and now it works!!!

Similar Threads

  1. Problem with QT 4.6 qDebug and QString.
    By weaver4 in forum Newbie
    Replies: 7
    Last Post: 18th March 2013, 12:15
  2. Replies: 3
    Last Post: 16th January 2011, 09:33
  3. Replies: 8
    Last Post: 14th April 2010, 05:54
  4. Get the qDebug() output to QString
    By umulingu in forum Qt Programming
    Replies: 3
    Last Post: 8th March 2010, 01:09
  5. qDebug() 's issue
    By lutins in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2008, 08:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.