简体   繁体   中英

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

In QML,I use TextArea show json data:

`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)
}`

I want TextArea show like :

{ "z1_spindle_speed": { "type": "int", "value": 6000 }, "z2_spindle_speed": { "type": "int", "value": 6000 }, .... } But the result is:

"{\\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 },....

What shold i do?

There is two issues with your assumption it would be printed multi-lined.

1) the standard is to not print pretty. You can add a spacer parameter according to this documentation

initDataStr = JSON.stringify(initDataJson, null, '\t')

2) You are using TextEdit.RichtText which resambles HTML. In HTML newlines, abundant spaces and other whitespace is handled smart, basically meaning it is not printed. You can use the <pre> tag for this:

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

In conclusion, you should have:

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')
}

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