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. how to display non-printable characters on QTextEdit
QtWS25 Last Chance

how to display non-printable characters on QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 3.3k 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.
  • A addebito
    31 Mar 2020, 20:23

    Oh my god.... It was so easy.
    Ok thank you @Christian-Ehrlicher, now I can replace the character but I don't know which characters incoming from... so, instead a "for" cycle on all ASCII table there isn't a better way?
    Thank you so much

    J Online
    J Online
    JonB
    wrote on 31 Mar 2020, 20:29 last edited by JonB
    #4

    @addebito
    You won't need a for loop to search the ASCII table, just use the value of the character (counts from 0, up to 31) as the index into a table holding the abbreviation string to output.

    A 1 Reply Last reply 1 Apr 2020, 00:26
    3
    • J JonB
      31 Mar 2020, 20:29

      @addebito
      You won't need a for loop to search the ASCII table, just use the value of the character (counts from 0, up to 31) as the index into a table holding the abbreviation string to output.

      A Offline
      A Offline
      addebito
      wrote on 1 Apr 2020, 00:26 last edited by
      #5

      Yes of course @JonB, I can check the value (int) of the char but I don't have only one char to test...
      So I'll have 2 situations...

      1. If I'll use the QString.replace() I have to iterate from 0 to 31 on every string
      2. If I'll check the integer value of the char I have to iterate on every chars on every string
        Am I missing somethins?

      I assume that this can be the solution and that it works, I'm looking for a better solution (...if there is) completely different, like this.

      void MainWindow::on_pushButton_clicked() 
      {
        QChar c = 9;
        // ui->textEdit->setText(c);
        QTextDocument *doc = new QTextDocument;
        doc->setPlainText(QString(c) + "this is a tab");
        QTextOption opt;
        opt.setFlags(QTextOption::ShowTabsAndSpaces);
        doc->setDefaultTextOption(opt);
        ui->textEdit->setDocument(doc);
      }
      

      but also in this case, if I replace the TAB char 9 with STX char 2 the application crashes.

      As always, thank you for any suggestions.

      J 1 Reply Last reply 1 Apr 2020, 05:06
      0
      • A addebito
        1 Apr 2020, 00:26

        Yes of course @JonB, I can check the value (int) of the char but I don't have only one char to test...
        So I'll have 2 situations...

        1. If I'll use the QString.replace() I have to iterate from 0 to 31 on every string
        2. If I'll check the integer value of the char I have to iterate on every chars on every string
          Am I missing somethins?

        I assume that this can be the solution and that it works, I'm looking for a better solution (...if there is) completely different, like this.

        void MainWindow::on_pushButton_clicked() 
        {
          QChar c = 9;
          // ui->textEdit->setText(c);
          QTextDocument *doc = new QTextDocument;
          doc->setPlainText(QString(c) + "this is a tab");
          QTextOption opt;
          opt.setFlags(QTextOption::ShowTabsAndSpaces);
          doc->setDefaultTextOption(opt);
          ui->textEdit->setDocument(doc);
        }
        

        but also in this case, if I replace the TAB char 9 with STX char 2 the application crashes.

        As always, thank you for any suggestions.

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 1 Apr 2020, 05:06 last edited by
        #6

        @addebito

        "but also in this case, if I replace the TAB char 9 with STX char 2 the application crashes" - please show your code. And did you try to debug to see what happens and to get stack trace?

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

        A 1 Reply Last reply 1 Apr 2020, 07:53
        0
        • J jsulm
          1 Apr 2020, 05:06

          @addebito

          "but also in this case, if I replace the TAB char 9 with STX char 2 the application crashes" - please show your code. And did you try to debug to see what happens and to get stack trace?

          A Offline
          A Offline
          addebito
          wrote on 1 Apr 2020, 07:53 last edited by
          #7

          @jsulm the application is really simple. A c++ widget application with a QPushButton and a QTextEdit.

          Demo_QTextEDit.pro

          QT       += core gui
          
          greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
          
          CONFIG += c++11
          
          # The following define makes your compiler emit warnings if you use
          # any Qt feature that has been marked deprecated (the exact warnings
          # depend on your compiler). Please consult the documentation of the
          # deprecated API in order to know how to port your code away from it.
          DEFINES += QT_DEPRECATED_WARNINGS
          
          # You can also make your code fail to compile if it uses deprecated APIs.
          # In order to do so, uncomment the following line.
          # You can also select to disable deprecated APIs only up to a certain version of Qt.
          #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
          
          SOURCES += \
              main.cpp \
              mainwindow.cpp
          
          HEADERS += \
              mainwindow.h
          
          FORMS += \
              mainwindow.ui
          
          # Default rules for deployment.
          qnx: target.path = /tmp/$${TARGET}/bin
          else: unix:!android: target.path = /opt/$${TARGET}/bin
          !isEmpty(target.path): INSTALLS += target
          

          mainwindow.h

          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          
          QT_BEGIN_NAMESPACE
          namespace Ui {
          class MainWindow;
          }
          QT_END_NAMESPACE
          
          class MainWindow : public QMainWindow {
            Q_OBJECT
          
          public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
          
          private slots:
            void on_pushButton_clicked();
          
          private:
            Ui::MainWindow *ui;
          };
          #endif // MAINWINDOW_H
          

          mainwindow.cpp

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent), ui(new Ui::MainWindow) {
            ui->setupUi(this);
          }
          
          MainWindow::~MainWindow() { delete ui; }
          
          void MainWindow::on_pushButton_clicked() {
            QChar c = 2;
            // ui->textEdit->setText(c);
            QTextDocument *doc = new QTextDocument;
            doc->setPlainText(QString(c) + "this is a tab");
            QTextOption opt;
            opt.setFlags(QTextOption::ShowTabsAndSpaces);
            doc->setDefaultTextOption(opt);
            ui->textEdit->setDocument(doc);
          }
          

          ...and when I run the application I get this error:

          31b25123-aff3-4deb-b4e1-c5f121677174-immagine.png

          Sorry @jsulm but I'm pretty new in Qt, in this case how to proceed to understand the problem?
          Qt 5.12.7 with compiler VS2017 on Windows 10
          Thank you for your time

          J J 2 Replies Last reply 1 Apr 2020, 07:58
          0
          • A addebito
            1 Apr 2020, 07:53

            @jsulm the application is really simple. A c++ widget application with a QPushButton and a QTextEdit.

            Demo_QTextEDit.pro

            QT       += core gui
            
            greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
            
            CONFIG += c++11
            
            # The following define makes your compiler emit warnings if you use
            # any Qt feature that has been marked deprecated (the exact warnings
            # depend on your compiler). Please consult the documentation of the
            # deprecated API in order to know how to port your code away from it.
            DEFINES += QT_DEPRECATED_WARNINGS
            
            # You can also make your code fail to compile if it uses deprecated APIs.
            # In order to do so, uncomment the following line.
            # You can also select to disable deprecated APIs only up to a certain version of Qt.
            #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
            
            SOURCES += \
                main.cpp \
                mainwindow.cpp
            
            HEADERS += \
                mainwindow.h
            
            FORMS += \
                mainwindow.ui
            
            # Default rules for deployment.
            qnx: target.path = /tmp/$${TARGET}/bin
            else: unix:!android: target.path = /opt/$${TARGET}/bin
            !isEmpty(target.path): INSTALLS += target
            

            mainwindow.h

            #ifndef MAINWINDOW_H
            #define MAINWINDOW_H
            
            #include <QMainWindow>
            
            QT_BEGIN_NAMESPACE
            namespace Ui {
            class MainWindow;
            }
            QT_END_NAMESPACE
            
            class MainWindow : public QMainWindow {
              Q_OBJECT
            
            public:
              MainWindow(QWidget *parent = nullptr);
              ~MainWindow();
            
            private slots:
              void on_pushButton_clicked();
            
            private:
              Ui::MainWindow *ui;
            };
            #endif // MAINWINDOW_H
            

            mainwindow.cpp

            #include "mainwindow.h"
            #include "ui_mainwindow.h"
            
            MainWindow::MainWindow(QWidget *parent)
                : QMainWindow(parent), ui(new Ui::MainWindow) {
              ui->setupUi(this);
            }
            
            MainWindow::~MainWindow() { delete ui; }
            
            void MainWindow::on_pushButton_clicked() {
              QChar c = 2;
              // ui->textEdit->setText(c);
              QTextDocument *doc = new QTextDocument;
              doc->setPlainText(QString(c) + "this is a tab");
              QTextOption opt;
              opt.setFlags(QTextOption::ShowTabsAndSpaces);
              doc->setDefaultTextOption(opt);
              ui->textEdit->setDocument(doc);
            }
            

            ...and when I run the application I get this error:

            31b25123-aff3-4deb-b4e1-c5f121677174-immagine.png

            Sorry @jsulm but I'm pretty new in Qt, in this case how to proceed to understand the problem?
            Qt 5.12.7 with compiler VS2017 on Windows 10
            Thank you for your time

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 1 Apr 2020, 07:58 last edited by
            #8

            @addebito When exactly does it crash? When on_pushButton_clicked() is called? If so please set a break point at the first line in on_pushButton_clicked() and run through debugger, when debugger stops inside on_pushButton_clicked() execute line by line and see in which line it crashes.

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

            A 1 Reply Last reply 1 Apr 2020, 08:59
            0
            • A addebito
              1 Apr 2020, 07:53

              @jsulm the application is really simple. A c++ widget application with a QPushButton and a QTextEdit.

              Demo_QTextEDit.pro

              QT       += core gui
              
              greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
              
              CONFIG += c++11
              
              # The following define makes your compiler emit warnings if you use
              # any Qt feature that has been marked deprecated (the exact warnings
              # depend on your compiler). Please consult the documentation of the
              # deprecated API in order to know how to port your code away from it.
              DEFINES += QT_DEPRECATED_WARNINGS
              
              # You can also make your code fail to compile if it uses deprecated APIs.
              # In order to do so, uncomment the following line.
              # You can also select to disable deprecated APIs only up to a certain version of Qt.
              #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
              
              SOURCES += \
                  main.cpp \
                  mainwindow.cpp
              
              HEADERS += \
                  mainwindow.h
              
              FORMS += \
                  mainwindow.ui
              
              # Default rules for deployment.
              qnx: target.path = /tmp/$${TARGET}/bin
              else: unix:!android: target.path = /opt/$${TARGET}/bin
              !isEmpty(target.path): INSTALLS += target
              

              mainwindow.h

              #ifndef MAINWINDOW_H
              #define MAINWINDOW_H
              
              #include <QMainWindow>
              
              QT_BEGIN_NAMESPACE
              namespace Ui {
              class MainWindow;
              }
              QT_END_NAMESPACE
              
              class MainWindow : public QMainWindow {
                Q_OBJECT
              
              public:
                MainWindow(QWidget *parent = nullptr);
                ~MainWindow();
              
              private slots:
                void on_pushButton_clicked();
              
              private:
                Ui::MainWindow *ui;
              };
              #endif // MAINWINDOW_H
              

              mainwindow.cpp

              #include "mainwindow.h"
              #include "ui_mainwindow.h"
              
              MainWindow::MainWindow(QWidget *parent)
                  : QMainWindow(parent), ui(new Ui::MainWindow) {
                ui->setupUi(this);
              }
              
              MainWindow::~MainWindow() { delete ui; }
              
              void MainWindow::on_pushButton_clicked() {
                QChar c = 2;
                // ui->textEdit->setText(c);
                QTextDocument *doc = new QTextDocument;
                doc->setPlainText(QString(c) + "this is a tab");
                QTextOption opt;
                opt.setFlags(QTextOption::ShowTabsAndSpaces);
                doc->setDefaultTextOption(opt);
                ui->textEdit->setDocument(doc);
              }
              

              ...and when I run the application I get this error:

              31b25123-aff3-4deb-b4e1-c5f121677174-immagine.png

              Sorry @jsulm but I'm pretty new in Qt, in this case how to proceed to understand the problem?
              Qt 5.12.7 with compiler VS2017 on Windows 10
              Thank you for your time

              J Online
              J Online
              JonB
              wrote on 1 Apr 2020, 08:22 last edited by JonB 4 Jan 2020, 08:24
              #9

              @addebito
              Apart from answering @jsulm about the "crash", which you should do.

              I'm not sure what it is you are trying to avoid iterating or expect to improve speed on. Your algorithm will be:

              for each char in input string
                  if 0 <= char <= 31
                      copy the 3-character "name" for char from table[indexed directly by char value] to new string
                  else
                      copy the original char unchanged to new string
              

              You're not getting to get anything more much more efficient than that.

              A 1 Reply Last reply 1 Apr 2020, 09:02
              2
              • J jsulm
                1 Apr 2020, 07:58

                @addebito When exactly does it crash? When on_pushButton_clicked() is called? If so please set a break point at the first line in on_pushButton_clicked() and run through debugger, when debugger stops inside on_pushButton_clicked() execute line by line and see in which line it crashes.

                A Offline
                A Offline
                addebito
                wrote on 1 Apr 2020, 08:59 last edited by
                #10

                @jsulm it crashes when the code reach this line

                ui->textEdit->setDocument(doc);
                

                ...or if I uncomment the second line, that was the first version without QTextDocument ...

                ui->textEdit->setText(c);
                

                ...so, in both case, when I put the text into QTextEdit

                J 1 Reply Last reply 1 Apr 2020, 09:02
                0
                • A addebito
                  1 Apr 2020, 08:59

                  @jsulm it crashes when the code reach this line

                  ui->textEdit->setDocument(doc);
                  

                  ...or if I uncomment the second line, that was the first version without QTextDocument ...

                  ui->textEdit->setText(c);
                  

                  ...so, in both case, when I put the text into QTextEdit

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 1 Apr 2020, 09:02 last edited by
                  #11

                  @addebito Is ui->textEdit a valid pointer?

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

                  A 1 Reply Last reply 1 Apr 2020, 09:03
                  0
                  • J JonB
                    1 Apr 2020, 08:22

                    @addebito
                    Apart from answering @jsulm about the "crash", which you should do.

                    I'm not sure what it is you are trying to avoid iterating or expect to improve speed on. Your algorithm will be:

                    for each char in input string
                        if 0 <= char <= 31
                            copy the 3-character "name" for char from table[indexed directly by char value] to new string
                        else
                            copy the original char unchanged to new string
                    

                    You're not getting to get anything more much more efficient than that.

                    A Offline
                    A Offline
                    addebito
                    wrote on 1 Apr 2020, 09:02 last edited by
                    #12

                    @JonB said in how to display non-printable characters on QTextEdit:

                    I'm not sure what it is you are trying to avoid iterating or expect to improve speed on. Your algorithm will be:

                    You're not getting to get anything more much more efficient than that.

                    Thank you @JonB.
                    My goal is only to explorer, as always when I have a little time to investigate, a better solution.
                    Never give up! ;-)

                    1 Reply Last reply
                    0
                    • J jsulm
                      1 Apr 2020, 09:02

                      @addebito Is ui->textEdit a valid pointer?

                      A Offline
                      A Offline
                      addebito
                      wrote on 1 Apr 2020, 09:03 last edited by
                      #13

                      @jsulm Yes of course!
                      If I print the TAB char or anything else it works fine.

                      J 1 Reply Last reply 1 Apr 2020, 09:06
                      0
                      • A addebito
                        1 Apr 2020, 09:03

                        @jsulm Yes of course!
                        If I print the TAB char or anything else it works fine.

                        J Offline
                        J Offline
                        J.Hilk
                        Moderators
                        wrote on 1 Apr 2020, 09:06 last edited by
                        #14

                        @addebito
                        if this

                        void MainWindow::on_pushButton_clicked() {
                          QChar c = 2;
                          // ui->textEdit->setText(c);
                          QTextDocument *doc = new QTextDocument;
                          doc->setPlainText(QString(c) + "this is a tab");
                          QTextOption opt;
                          opt.setFlags(QTextOption::ShowTabsAndSpaces);
                          doc->setDefaultTextOption(opt);
                          ui->textEdit->setDocument(doc);
                        }
                        

                        is your function, then where is your replace operation ?


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        A 1 Reply Last reply 1 Apr 2020, 09:13
                        0
                        • J J.Hilk
                          1 Apr 2020, 09:06

                          @addebito
                          if this

                          void MainWindow::on_pushButton_clicked() {
                            QChar c = 2;
                            // ui->textEdit->setText(c);
                            QTextDocument *doc = new QTextDocument;
                            doc->setPlainText(QString(c) + "this is a tab");
                            QTextOption opt;
                            opt.setFlags(QTextOption::ShowTabsAndSpaces);
                            doc->setDefaultTextOption(opt);
                            ui->textEdit->setDocument(doc);
                          }
                          

                          is your function, then where is your replace operation ?

                          A Offline
                          A Offline
                          addebito
                          wrote on 1 Apr 2020, 09:13 last edited by addebito 4 Jan 2020, 09:17
                          #15

                          @J-Hilk this is only a demo to reproduce the same error, when the software has to print on QTextEdit some non printable chars, it crashes.

                          I've tried these chars
                          1 = SOH
                          2 = STX
                          3 = ETX
                          4 = EOT
                          5 = ENQ

                            QChar c = 1;
                            ui->textEdit->setText(c);
                          

                          If you try to print these chars on QTextEdit your application doesn't crash ??

                          J 1 Reply Last reply 1 Apr 2020, 09:22
                          0
                          • A addebito
                            1 Apr 2020, 09:13

                            @J-Hilk this is only a demo to reproduce the same error, when the software has to print on QTextEdit some non printable chars, it crashes.

                            I've tried these chars
                            1 = SOH
                            2 = STX
                            3 = ETX
                            4 = EOT
                            5 = ENQ

                              QChar c = 1;
                              ui->textEdit->setText(c);
                            

                            If you try to print these chars on QTextEdit your application doesn't crash ??

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 1 Apr 2020, 09:22 last edited by
                            #16

                            @addebito said in how to display non-printable characters on QTextEdit:

                            If you try to print these chars on QTextEdit your application doesn't crash ??

                            I don't understand: didn't you want to convert these chars to something printable?

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

                            A 1 Reply Last reply 1 Apr 2020, 09:44
                            1
                            • J jsulm
                              1 Apr 2020, 09:22

                              @addebito said in how to display non-printable characters on QTextEdit:

                              If you try to print these chars on QTextEdit your application doesn't crash ??

                              I don't understand: didn't you want to convert these chars to something printable?

                              A Offline
                              A Offline
                              addebito
                              wrote on 1 Apr 2020, 09:44 last edited by
                              #17

                              @jsulm said in how to display non-printable characters on QTextEdit:

                              I don't understand: didn't you want to convert these chars to something printable?

                              Yes @jsulm and I'm looking for a different solution instead a for cycle iteration, but the discussion is getting longer than expected, so I'll adopt this one, I'll check every single char.

                              Thank you so much for all the advice and for your time.

                              1 Reply Last reply
                              0

                              13/17

                              1 Apr 2020, 09:03

                              • Login

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