Results 1 to 2 of 2

Thread: QIODevice dosn't work inside slot

  1. #1
    Join Date
    Jun 2008
    Posts
    9
    Thanks
    9
    Platforms
    Unix/X11 Windows

    Question QIODevice dosn't work inside slot

    Hi!

    I've wrote a class which downloads a xml file from the internet. I made it just like it was in the documentation. Now I want to save downloaded data in the file. But the QIODevice dosn't work in void HttpDownload::replyFinished(QNetworkReply *reply) slot. In constructor and other functions its OK but in slot nothing happens. This is the code:
    I commented what is wrong and what works ok.

    httpdownload.h
    Qt Code:
    1. #ifndef HTTPDOWNLOAD_H
    2. #define HTTPDOWNLOAD_H
    3.  
    4. #include <QObject>
    5. #include <QUrl>
    6.  
    7. class QIODevice;
    8. //class QUrl;
    9. class QNetworkRequest;
    10. class QNetworkAccessManager;
    11. class QNetworkReply;
    12.  
    13.  
    14. class HttpDownload : QObject
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. HttpDownload(const QUrl &downloadUrl, QIODevice *dev);
    20. virtual ~HttpDownload();
    21.  
    22. void asynchronous(const QUrl &url = QUrl());
    23.  
    24. private:
    25.  
    26. inline void write(const char * str) {device->write(str);}
    27.  
    28. const QUrl url;
    29. QIODevice *device;
    30. QNetworkRequest *request;
    31. QNetworkAccessManager *manager;
    32. public slots:
    33. void replyFinished(QNetworkReply* reply);
    34.  
    35. };
    36.  
    37. #endif // HTTPDOWNLOAD_H
    To copy to clipboard, switch view to plain text mode 

    httpdownload.cpp
    Qt Code:
    1. #include "HttpDownload.h"
    2.  
    3. #include <QNetworkAccessManager>
    4. #include <QNetworkRequest>
    5. #include <QIODevice>
    6. #include <QByteArray>
    7. #include <QNetworkReply>
    8. #include <QIODevice>
    9.  
    10. #include <iostream>
    11.  
    12. HttpDownload::HttpDownload(const QUrl &downloadUrl, QIODevice *dev) : url(downloadUrl)
    13. {
    14. device = dev;
    15.  
    16. write("constructor"); // this works fine
    17.  
    18. if( url.isValid()) {
    19. manager = new QNetworkAccessManager(this);
    20. request = new QNetworkRequest(url);
    21. } else {
    22. //throw exception
    23. }
    24.  
    25. connect(manager, SIGNAL(finished(QNetworkReply*)),
    26. this, SLOT(replyFinished(QNetworkReply*)));
    27. }
    28.  
    29. HttpDownload::~HttpDownload() {
    30.  
    31. if(manager != NULL)
    32. delete manager;
    33. if(request != NULL)
    34. delete request;
    35. }
    36.  
    37. void HttpDownload::asynchronous(const QUrl &downloadUrl) {
    38.  
    39. write("asynchronous func"); // this also works
    40.  
    41. if(!downloadUrl.isEmpty()) {
    42. delete request;
    43. request = new QNetworkRequest(downloadUrl);
    44. }
    45.  
    46. manager->get(*request);
    47. }
    48.  
    49. /* slot */
    50. void HttpDownload::replyFinished(QNetworkReply *reply) {
    51.  
    52. QByteArray array;
    53.  
    54. if(! (array = reply->rawHeader(QByteArray("Location"))).isEmpty()) {
    55. this->asynchronous(QUrl(array));
    56. return;
    57. }
    58.  
    59. write(" slotslotslot"); // nothing happens
    60.  
    61. }
    To copy to clipboard, switch view to plain text mode 




    main.cpp

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "HttpDownload.h"
    3.  
    4. #include <QFile>
    5. #include <QUrl>
    6. #include <iostream>
    7. #include <QString>
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QCoreApplication a(argc, argv);
    12.  
    13. QFile *file = new QFile("rssxml.dat");
    14. if (!file->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
    15. return 1;
    16.  
    17. HttpDownload hd(QUrl("http://www.google.pl"), file);
    18. hd.asynchronous();
    19. file->flush();
    20. file->close();
    21.  
    22. return a.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

    Thank for help
    s410i
    Last edited by s410i; 1st August 2009 at 15:24.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QIODevice dosn't work in slot

    Please use the code tag to format your code, not the qtclass tag.

Similar Threads

  1. minimize slot does not work in linux.Is it a Bug in Qt?
    By anupamgee in forum Qt Programming
    Replies: 4
    Last Post: 4th June 2009, 08:18
  2. copy() slot doesn't work
    By Macok in forum Qt Programming
    Replies: 3
    Last Post: 14th February 2009, 10:55

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.