Results 1 to 11 of 11

Thread: QNetworkAccessManager Finished() Error

  1. #1
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QNetworkAccessManager Finished() Error

    Hello again, my signal and slot are not running

    Qt Code:
    1. #include "loginthread.h"
    2. #include <QtCore>
    3. #include <QtGui>
    4. #include <QtNetwork/QNetworkAccessManager>
    5. #include <QtNetwork/QNetworkReply>
    6. #include <QtNetwork/QNetworkRequest>
    7. #include <loginwait.h>
    8.  
    9. loginThread::loginThread()
    10. {
    11. }
    12.  
    13. void loginThread::run()
    14. {
    15. QNetworkAccessManager *login = new QNetworkAccessManager();
    16.  
    17. QNetworkRequest request;
    18. request.setUrl(QUrl(server));
    19. request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
    20.  
    21. qDebug() << "here!" << "server is : " << server;
    22. QNetworkReply *reply = login->get(request);
    23.  
    24. // Some testing only
    25. qDebug() << "got request!!!!";
    26. //readdata(reply);
    27.  
    28. // This won't run!
    29. connect(login, SIGNAL(finished(QNetworkReply*)), this, SLOT(readdata(QNetworkReply*)));
    30. }
    31.  
    32.  
    33. void loginThread::readdata(QNetworkReply *reply)
    34. {
    35. if (reply->error() == QNetworkReply::NoError)
    36. {
    37. qDebug() << "start to read!!!";
    38. QByteArray homepage = reply->readAll();
    39. qDebug() << homepage;
    40. qDebug() << "reading done! readall size :" << homepage.size();
    41. reply->deleteLater();
    42. }else
    43. {
    44. qDebug() << "eerror!";
    45. emit networkreplyError(QString(reply->errorString()));
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

    Header file :
    Qt Code:
    1. #ifndef LOGINTHREAD_H
    2. #define LOGINTHREAD_H
    3. #include <QtCore>
    4. #include <QtNetwork/QNetworkReply>
    5.  
    6. extern QString server;
    7.  
    8. class loginThread: public QThread
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. loginThread();
    14. void run();
    15.  
    16. private slots:
    17. void readdata(QNetworkReply *reply);
    18.  
    19. signals:
    20. void networkreplyError(QString);
    21.  
    22. };
    23.  
    24. #endif // LOGINTHREAD_H
    To copy to clipboard, switch view to plain text mode 
    I did add http:// infront of the url.

    If i run readdata(reply) as shown above for some test, readdata() did run, but it shows size 0.
    But the real thing is the connect() won't execute readdata(), I even tried to do :

    Qt Code:
    1. QNetworkRequest *request = new QNetworkRequest();
    To copy to clipboard, switch view to plain text mode 
    some forums thread said object ended too fast, I did made QNetworkAccessManager like this, so I tried this as well, but still the same.

    for qDebug, it did shows "got request" and if I didn't comment out readdata(), it will also shows "start to read", "reading done, size 0"

    is my problem related with the 'this' ? or something else?
    thx.



    EDIT:

    I tried to change to code to this, still not working, only shows got request
    Qt Code:
    1. qDebug() << "here!" << "server is : " << server;
    2. //removed this. QNetworkReply *reply =
    3. login->get(*request);
    4. qDebug() << "got request!!!!";
    5. connect(login, SIGNAL(finished(QNetworkReply*)), this, SLOT(readdata(QNetworkReply*)));
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. loginThread *w = new loginThread();
    2. connect(login, SIGNAL(finished(QNetworkReply*)), w, SLOT(readdata(QNetworkReply*)));
    To copy to clipboard, switch view to plain text mode 
    Last edited by ttimt; 23rd February 2014 at 17:43.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QNetworkAccessManager Finished() Error

    Try without the separate thread, I don't think you need to launch a thread just to complete a login request.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager Finished() Error

    You forgot to run the thread's event loop in run()

    Qt Code:
    1. void loginThread::run()
    2. {
    3. QNetworkAccessManager *login = new QNetworkAccessManager();
    4.  
    5. QNetworkRequest request;
    6. request.setUrl(QUrl(server));
    7. request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36");
    8.  
    9. qDebug() << "here!" << "server is : " << server;
    10. QNetworkReply *reply = login->get(request);
    11.  
    12. connect(login, SIGNAL(finished(QNetworkReply*)), this, SLOT(readdata(QNetworkReply*)));
    13.  
    14. exec(); // <--- you forgot this
    15. }
    To copy to clipboard, switch view to plain text mode 

    In readdata() you will then want to quit() the thread's event loop so that it finishes and exits run()

    And as stampede said: you probably don't need a thread there anyway, usually only makes things more complicated and less efficient.

    Cheers,
    _

  4. The following user says thank you to anda_skoa for this useful post:

    ttimt (25th February 2014)

  5. #4
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QNetworkAccessManager Finished() Error

    Thanks, it's working. Didn't know about event loop is QThread.

    Also suddenly, I can't do this anymore
    In header file:
    Qt Code:
    1. loginwait *loginwaitw;
    To copy to clipboard, switch view to plain text mode 
    It suddenly shows ISO C++ forbidden, I don't have this problem when using this method before.
    I moved that line of code to cpp file and it's working again.


    Some sites work and outputted source code, but if i enter http://google.com, it only shows
    reading done! readall size : 136191
    but not the source code?
    Last edited by ttimt; 24th February 2014 at 09:08.

  6. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager Finished() Error

    Quote Originally Posted by ttimt View Post
    Thanks, it's working. Didn't know about event loop is QThread.
    Most of Qt's I/O classes are event loop driven to provide, i.e. non-blocking access without the need for threads.
    The classes can of course be used in threads, but then these threads need to run an event loop, just like the main thread does due to QCoreApplication::exec()

    Quote Originally Posted by ttimt View Post
    Also suddenly, I can't do this anymore
    In header file:
    Qt Code:
    1. loginwait *loginwaitw;
    To copy to clipboard, switch view to plain text mode 
    It suddenly shows ISO C++ forbidden, I don't have this problem when using this method before.
    I moved that line of code to cpp file and it's working again.
    Without the exact error message it is hard to guess what is happening, most likely you forgot the forward declaration for the class so it si an unknown type as far as the compiler is concerned.

    Quote Originally Posted by ttimt View Post
    Some sites work and outputted source code, but if i enter http://google.com, it only shows
    but not the source code?
    The content seems to be there, so maybe something is interfering with the debug printing.
    You could try writing it to a file and looking at that with a text editor.

    Cheers,
    _

  7. #6
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QNetworkAccessManager Finished() Error

    The QFile did work. Maybe output is too long I guess.


    Without the exact error message it is hard to guess what is happening, most likely you forgot the forward declaration for the class so it si an unknown type as far as the compiler is concerned.
    error: ISO C++ forbids declaration of 'login' with no type
    error: expected ';' before '*' token

    Qt Code:
    1. #include "login.h"
    2. public:
    3. login *loginw;
    To copy to clipboard, switch view to plain text mode 

    Maybe because the name QT got confused

  8. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager Finished() Error

    From your code snippets above I'd say your class is called loginThread, not login.

    Cheers,
    _

  9. #8
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QNetworkAccessManager Finished() Error

    Here's the thing :

    I got 3 class, login, loginThread and loginwait.
    In loginwait.h, i included loginThread :

    Qt Code:
    1. public:
    2. explicit loginwait(QWidget *parent = 0);
    3. ~loginwait();
    4. loginThread *loginthreadw;
    To copy to clipboard, switch view to plain text mode 

    Compiled and no errors, now again in loginwait.h I add this line just below the last line above, and included the login.h header
    Qt Code:
    1. login *loginw;
    To copy to clipboard, switch view to plain text mode 

    Compile and it shows
    error: ISO C++ forbids declaration of 'login' with no type
    error: expected ';' before '*' token

    In loginwait.cpp :

    Qt Code:
    1. loginthreadw = new loginThread();
    2. loginthreadw->start();
    3. /*and*/
    4. loginw = new login();
    5. loginw->show();
    To copy to clipboard, switch view to plain text mode 
    And so i took the login *loginw; from header file and put it into cpp file just above new login() only it works, no error.




    Also, do I need to manually store and send cookies when I'm using QNetworkAccessManager? Or it's automated.

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager Finished() Error

    Quote Originally Posted by ttimt View Post
    Here's the thing :

    I got 3 class, login, loginThread and loginwait.
    In loginwait.h, i included loginThread :

    Qt Code:
    1. public:
    2. explicit loginwait(QWidget *parent = 0);
    3. ~loginwait();
    4. loginThread *loginthreadw;
    To copy to clipboard, switch view to plain text mode 

    Compiled and no errors, now again in loginwait.h I add this line just below the last line above, and included the login.h header
    Qt Code:
    1. login *loginw;
    To copy to clipboard, switch view to plain text mode 

    Compile and it shows
    error: ISO C++ forbids declaration of 'login' with no type
    error: expected ';' before '*' token

    In loginwait.cpp :

    Qt Code:
    1. loginthreadw = new loginThread();
    2. loginthreadw->start();
    3. /*and*/
    4. loginw = new login();
    5. loginw->show();
    To copy to clipboard, switch view to plain text mode 
    And so i took the login *loginw; from header file and put it into cpp file just above new login() only it works, no error.
    You don't need to include login.h or loginthread.h in loginw.h if you only declare pointers. Forward declarations work as well

    Qt Code:
    1. class login;
    2. class loginThread;
    3.  
    4. class loginw
    5. {
    6. private:
    7. login *loginw;
    8. loginthread *loginthreadw;
    9. };
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by ttimt View Post
    Also, do I need to manually store and send cookies when I'm using QNetworkAccessManager? Or it's automated.
    QNetworkAccessManager uses a QNetworkCookieJar object to handle cookies. The default implementation of that stores cookies only in memory. If you need persistant storage you need to implement it on top of that.
    The documentation of QNetworkCookieJar says : "If you want to save the cookies, you should derive from this class and implement the saving to disk to your own storage format."

    Cheers,
    _

  11. #10
    Join Date
    Nov 2012
    Posts
    47
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QNetworkAccessManager Finished() Error

    How bout sending cookies? If I use same object of QNetworkaccess manager, and, I interact some web and they sent me cookie that I need to send back
    Is it automated? Like sending and setting received cookies etc..



    Others are working fine. Also noticed I forgot to disconnect.
    Gave a thanks
    Last edited by ttimt; 25th February 2014 at 10:55.

  12. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QNetworkAccessManager Finished() Error

    Yes, I think cookie handling is automatic.

    Cheers,
    _

Similar Threads

  1. QNetworkAccessManager no finished() signal emitted
    By realperson in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2018, 09:42
  2. Replies: 4
    Last Post: 17th September 2012, 16:23
  3. QNetworkAccessManager unknown error
    By lipk in forum Newbie
    Replies: 2
    Last Post: 20th March 2011, 14:38
  4. QNetworkAccessManager does not signal finished
    By lukas.zachy in forum Newbie
    Replies: 5
    Last Post: 26th January 2011, 10:05
  5. which one has finished with QFutrueWatcher
    By wookoon in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2010, 13: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.