Results 1 to 7 of 7

Thread: Need help. can't append chars in QString

  1. #1
    Join Date
    Jun 2008
    Location
    Germany/Belarus/Sweden
    Posts
    53
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Need help. can't append chars in QString

    Hi everyone!,

    I trying to append chars readed from http response which is writen with wininit module into QString. but the QString is stays 0 byte length.
    I can't use QHttp class, because it has problem with POST request under wince.
    http://trolltech.com/developer/task-...5&method=entry

    here is sample of code:
    Qt Code:
    1. QString str;
    2. char szBuffer[4096];
    3. DWORD dwNumberOfBytesRead = 0;
    4. while ( InternetReadFile(hResourceHandle, szBuffer, sizeof(szBuffer),
    5. &dwNumberOfBytesRead) &&
    6. dwNumberOfBytesRead )
    7. {
    8. str.append(szBuffer);
    9.  
    10. }
    11.  
    12. qDebug() << str;
    To copy to clipboard, switch view to plain text mode 

    qDebug prints nothing and str.length() == 0, but dwNumberOfBytesRead > 0

    Thanks for any help!

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Need help. can't append chars in QString

    Quote Originally Posted by AcerExtensa View Post
    Qt Code:
    1. QString str;
    2. char szBuffer[4096];
    3. DWORD dwNumberOfBytesRead = 0;
    4. while ( InternetReadFile(hResourceHandle, szBuffer, sizeof(szBuffer),
    5. &dwNumberOfBytesRead) &&
    6. dwNumberOfBytesRead )
    7. {
    8. str.append(szBuffer);
    9.  
    10. }
    11.  
    12. qDebug() << str;
    To copy to clipboard, switch view to plain text mode 
    You are not using append correctly. QString's append receives "char*" as a paramete,r but not "char".
    Last edited by lyuts; 11th June 2008 at 22:15.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Jun 2008
    Location
    Germany/Belarus/Sweden
    Posts
    53
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need help. can't append chars in QString

    Compiler says
    Converting Parameter 1 from char (*)[256] in QChar inpossible
    i can read data by copying each char into string:
    Qt Code:
    1. for(int i= 0; i<= dwNumberOfBytesRead; i++){
    2. str += QString(szBuffer[i]);
    3. }
    To copy to clipboard, switch view to plain text mode 

    but it is not good solution......

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help. can't append chars in QString

    Quote Originally Posted by AcerExtensa View Post
    I trying to append chars readed from http response which is writen with wininit module into QString. but the QString is stays 0 byte length.
    Weird. Maybe the buffer begins with '\0'? What does "qDebug() << QByteArray( szBuffer, dwNumberOfBytesRead )" output?

    And you shouldn't disregard dwNumberOfBytesRead, unless you like random crashes.


    Quote Originally Posted by lyuts View Post
    You are not using append correctly. QString's append receives "char*" as a paramete,r but not "char".
    But szBuffer is a char *.

  5. #5
    Join Date
    Jun 2008
    Location
    Germany/Belarus/Sweden
    Posts
    53
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need help. can't append chars in QString

    Here what I get with qDebug:
    "y></SOAP-ENV:Envelope>
    "
    and here is original response copyed from Wireshark:
    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:crmlogon" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:crmLoginResponse><return xsi:type="xsd:string">dsfsdfdfsdfsdfdsfsdfdsf</return></ns1:crmLoginResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
    here is the code:
    Qt Code:
    1. DWORD code = sizeof (DWORD), dwBytesWritten = sizeof (code);
    2. ::HttpQueryInfo(hResourceHandle, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &code, &dwBytesWritten, NULL);
    3. qDebug() << "status code:" << code;
    4. DWORD baval = sizeof(DWORD);
    5. ::InternetQueryDataAvailable(hResourceHandle, &baval, NULL, NULL);
    6. qDebug() << "Bytes Available:" << baval;
    7.  
    8. char szBuffer[256];
    9. DWORD dwNumberOfBytesRead = 1;
    10. while(dwNumberOfBytesRead){
    11. if(::InternetReadFile(hResourceHandle, szBuffer, sizeof(szBuffer),&dwNumberOfBytesRead)){
    12. if(dwNumberOfBytesRead == 0) break;
    13. //qDebug() << QByteArray( szBuffer, dwNumberOfBytesRead );
    14. //szBuffer[dwNumberOfBytesRead] = 0;
    15.  
    16.  
    17. qDebug() << "BytesRead" << dwNumberOfBytesRead;
    18. str.append(szBuffer);
    19. /*
    20. for(int i= 0; i<= (int)dwNumberOfBytesRead; i++){
    21. str += QString(szBuffer[i]);
    22. }*/
    23. qDebug() << "current str.length()" << str.length();
    24. }
    25. else{ qDebug() << "error:" << GetLastError(); }
    26. }
    27.  
    28.  
    29. QFile *file = new QFile("log");
    30. file->open(QIODevice::WriteOnly);
    31. bbb.append(str);
    32. file->write(bbb);
    33. file->close();
    34.  
    35. }
    36. else{
    37. qDebug() << "dwErrorCode" << GetLastError();
    38. }
    39. qDebug() << "complete response" << str;
    40. qDebug() << "length" << str.length();
    To copy to clipboard, switch view to plain text mode 

    debug output:
    status code: 200
    Bytes Available: 535
    BytesRead 256
    current str.length() 258
    BytesRead 256
    current str.length() 516
    BytesRead 23
    current str.length() 774
    length 774
    data in file:
    <?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:crmlogon" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC=Hµݢhttp://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:crmLoginResponse><return xsi:type="xsd:string">93b262066b4ec01dcafd26f7fb26 f53e</return></ns1:crmLoginResponse></SOAP-ENV:BodH޹µ></SOAP-ENV:Envelope>
    .org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:crmLoginResponse><return xsi:type="xsd:string">93b262066b4ec01dcafd26f7fb26 f53e</return></ns1:crmLoginResponse></SOAP-ENV:BodH݀µ
    µ - on place µ symbol is symbole like that [] filled with black color, don't know wtf is that

    absolute same result if str is QByteArray.

    any idea people?

  6. #6
    Join Date
    Apr 2006
    Location
    Minsk, Belarus
    Posts
    33
    Thanks
    2
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Need help. can't append chars in QString

    Quote Originally Posted by AcerExtensa View Post
    Hi everyone!,

    I trying to append chars readed from http response which is writen with wininit module into QString. but the QString is stays 0 byte length.
    I can't use QHttp class, because it has problem with POST request under wince.
    http://trolltech.com/developer/task-...5&method=entry

    here is sample of code:
    Qt Code:
    1. QString str;
    2. char szBuffer[4096];
    3. DWORD dwNumberOfBytesRead = 0;
    4. while ( InternetReadFile(hResourceHandle, szBuffer, sizeof(szBuffer),
    5. &dwNumberOfBytesRead) &&
    6. dwNumberOfBytesRead )
    7. {
    8. str.append(szBuffer);
    9.  
    10. }
    11.  
    12. qDebug() << str;
    To copy to clipboard, switch view to plain text mode 

    qDebug prints nothing and str.length() == 0, but dwNumberOfBytesRead > 0

    Thanks for any help!
    Problems that I see:

    1) array of bytes char* is converted into QString without specifying encoding (which is UTF-8 in that case);
    2) since length of data is not provided then char* is read untill first '\0'. In your case it looks like QString::append reads beyond bounds of buffer;
    3) why do you need QString here? You are getting array of bytes so keep them into QByteArray;

    Try this code:

    Qt Code:
    1. QString str;
    2. char szBuffer[4096];
    3. DWORD dwNumberOfBytesRead = 0;
    4. while ( InternetReadFile(hResourceHandle, szBuffer, sizeof(szBuffer),
    5. &dwNumberOfBytesRead) &&
    6. dwNumberOfBytesRead )
    7. {
    8. QByteArray readData(szBuffer, dwNumberOfBytesRead);
    9. ba.append(readData);
    10. }
    11. str = QString::fromUtf8(ba.data(), ba.length());
    12.  
    13. qDebug() << str;
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jun 2008
    Location
    Germany/Belarus/Sweden
    Posts
    53
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Need help. can't append chars in QString

    QByteArray is mutch better for me, I have just played with QBytes and QStrings.

    Thanks! I get now data without any extra symbols, but qDebug don't print any, ba.length() prints 535
    ok, I don't really need to print this on screen or in console.... lets see if I can parse that data without problem....

Similar Threads

  1. easiest Way QString can do
    By baray98 in forum Qt Programming
    Replies: 12
    Last Post: 15th April 2008, 21:49
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59
  3. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 09:55
  4. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 23:10
  5. [SOLVED] Widget plugin ... how to ?
    By yellowmat in forum Newbie
    Replies: 10
    Last Post: 29th January 2006, 21:41

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.