0

I have Qt5 (5.7) and I'am facing a strange issue. I was unable to transform a std::string (output of a tested algorithm) to QString by following simple methods.

Does anybody have an idea ?

here are simple examples I have tested (none of these lines work...). each time the Visual studio watcher gave me the unknow character for accentuated letters.

std::string l_s = "&é'(-è_çà)=";
QString l = QString::fromUtf8(l_s.data(), l_s.size());
QString lll = QString::fromStdString(l_s);

QByteArray l_ba = QString::fromStdString(l_s).toUtf8();

QString l_t = "&é'(-è_çà)=";
std::string res = l_t.toStdString(); // here I lost information

QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QString ss = codec->toUnicode(l_s.c_str());
2
  • Something like this &Ú'(-Þ_þÓ)=? This solution may work for you: stackoverflow.com/questions/15531105/… Commented Jul 10, 2020 at 16:33
  • Thank you for your reply. I already test this soution without success. The string I have is stored in a std::string. Commented Jul 13, 2020 at 6:44

1 Answer 1

1

This worked for me. In a Linux system. Using Qt 5.6.0:

#include <QString>

#include <iostream>
#include <string>

int main()
{
    std::string str = "&é(-è_çà)=";
    std::cout << "std::string: " << str << std::endl;

    QString qstr = QString::fromStdString(str);
    std::cout << "QString: " << qstr.toStdString() << std::endl;

    return 0;
}

Output:

std::string: &é(-è_çà)=
QString: &é(-è_çà)=
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. It works when the string is directly written in intialization but sometimes does not work when the string comes from the output of an algorithm.
@Joh can you provide a sample of the code using that algorithm?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.