0

I've encountered some feature in Qt widgets QTextEdit, QPlainTextEdit, etc that I wouldn't like to encounter. And I actually have no idea how to change this issue.

I want to cipher some text, replacing one unicode character with another unicode character, even if it is control one.

So I delcare a simple string this way: QChar ch = (QChar)0x20; QChar ch2 = (QChar)0xA0; QString str; str.append(ch); str.append(ch2);

These are usual space (0x20) and some unusual No-Break Space (0xA0). In memory the string is absolutely perfect. But if I set it as a text of QTextEdit with ui->txt_ciphered->setPlainText(str); this No-Break Space (0xA0) becomes usual space. I guess that's because they are for some similar purposes, but I still want to get No-Break Space character in TextEdit so that I could copy it etc. like I can in Notepad++ in example.

How could I change this?

  • If you want a general-purpose plaintext editor, you might need to use `QScintilla` or somesuch. `QTextEdit` is not a general-purpose plain text editor. It is, by design, a rich text editor. Qt doesn't really come with a plain text editor :( – Kuba hasn't forgotten Monica Jun 24 '16 at 15:15

1 Answers1

0

QTextEdit does not accept non-break space, why? How can I still use non-break space with Qt edit controls?

The problem is that QTextEdit::plainText property expressively prevents non-break space from being inserted and converts it to just space character.

plainText : QString

This property gets and sets the text editor's contents as plain text. Previous contents are removed and undo/redo history is reset when the property is set.

If the text edit has another content type, it will not be replaced by plain text if you call toPlainText(). The only exception to this is the non-break space, nbsp;, that will be converted into standard space.

Suggestion: use QTextEdit::setText.

If it still does an unwanted conversion or does not treat that symbol right, you need to choose HTML form of test for QTextEdit::setText by inserting the text containing &nbsp; in an envelope of html tags or <html>My&nbsp;text</html>.

Alexander V
  • 8,351
  • 4
  • 38
  • 47
  • Thank you for the answer. Though I still don't get it clearly. Do I need to change this 0xA0 character to   in the html content of the TextEdit? It would be kinda awkward each time. Are there other characters being replaced with more common ones? Not all of control characters are replaced. First 32 (0x0 - 0x1F) are shown correctly, even null. QTextEdit::setText replaces the character too. Maybe there's another TextEdit which does't change anything? Or some options of TextEdit, that would prohibit any changes? – platonshubin Jun 24 '16 at 05:23
  • @platonshubin Platon Shubin, see, I should have tried with my own code. I only remember why it does not work for you as you stated in question - nbsp transformed to space - and what kind of alternative there. Попробуйте, с тем же самым текстом, и если не сработает, то с тегом и &nbsp, – Alexander V Jun 24 '16 at 05:52
  • Well, okay. Я устанавливал текст и с помощью setText() и с setHtml(). Строка QString("My text"). теги он понял и так и так, но пробел всё равно вывел стандартный. (My text). I could and will just avoid using such characters in dictionaries, but for now I am just wondering how to display such symbol like notepad++ does. – platonshubin Jun 24 '16 at 07:35