我只是想做一个非常简单的qt关于网络的测试,读取一个网页内容作为请求,解析出其中信息,代码附上:
myHTTP.h
#pragma once
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QtWidgets/QMainWindow>
#include "ui_myHTTP.h"
class myHTTP : public QMainWindow
{
Q_OBJECT
public:
myHTTP(QWidget *parent = Q_NULLPTR);
private:
Ui::myHTTPClass ui;
QNetworkAccessManager *manager;
public slots:
void slotFinished(QNetworkReply* reply);
};
myHTTP.cpp
#include "myHTTP.h"
#include <QUrl>
myHTTP::myHTTP(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
manager = new QNetworkAccessManager(this);
bool b = connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotFinished((QNetworkReply*))));
/*此处b总是为false*/
manager->get(QNetworkRequest(QUrl("http://www.baidu.com"))); // 开启一个以百度首页的网络请求
}
void myHTTP::slotFinished(QNetworkReply* reply)
{
QString allData = reply->readAll();
ui.textBrowser->setText(allData);
// 删除网络回复资源
reply->deleteLater();
}
}