2017-10-04 36 views
0

我有功能,只返回选定的文本从qtextedit。 我需要获取纯文本,但此函数返回带有一些控制字符的文本。qtextedit selectedtext()返回控制字符

例如: 功能textEdit->的TextCursor()selectedText()的返回:

"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()的返回:

"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" 

在第一示例中是(问号)没有进入,我无法取代他们。

我在做什么错?

+0

另外: 第二个例子效果很好。第一个在sqlite中是不可用的,因为有问号 – exo

+0

这很可能是QTextCursor的限制。为什么不使用第二个例子?换行符在sqlite中很好 – Felix

+0

因为我只需要选定的文本。第二个例子返回所有文本。在例子中都是相同的(对于ilustration)...但是在实际情况下,我将只需要几条来自所有的选定行。 – exo

回答

2

为了总结注释了一个答案:

由于QTextCursor::selectedText状态的文档:

注意:如果从编辑获得的选择跨越线断裂, 文本将包含一个Unicode U + 2029段落分隔符 而不是换行符\ n字符。使用QString :: replace()用换行符替换 这些字符。

在调试输出中显示时显示为?。一个既可以使用QString::replace如文档中所述,或使用QTextCursor::selection代替(通过使用selection().toPlainText()获得从选择的文本)

+0

我注意到文档中关于段落的注释。但是在我看来,调试不会将这个字符显示为一个问号:-)。 替换“\ n”字符当然是代码中的第一件事。但它没有导致任何结果。 在这种情况下,不可能替换“\ n”,但我认为char代码U + 2029必须被替换。 – exo

+0

是的,就是这样。你可以从该unicode整数初始化'QChar' – Felix