对Qt有点新,并尝试向网站发出HTTP GET请求。 问题是它没有发送任何东西。我已经检查了提琴手。没有运动。没有错误,信号没有被激发。
我还检查过我是否有其他可能使用的网络接口,因此我禁用了它们并且没有帮助
以下是来源:
linkslooker::linkslooker(QThread *thread){
this->mainThread = thread;
Loop();
}
void linkslooker::Loop(){
// while(true){
qDebug() << "Looking for sites. Accessability:" << qnam.networkAccessible(); // Always 1
mainThread->msleep(1000); //To slow down the loop
QNetworkRequest req(QUrl("http://www.test.com"));
reply = qnam.get(req); //Get request to a QNetworkReply
if(reply->error() != QNetworkReply::NoError)
qDebug("network error!"); //There are no errors
connect(reply, SIGNAL(finished()),
SLOT(httpFinished())); //Finish request signal
connect(reply, SIGNAL(readyRead()),
this, SLOT(httpReadyRead())); //Read to start signal
connect(reply, SIGNAL(downloadProgress(qint64,qint64)),
this, SLOT(updateDataReadProgress(qint64,qint64))); //Reading Signal
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotError(QNetworkReply::NetworkError))); //Error Signal
// }
}
void linkslooker::httpFinished(){
qDebug() << QString::fromUtf8(m_data);
}
void linkslooker::slotError(QNetworkReply::NetworkError e) {
qDebug() << "slotError" << e ; //Not fired up
}
void linkslooker::httpReadyRead()
{
m_data.append(reply->readAll());
qDebug() << "READY TO READ";
}
void linkslooker::updateDataReadProgress(qint64 bytesRead, qint64 totalBytes)
{
if (httpRequestAborted)
qDebug() << "Network request Aborted";
qDebug() << "Have read " << bytesRead << "Out of " << totalBytes;
}
以下是供参考的标题:
#ifndef LINKSLOOKER_H
#define LINKSLOOKER_H
#include <QCoreApplication>
#include <QQueue>
#include <QtNetwork>
#include <QList>
#include <QNetworkAccessManager>
class linkslooker:public QObject{
Q_OBJECT
public:
linkslooker(QThread *);
~linkslooker();
private:
void Loop();
QThread *mainThread;
QQueue<QString> websites;
QList<QString> searched;
QNetworkAccessManager qnam;
QNetworkReply *reply;
bool httpRequestAborted;
QByteArray m_data;
private slots:
void httpFinished();
void httpReadyRead();
void updateDataReadProgress(qint64 bytesRead, qint64 totalBytes);
void slotError(QNetworkReply::NetworkError);
};
#endif // LINKSLOOKER_H
答案 0 :(得分:0)
发现问题:
在调用这个类的时候,我没有给它一个变量来保持,所以在它附加了所有的信号之后 - 类被破坏了(因为它超出了我认为的范围?)
不得不改变:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
linkslooker(a.thread());
return a.exec();
}
对此:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
linkslooker ll(a.thread());
return a.exec();
}