qtextedit selectedtext() return 控制字符
qtextedit selectedtext() return control characters
我有一个 return 仅从 qtextedit 中选择文本的功能。
我需要获取纯文本,但是此函数 returns 文本带有一些控制字符。
例如:
函数 textEdit->textCursor().selectedText() return:
"select? timestamp,? strftime('%d.%m.%Y', Datetime(timestamp, 'unixepoch', 'localtime')) as date,? strftime('%H:%M:%S', Datetime(timestamp, 'unixepoch', 'localtime')) as time,? author,? from_dispname,? dialog_partner,? body_xml?from? Messages?where? timestamp >= 1501504199? -- timestamp >= 1502345001?order by? timestamp asc"
函数 textEdit->toPlainText() return:
"select\n timestamp,\n strftime('%d.%m.%Y', Datetime(timestamp, 'unixepoch', 'localtime')) as date,\n strftime('%H:%M:%S', Datetime(timestamp, 'unixepoch', 'localtime')) as time,\n author,\n from_dispname,\n dialog_partner,\n body_xml\nfrom\n Messages\nwhere\n timestamp >= 1501504199\n -- timestamp >= 1502345001\norder by\n timestamp asc"
第一个例子中的 ?(问号) 没有输入,我无法替换它们。
我做错了什么?
将评论总结为答案:
正如 QTextCursor::selectedText
的文档所述:
Note: If the selection obtained from an editor spans a line break, the
text will contain a Unicode U+2029 paragraph separator character
instead of a newline \n character. Use QString::replace() to replace
these characters with newlines.
在调试输出中显示为 ?
。可以按照文档中的说明使用 QString::replace
,也可以改用 QTextCursor::selection
(通过使用 selection().toPlainText()
从选择中获取文本)
我有一个 return 仅从 qtextedit 中选择文本的功能。 我需要获取纯文本,但是此函数 returns 文本带有一些控制字符。
例如: 函数 textEdit->textCursor().selectedText() return:
"select? timestamp,? strftime('%d.%m.%Y', Datetime(timestamp, 'unixepoch', 'localtime')) as date,? strftime('%H:%M:%S', Datetime(timestamp, 'unixepoch', 'localtime')) as time,? author,? from_dispname,? dialog_partner,? body_xml?from? Messages?where? timestamp >= 1501504199? -- timestamp >= 1502345001?order by? timestamp asc"
函数 textEdit->toPlainText() return:
"select\n timestamp,\n strftime('%d.%m.%Y', Datetime(timestamp, 'unixepoch', 'localtime')) as date,\n strftime('%H:%M:%S', Datetime(timestamp, 'unixepoch', 'localtime')) as time,\n author,\n from_dispname,\n dialog_partner,\n body_xml\nfrom\n Messages\nwhere\n timestamp >= 1501504199\n -- timestamp >= 1502345001\norder by\n timestamp asc"
第一个例子中的 ?(问号) 没有输入,我无法替换它们。
我做错了什么?
将评论总结为答案:
正如 QTextCursor::selectedText
的文档所述:
Note: If the selection obtained from an editor spans a line break, the text will contain a Unicode U+2029 paragraph separator character instead of a newline \n character. Use QString::replace() to replace these characters with newlines.
在调试输出中显示为 ?
。可以按照文档中的说明使用 QString::replace
,也可以改用 QTextCursor::selection
(通过使用 selection().toPlainText()
从选择中获取文本)