QThttp程序

#ifndef WIDGET_H

#define WIDGET_H

#include <QWidget>

#include <QtNetwork>

#include <QNetworkAccessManager>

#include <QNetworkReply>

#include <QNetworkRequest>

QT_BEGIN_NAMESPACE

namespace Ui { class Widget; }

QT_END_NAMESPACE

class Widget : public QWidget

{

Q_OBJECT

public:

Widget(QWidget *parent = nullptr);

~Widget();

QNetworkAccessManager *manager;

private:

Ui::Widget *ui;

private slots:

void on_pushButton_clicked();

void GetFinished(QNetworkReply *reply);

};

#endif // WIDGET_H


#include "widget.h"

#include "ui_widget.h"

Widget::Widget(QWidget *parent)

: QWidget(parent)

, ui(new Ui::Widget)

{

ui->setupUi(this);

ui->lineEdit->setText("http://www.baidu.com/");

//建立新对象

manager = new QNetworkAccessManager(this);

//连接槽

connect(manager,&QNetworkAccessManager::finished,this,&Widget::GetFinished);

}

Widget::~Widget()

{

delete ui;

}

void Widget::on_pushButton_clicked()

{

QString url = ui->lineEdit->text();

manager->get(QNetworkRequest(QUrl(url)));

}

void Widget::GetFinished(QNetworkReply *reply)

{

//转换为utf-8编码,这样就可以显示中文了

QTextCodec *codec = QTextCodec::codecForName("utf8");

QString getData = codec->toUnicode(reply->readAll());

ui->textEdit->setText(getData);

}

相关推荐
Quz3 天前
QML Hello World 入门示例
qt
xcyxiner6 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner6 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner7 天前
DicomViewer (添加模型类)3
qt
xcyxiner7 天前
DicomViewer (目录调整) 2
qt
xcyxiner8 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能9 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G10 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
森G10 天前
77、线程池原理和实现------服务器源码解析----云视频服务项目
服务器·c++·qt
森G10 天前
71、打包发布---------打包发布
c++·qt