Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTextEdit and Unicode char no-break space
Forum Updated to NodeBB v4.3 + New Features

QTextEdit and Unicode char no-break space

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Wurgl
    wrote on last edited by
    #1

    When I fill a QTextEdit field with a string containing that character and then retrieve the content with toPlainText() that character is converted to a normal space (0x20)

    Is there any way to prevent this?

    No-break space is the following:
    https://www.fileformat.info/info/unicode/char/00a0/index.htm

    minimal example to reproduce:

    #include <QtCore/qdebug.h>
    #include <QtWidgets/qapplication.h>
    #include <QtWidgets/qtextedit.h>
    
    int main(int argc, char **argv) {
      QApplication app(argc, argv);
      QString str(QChar(160));
      qDebug() << "before" << str[0].unicode();
      QTextEdit txt;
      txt.setPlainText(str);
      str = txt.toPlainText();
      qDebug() << "after" << str[0].unicode();
      return 0;
    }
    
    jsulmJ 1 Reply Last reply
    0
    • W Wurgl

      When I fill a QTextEdit field with a string containing that character and then retrieve the content with toPlainText() that character is converted to a normal space (0x20)

      Is there any way to prevent this?

      No-break space is the following:
      https://www.fileformat.info/info/unicode/char/00a0/index.htm

      minimal example to reproduce:

      #include <QtCore/qdebug.h>
      #include <QtWidgets/qapplication.h>
      #include <QtWidgets/qtextedit.h>
      
      int main(int argc, char **argv) {
        QApplication app(argc, argv);
        QString str(QChar(160));
        qDebug() << "before" << str[0].unicode();
        QTextEdit txt;
        txt.setPlainText(str);
        str = txt.toPlainText();
        qDebug() << "after" << str[0].unicode();
        return 0;
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Wurgl Try

      QString str = "\u00A0";
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Wurgl
        wrote on last edited by
        #3

        @jsulm, this is not the problem! The problem is documented in QTextEdit, where you can read:
        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.

        I found a "hack" (or solution, you name it …) which looks like

                QTextDocument *doc = txt.document();
                QString str;
                for(int i = 0; i < doc->characterCount(); ++ i) {
                    QChar c = doc->characterAt(i);
                    if(c.unicode() == 0x2029) {
                        if(i < doc->characterCount() - 1)
                            str += '\n';
                    }
                    else
                        str += c;
                }
        

        This seems to fix my problem, but I need more testing. I am sure there is still some hidden problem with some other unusual/strange/seldom used characters

        R 1 Reply Last reply
        0
        • W Wurgl

          @jsulm, this is not the problem! The problem is documented in QTextEdit, where you can read:
          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.

          I found a "hack" (or solution, you name it …) which looks like

                  QTextDocument *doc = txt.document();
                  QString str;
                  for(int i = 0; i < doc->characterCount(); ++ i) {
                      QChar c = doc->characterAt(i);
                      if(c.unicode() == 0x2029) {
                          if(i < doc->characterCount() - 1)
                              str += '\n';
                      }
                      else
                          str += c;
                  }
          

          This seems to fix my problem, but I need more testing. I am sure there is still some hidden problem with some other unusual/strange/seldom used characters

          R Offline
          R Offline
          RobinW
          wrote on last edited by
          #4

          @Wurgl I discovered that you can call

          textEdit->document()->toRawText();
          

          This does not replace the non-break space chracter.

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved