hello大皮——使用Qt访问Python Web服务 (使用Qt获取网页数据)

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

 

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

 

简单说下应用场景:

一般的智能相机在传输图片时一般使用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请求。

 

欢迎大家留言交流:

 

640?wx_fmt=png

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值