c++qtutf-8qt5qnetworkreply

Qt5 no polish characters after reading from QNetworkReply


I need to get data from this url https://www.nbp.pl/kursy/xml/lasta.xml. The problem is that response contains broken polish characters (????? instead of śćąęó).

My console and GUI is fully capable of showing polish characters and only this one content string is broken.

I have no idea what to do with this. I'm using CLion on Linux.

#include "DataDownloader.h"
#include <QtWidgets/QMessageBox>

std::string DataDownloader::downloadData() const
{
    QNetworkAccessManager manager;
    QNetworkReply *response = manager.get(QNetworkRequest(QUrl(this->url)));
    QEventLoop event;
    connect(response, SIGNAL(finished()), &event, SLOT(quit()));
    event.exec();

    std::string content = QString(response->readAll()).toStdString();

    return content;
}

Edit: Actually other sites that contain polish signs work just fine. But still, I have to make it work with the one I provided above

Edit 2: Seems like this xml is encoded in ISO-8859-2. This explains the issue but I still don't know how to deal with it.


Solution

  • This works.

    std::string DataDownloader::downloadData() const
    {
        QNetworkAccessManager manager;
        QNetworkReply *response = manager.get(QNetworkRequest(QUrl(this->url)));
        QEventLoop event;
        connect(response, SIGNAL(finished()), &event, SLOT(quit()));
        event.exec();
    
        QTextCodec* codec = QTextCodec::codecForName("ISO-8859-2");
        QTextDecoder decoder{codec};
    
        return decoder.toUnicode(response->readAll()).toStdString();
    }