使用Qt访问Python Web服务 !

作者: 14e61d025165 | 来源:发表于2019-07-22 14:19 被阅读0次

本次主要介绍一种Qt提供的访问 Web URL 的方法—— QNetworkAccessManager 。

平台:ubuntu14.04 Qt Creator 3.5.1 python2.7

简单说下应用场景:Python资源共享群:484031800

一般的智能相机在传输图片时一般使用FTP协议,后来的相机也使用 HTTP 传输图片,在 Qt 中获取 HTTP 传输的图片则可以使用 QNetworkAccessManager 。

程序也比较少,直接贴代码:

include "widget.h"

include "ui_widget.h"

Widget::Widget(QWidget *parent) :

QWidget(parent),

ui(new Ui::Widget)

{

ui->setupUi(this);

//请求python服务数据

mp_clsManager = new QNetworkAccessManager(this);

connect(mp_clsManager,SIGNAL(finished(QNetworkReply*)),

this,SLOT(slot_replyFinished(QNetworkReply*)));

}

Widget::~Widget()

{

delete ui;

}

void Widget::slot_replyFinished(QNetworkReply *reply)

{

//在槽中接收python服务信息

if(reply->error() == QNetworkReply::NoError)

{

QString str(reply->readAll());

qDebug() << "str is :" << str;

}

}

void Widget::on_pbn_sendMsg_clicked()

{

QString postRequest = QString("http://%1:8080/hello?str=你好,大皮")

.arg("localhost");

mp_clsManager->get(QNetworkRequest(QUrl(postRequest)));

}

连接信号和槽,在槽中get请求的的内容。最开始用的时候是 Qt 如何获取网页数据,也就是对应的 HTTP 协议。

点击Sendmsg按钮, post 请求。

相关文章

网友评论

    本文标题:使用Qt访问Python Web服务 !

    本文链接:https://www.haomeiwen.com/subject/xxfwlctx.html