Не знаю точно можно ли прикрутить PyQT’ешные плагины к С++ проге… поиск в инете толком ничего не дал.
Вот небольшая заметка для программеров, потом может попробую в вики написать.
Для отправки запросов на сервак OSM’а где нужна авторизация (например при изменении данных) использую такую штуку на Qt:
OSMQHttp.hpp
#pragma once
#include <QHttp>
#include <QString>
#include <QEventLoop>
class OSMResponse
{
public:
bool error;
QHttp::Error errorType;
QString errorMessage;
QString response;
OSMResponse()
{
error = false;
errorType = QHttp::NoError;
errorMessage = "";
response = "";
}
};
class OSMQHttp : public QObject
{
Q_OBJECT
public:
OSMQHttp(QString login = "", QString password = "", QString softwareId = "");
virtual ~OSMQHttp();
void SetUser(QString login, QString password);
void SetSoftwareId(QString softwareId);
// send PUT request and wait until finished
OSMResponse sendPut(QString path, QString data);
private:
QHttp *http;
QHttpRequestHeader header;
OSMResponse response;
int requestID; // id of current request
QEventLoop loop; // event loop used to block until request finished
private slots:
void httpRequestFinished(int requestId, bool error);
};
OSMQHttp.cpp
#include "OSMQHttp.hpp"
#include <iostream>
#include <QUrl>
OSMQHttp::OSMQHttp(QString login, QString password, QString softwareId)
{
http = new QHttp();
requestID = -1;
http->setUser(login, password);
connect(http, SIGNAL(requestFinished(int, bool)), SLOT(httpRequestFinished(int, bool)));
header.setValue("Content-Type", "text/xml");
header.setValue("User-Agent", softwareId);
header.setValue("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
header.setValue("Connection", "keep-alive");
}
OSMQHttp::~OSMQHttp() { }
void OSMQHttp::SetUser(QString login, QString password)
{
http->setUser(login, password);
}
void OSMQHttp::SetSoftwareId(QString softwareId)
{
header.setValue("User-Agent", softwareId);
}
OSMResponse OSMQHttp::sendPut(QString path, QString content)
{
QByteArray data = content.toAscii();
QUrl url(path);
QHttp::ConnectionMode mode = url.scheme().toLower() == "https" ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
http->setHost(url.host(), mode, url.port() == -1 ? 0 : url.port());
header.setRequest("PUT", url.toEncoded(QUrl::RemoveScheme | QUrl::RemoveAuthority));
header.setValue("Host", url.host());
header.setValue("Content-Length", QString::number(data.size()));
requestID = http->request(header, data);
/// block until the request is finished
loop.exec();
return response;
}
void OSMQHttp::httpRequestFinished(int requestId, bool error)
{
/// check to see if it’s the request we made
if(requestId != requestID) return;
OSMResponse response;
response.error = error;
if(error){
response.errorMessage = http->errorString();
response.errorType = http->error();
//std::cout << "Error: " << http->errorString().toStdString().data() << " " << requestId << std::endl;
}else{
QByteArray data = http->readAll();
response.response = QString(data);
//std::cout << "Response: " << QString(data).toStdString().data() << std::endl;
}
this->response = response;
/// end the loop
loop.exit();
}
Испльзовать можно так:
#include <stdlib.h>
#include <QApplication>
#include "OSMQHttp.hpp"
int main(int argc, char *argv[])
{
setlocale(LC_ALL, "Russian");
QApplication qapp(argc, argv);
QString login("MyLogin");
QString password("MyPass");
QString url("http://api.openstreetmap.org/api/0.6/changeset/create");
QString content("<?xml version='1.0' encoding='UTF-8'?>\n<osm version='0.6' generator='JOSM'>\n<changeset id='0' open='false'>\n<tag k='created_by' v='MyNewEditor' />\n<tag k='comment' v='editor test' />\n</changeset>\n</osm>");
OSMQHttp osmhttp;
osmhttp.SetUser(login, password);
osmhttp.SetSoftwareId("MyNewEditor");
OSMResponse response = osmhttp.sendPut(url, content);
std::cout << "OSMResponse: " << response.response.toStdString().data() << std::endl;
return qapp.exec();
}