简体   繁体   中英

How to display special characters in QT 5?

Excerpt from my code:

  char msg[80];
  sprintf(msg, "Chuck Temperature: %.1f °C", temperature);
  ui.TemperatureLabel->setText(QString::fromUtf8(msg));

What I get:

What does not work:

  • Replacing the degree symbol (°) by ° while setting textFormat to RichText. It is displayed as " Chuck Temperature: °C ".

  • Adding QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); did not help at all.

I work under Visual C++, if that is important.

What about using a QString instead ? It handles for you the special characters.

If I understand your code, you want to update the text of a QLabel .

You could do something as follows:

QString msg("Chuck Temperature: " + QString::number(temperature) + " °C");
ui.TemperatureLabel->setText(msg);

Note that you can specify the desired precision in QString::number() . Here you can see how to do it.
If you want to format the temperature the same way as %.1f does, just write the following:

QString msg("Chuck Temperature: " + QString::number(temperature, 'g', 2) + " °C");
ui.TemperatureLabel->setText(msg);

I hope it will solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM