我正在嘗試使用QNetworkManager編寫應用程序。我簡化了代碼以解決問題。下面的代碼掛了,我不知道爲什麼:Qt4 QNetworkManager掛起
main.cpp中:
#include <QApplication>
#include "post.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
post("http://google.com/search", "q=test");
return app.exec();
}
post.h:
#ifndef _H_POST
#define _H_POST
#include <QNetworkAccessManager>
#include <QNetworkRequest>
class post : public QObject {
Q_OBJECT
public:
post(QString URL, QString data);
public slots:
void postFinished(QNetworkReply* reply);
protected:
QNetworkAccessManager *connection;
};
#endif
post.cpp:
#include <QApplication>
#include <QUrl>
#include "post.h"
post::post(QString URL, QString data) {
connection = new QNetworkAccessManager(this);
connect(connection, SIGNAL(finished(QNetworkReply*)), this, SLOT(postFinished(QNetworkReply*)));
connection->post(QNetworkRequest(QUrl(URL)), data.toAscii());
}
void post::postFinished(QNetworkReply*) {
qApp->exit(0);
}
一些谷歌搜索顯示,這可能是因爲我有一個線程上的所有內容,但我不知道如何改變它在Qt中...沒有任何網絡示例sh這個。
它掛在connect-> post或app.exec中嗎? – 2009-12-24 16:21:08
app.exec 看來post :: postFinished永遠不會被調用 – singpolyma 2009-12-24 16:24:59
您運行的是哪個版本的Qt? – 2009-12-24 16:30:50