我在 QThread 中使用 QNetworkAccessManager 时遇到麻烦。相同的功能在不使用线程的情况下也能正常工作。我目前只使用一个线程,我还需要添加更多线程。
它会触发错误消息:“Object::connect: No such slot QThread::replyFinished(QNetworkrReply*)”
头文件代码(NewThread.h)为:
class NewThread: public QThread
{
public slots:
void replyFinished(QNetworkReply* reply);
protected:
void run();
private:
};
The source code file(NewThread.cpp ):
void NewThread::replyFinished(QNetworkReply *net_reply)
{
QByteArray data = net_reply->readAll();
QString str(data);
}
void NewThread::run()
{
QNetworkAccessManager *manager;
manager = new QNetworkAccessManager ();
QNetworkRequest req;
req.setUrl(QUrl("My url"));
QByteArray postData;
postData.append("some data string");
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
manager->setCookieJar(new QNetworkCookieJar(manager));
//Define the Request-url:
connect (manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinish (QNetworkReply *)));
//Send the request:
manager->post(req, postData);
}
void NewThread::replyFinish(QNetworkReply *reply)
{
QString answer = QString::fromUtf8(reply->readAll());
qDebug () << answer;
}
我正在另一个类中创建一个 NewThread 类的对象,如下所示:
NewThread thread1;
thread1.start();
我想知道为什么相同的代码在没有线程而不是线程的情况下工作。我处境艰难,任何帮助表示赞赏。