Hello again, my signal and slot are not running
#include "loginthread.h"
#include <QtCore>
#include <QtGui>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <loginwait.h>
loginThread::loginThread()
{
}
void loginThread::run()
{
QNetworkAccessManager *login = new QNetworkAccessManager();
QNetworkRequest request;
request.
setUrl(QUrl(server
));
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");
qDebug() << "here!" << "server is : " << server;
QNetworkReply *reply = login->get(request);
// Some testing only
qDebug() << "got request!!!!";
//readdata(reply);
// This won't run!
connect(login, SIGNAL(finished(QNetworkReply*)), this, SLOT(readdata(QNetworkReply*)));
}
void loginThread::readdata(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{
qDebug() << "start to read!!!";
qDebug() << homepage;
qDebug() << "reading done! readall size :" << homepage.size();
reply->deleteLater();
}else
{
qDebug() << "eerror!";
emit networkreplyError
(QString(reply
->errorString
()));
}
}
#include "loginthread.h"
#include <QtCore>
#include <QtGui>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <loginwait.h>
loginThread::loginThread()
{
}
void loginThread::run()
{
QNetworkAccessManager *login = new QNetworkAccessManager();
QNetworkRequest request;
request.setUrl(QUrl(server));
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");
qDebug() << "here!" << "server is : " << server;
QNetworkReply *reply = login->get(request);
// Some testing only
qDebug() << "got request!!!!";
//readdata(reply);
// This won't run!
connect(login, SIGNAL(finished(QNetworkReply*)), this, SLOT(readdata(QNetworkReply*)));
}
void loginThread::readdata(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{
qDebug() << "start to read!!!";
QByteArray homepage = reply->readAll();
qDebug() << homepage;
qDebug() << "reading done! readall size :" << homepage.size();
reply->deleteLater();
}else
{
qDebug() << "eerror!";
emit networkreplyError(QString(reply->errorString()));
}
}
To copy to clipboard, switch view to plain text mode
Header file :
#ifndef LOGINTHREAD_H
#define LOGINTHREAD_H
#include <QtCore>
#include <QtNetwork/QNetworkReply>
{
Q_OBJECT
public:
loginThread();
void run();
private slots:
void readdata(QNetworkReply *reply);
signals:
};
#endif // LOGINTHREAD_H
#ifndef LOGINTHREAD_H
#define LOGINTHREAD_H
#include <QtCore>
#include <QtNetwork/QNetworkReply>
extern QString server;
class loginThread: public QThread
{
Q_OBJECT
public:
loginThread();
void run();
private slots:
void readdata(QNetworkReply *reply);
signals:
void networkreplyError(QString);
};
#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 :
QNetworkRequest *request = new QNetworkRequest();
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
qDebug() << "here!" << "server is : " << server;
//removed this. QNetworkReply *reply =
login->get(*request);
qDebug() << "got request!!!!";
connect(login, SIGNAL(finished(QNetworkReply*)), this, SLOT(readdata(QNetworkReply*)));
qDebug() << "here!" << "server is : " << server;
//removed this. QNetworkReply *reply =
login->get(*request);
qDebug() << "got request!!!!";
connect(login, SIGNAL(finished(QNetworkReply*)), this, SLOT(readdata(QNetworkReply*)));
To copy to clipboard, switch view to plain text mode
or
loginThread *w = new loginThread();
connect(login, SIGNAL(finished(QNetworkReply*)), w, SLOT(readdata(QNetworkReply*)));
loginThread *w = new loginThread();
connect(login, SIGNAL(finished(QNetworkReply*)), w, SLOT(readdata(QNetworkReply*)));
To copy to clipboard, switch view to plain text mode
Bookmarks