Results 1 to 9 of 9

Thread: How to read the contents of the QHTTP file in QString?

  1. #1
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default How to read the contents of the QHTTP file in QString?

    How to read the contents of the QHTTP file in QString? I want to read the contents of the file in http and am using QHTTP for it. How to read it in QString or any other variables.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to read the contents of the QHTTP file in QString?

    Read the data into a QByteArray then, assuming the data is actually text, use either:
    Qt Code:
    1. // QByteArray ba
    2. QString str = QString::fromUtf8(ba.constData(), ba.size());
    3. QString str = QString::fromAscii(ba.constData(), ba.size());
    4. // or one of the other variants
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to read the contents of the QHTTP file in QString?

    How to read the contents from QHTTP in a bytearray?

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to read the contents of the QHTTP file in QString?

    by using a method that returns a QByteArray. You'll find it in the documentation.

    just to mention it the 134281246874th time:

    This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.

  5. #5
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to read the contents of the QHTTP file in QString?

    I am able to write the contents into a file, but unable to write those contents in a variable. Whether it is possible of reading the contents directly to a variable rather than writing to a file and then reading??


    Added after 1 29 minutes:


    Qt Code:
    1. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    2. connect(manager, SIGNAL(finished(QNetworkReply*)),
    3. this, SLOT(replyFinished(QNetworkReply*)));
    4. QNetworkReply *reply= manager->get(QNetworkRequest(QUrl("http://www.oczenterprise.com/support/firmware/MN0MTV660VYQ0BGL/release/12606")));
    5.  
    6. QEventLoop loop;
    7. QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    8. loop.exec();
    9.  
    10. QFile file("C:\\data.dat");
    11.  
    12. file.open(QIODevice::WriteOnly);
    13.  
    14. file.write(reply->readAll());
    15. QByteArray strbytes;
    16. strbytes=reply->readAll();
    17. file.close();
    18.  
    19. QFile ffile("D:\\data.dat");
    20. ffile.open((QIODevice::WriteOnly));
    21. ffile.write(strbytes);
    To copy to clipboard, switch view to plain text mode 

    In this code, strbytes has nothing. I have used
    Qt Code:
    1. QByteArray strbytes;
    2. strbytes=reply->readAll();
    To copy to clipboard, switch view to plain text mode 
    Last edited by Gokulnathvc; 16th June 2011 at 10:56.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to read the contents of the QHTTP file in QString?

    Are you serious?!

    You use the QHttp::readAll() method that returns a QByteArray (you know, a variable in memory) in the code you just posted. It contains nothing because you read the stream when it is already at the end of file, something that has been pointed out to you before. If you cannot work it out from the QHttp documentation, general programming knowledge, your own code, and the numerous other threads on this same problem then I suggest it is time you sat back and reassessed your programmer aspirations.
    Last edited by ChrisW67; 17th June 2011 at 00:24.

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

    Default Re: How to read the contents of the QHTTP file in QString?

    A classic facepalm thread

    facepalm.jpg
    Last edited by wysota; 17th June 2011 at 00:26.
    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.


  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to read the contents of the QHTTP file in QString?

    I was just about to post this:
    doublefacepalm.jpg
    because it needs some emphasis... but you and I are obviously tuned into the same meme

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

    Default Re: How to read the contents of the QHTTP file in QString?

    I had this one bookmarked for the next post but you beat me to it

    Edit: Sorry, just can't stop laughing
    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.


Similar Threads

  1. Read QString from file
    By justin123 in forum Qt Programming
    Replies: 11
    Last Post: 16th March 2011, 22:22
  2. Display contents of .txt file
    By karmo in forum Newbie
    Replies: 17
    Last Post: 23rd January 2010, 20:39
  3. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 13th June 2008, 23:05
  4. Replies: 13
    Last Post: 21st June 2006, 21:22

Tags for this Thread

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.