Data loss while converting from QString to string



  • QString output = ui->textEdit_2->toPlainText();
    string text = output.toUtf8().constData();
    ui->label_4->setText(QString::fromStdString(text));

    result: screenshot



  • QString can handle having an embedded null character (0x00) whereas C strings would treat this a terminator. I can't tell from your screenshot but likely the character where it is truncated is a null byte.

    Probably the data is lost when you construct the std::string as constData() returns a pointer to char. You should use the method toStdString() instead.

    I am not sure if std::string can contain a null character but I suspect it can.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.