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
Forum Updated to NodeBB v4.3 + New Features

how to display non-printable characters on QTextEdit

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 3.7k Views 1 Watching
  • 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.
  • jsulmJ jsulm

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

    jsulmJ JonBJ 2 Replies Last reply
    0
    • A addebito

      @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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      0
      • A addebito

        @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

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #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
        2
        • jsulmJ jsulm

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

          jsulmJ 1 Reply Last reply
          0
          • A addebito

            @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

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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
            0
            • JonBJ JonB

              @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 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
              • jsulmJ jsulm

                @addebito Is ui->textEdit a valid pointer?

                A Offline
                A Offline
                addebito
                wrote on last edited by
                #13

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

                J.HilkJ 1 Reply Last reply
                0
                • A addebito

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

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on 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
                  0
                  • J.HilkJ J.Hilk

                    @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 last edited by addebito
                    #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 ??

                    jsulmJ 1 Reply Last reply
                    0
                    • A addebito

                      @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 ??

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 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
                      • jsulmJ jsulm

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

                        • Login

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