0

In my Qt GUI app I use libwebrtc. From one on callbacks I want to emit signal with data, which is std::string. As core app logic use QString, I want convert std::string to Qstring. I try following:

QString::fromStdString(stdstr)
QString::fromLatin1(stdstr.data())

Both of this return broken text, something like this

Only working way for me was

QString qstr;
for(uint i =0; i< stdstr.length(); i++)
    qstr.append(stdstr.at(i));

Here my thoughts about reason of problem:

  1. Encoding problems.
  2. Binary problems

About encoding, libwebrtc should return std::string in UTF-8 by default, same as QString.

About binary. As I understand Qt built with GCC and corresponding stdlib, but libwebrtc build with CLang and libc++. For building I also specify usinng QMAKE_CXXFLAGS += -stdlib=libc++

What is correct way to convert types here?

UPD

I compare length of converted string and source string, they are very different.

For std::string I get 64, and for converted QString I get 5.

UPD2

Here is full function and corresponding slot for SPDGen signal

void foo(const webrtc::IceCandidateInterface *candidate) override {
    std::string str;
    candidate->ToString(&str);
    QString qstr = QString::fromStdString(str);
    qDebug() << qstr;
    Q_EMIT SPDGen(qstr);
}

connect(conductor, &Conductor::SPDGen, this, [=](QString value){
    ui->textEdit->setText(value);
});
Heavyrro
  • 11
  • 4
  • It might be an encoding issue. Did you try something like: `QString::fromUtf8(stdstr.data());`? – vahancho Jun 25 '20 at 07:06
  • @vahancho Yes, I try also `QString::fromUtf8`, `QString::fromLatin1`, `QString::fromLocal8Bit`. They return incorrect data too. – Heavyrro Jun 25 '20 at 09:31
  • What's the content of your `stdstr`? Can you bring an example? – vahancho Jun 25 '20 at 09:35
  • @vahancho here is how i get data for this string ` void foo(const webrtc::IceCandidateInterface *candidate) override { std::string str; candidate->ToString(&str); ... } ` And here is actual data in contains: `candidate:3800372419 1 tcp 1518280447 192.168.126.9 46851 typ ho` – Heavyrro Jun 25 '20 at 09:39
  • Please show the complete code that is in your `foo()` function. BTW, your string looks ok, and I even don't see any of non ASCII characters. – vahancho Jun 25 '20 at 09:49
  • @vahancho I update question with example code – Heavyrro Jun 25 '20 at 10:00

0 Answers0