1,软件界面如图:
2.代码如下:
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void query(QNetworkReply* reply);
public slots:
void click();
private:
QVBoxLayout* layout;
QLineEdit* link;
QPushButton* button;
QTextEdit* edit;
QNetworkAccessManager* mgr;
};
#include <QtNetwork/QNetworkReply>
#include "widget.h"
Widget::Widget(QWidget *parent):QWidget(parent)
{
layout = new QVBoxLayout;
this->setLayout(layout);
link = new QLineEdit("http://qt.nokia.com");
layout->addWidget(link);
button = new QPushButton("Check");
layout->addWidget(button);
edit = new QTextEdit;
layout->addWidget(edit);
mgr = new QNetworkAccessManager(this);
connect(mgr,SIGNAL(finished(QNetworkReply*)),this, SLOT(query(QNetworkReply*)));
connect(button,SIGNAL(clicked()),this,SLOT(click()));
}
Widget::~Widget()
{
}
void Widget::query(QNetworkReply* reply)
{
QString buffer = reply->readAll();
edit->setText(buffer);
}
void Widget::click()
{
QString uri = link->text();
mgr->get(QNetworkRequest(QUrl(uri)));
}
需要说明的是Qt是使用异步的方式下载网页的
Pro文件
QT += core gui network