Results 1 to 15 of 15

Thread: QNetworkAccessManager help needed! Class problem...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Angry QNetworkAccessManager help needed! Class problem...

    Hi,

    I'm trying to download a page from a server to my QT program, but I've been searching ways to do it and they don't really work a lot. I'm not an expert in QT, so be kind

    I ear some suggestions under StackOverflow about how to use QNetworkAccessManager. So here it is my codes so far working (almost) correctly.

    http2.cpp
    Qt Code:
    1. #include "http2.h"
    2.  
    3. http2::http2(QObject *parent) :
    4. QObject(parent)
    5. {
    6. m_manager = new QNetworkAccessManager(this);
    7. connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(httpdown(QNetworkReply*)));
    8.  
    9. QNetworkRequest request;
    10. request.setUrl(QUrl("http://localhost/test.php"));
    11. request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");
    12.  
    13. m_manager->get(request);
    14. }
    15.  
    16. void http2::httpdown(QNetworkReply* result)
    17. {
    18.  
    19. QByteArray data= result->readAll();
    20. QString str(data);
    21.  
    22. qDebug() << str;
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    http2.h
    Qt Code:
    1. #ifndef HTTP2_H
    2. #define HTTP2_H
    3.  
    4. #include <QObject>
    5. #include <QDebug>
    6. #include <QtNetwork>
    7. #include <QNetworkReply>
    8.  
    9. class http2 : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit http2(QObject *parent = 0);
    14.  
    15. signals:
    16.  
    17. public slots:
    18. void httpdown(QNetworkReply* result);
    19. private:
    20. QNetworkAccessManager* m_manager;
    21.  
    22. };
    23.  
    24. #endif // HTTP2_H
    To copy to clipboard, switch view to plain text mode 
    Now if I call it directly under main.cpp like this:

    main.cpp
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "tcpserver.h"
    3. #include "http2.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8.  
    9. http2 h; // --> Working!!
    10.  
    11. tcpserver mServer;
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    It works fine. However if I call it inside the tcpserver class like this:

    tcpserver.cpp
    Qt Code:
    1. #include "tcpserver.h"
    2. #include "protocol.h"
    3. #include "http2.h"
    4.  
    5. QTextStream in(stdin);
    6.  
    7. tcpserver::tcpserver(QObject *parent) :
    8. QObject(parent)
    9. {
    10. server = new QTcpServer(this);
    11.  
    12. [ ... Other Server Stuff ... ]
    13.  
    14. http2 h; // --> Not Working :(
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    The signal never happens... http2::httpdown(QNetworkReply* result) is never called, so I don't get the page contents.

    What's wrong? :S

    Thanks.
    Last edited by TCB13; 25th August 2011 at 21:37.

Similar Threads

  1. Replies: 8
    Last Post: 19th April 2011, 07:17
  2. QNetworkAccessManager problem instantiating
    By hojoff79 in forum Qt Programming
    Replies: 8
    Last Post: 9th February 2011, 04:59
  3. Replies: 7
    Last Post: 2nd September 2010, 19:42
  4. qnetworkaccessmanager problem!
    By novamaster in forum Qt Programming
    Replies: 6
    Last Post: 7th August 2010, 11:46
  5. help needed to write a render class
    By franco.amato in forum Qt Programming
    Replies: 9
    Last Post: 1st June 2010, 22:07

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
  •  
Qt is a trademark of The Qt Company.