QML,TextArea 显示 json 数据但无法显示 \r\n

QML,TextArea show json data but can't show \r\n

在 QML 中,我使用 TextArea 显示 json 数据:

`TextArea
 {
  id:oldJson
  width: parent.width * 0.4
  height: parent.height
  textFormat: TextEdit.RichText
  text: "<p style='color:red'>"+initDataStr+"</p>"      
//initDataStr is json data like :  initDataStr = JSON.stringify(initDataJson)
}`

我希望 TextArea 显示如下:

{ "z1_spindle_speed": { "type": "int", "value": 6000 }, "z2_spindle_speed": { "type": "int", "value": 6000 }, .... } 但结果是:

"{\r\n \"z1_spindle_speed\": \r\n\t{\r\n\t\t\"type\": \"int\",\r\n \"value\": 6000\r\n },\r\n\t\"z2_spindle_speed\": \r\n\t{\r\n \"type\": \"int\",\r\n \"value\": 6000\r\n },....

我要做什么?

您假设多行打印有两个问题。

1) 标准是打印不漂亮。可以根据这个documentation

加一个spacer参数
initDataStr = JSON.stringify(initDataJson, null, '\t')

2) 您正在使用 TextEdit.RichtText 重播 HTML。在 HTML 换行符中,大量空格和其他空白被巧妙地处理,基本上意味着它不会被打印出来。您可以为此使用 <pre> 标签:

text: "<pre style='color:red'>"+initDataStr+"</pre>"

总之,您应该:

TextArea
{
    id:oldJson
    width: parent.width * 0.4
    height: parent.height
    textFormat: TextEdit.RichText
    text: "<pre style='color:red'>"+initDataStr+"</pre>"      
    //initDataStr is json data like :  initDataStr = JSON.stringify(initDataJson, null, '\t')
}