Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how to display non-printable characters on QTextEdit



  • Hi at all,
    I have to print into a QTextEdit some non-printable asci characters like 002 (STX), 003 (ETX), 004 (EOT), etc...

    7edcf869-895e-4b80-9646-f1c2a1115147-immagine.png

    If it's possibile I'd like to emulate Notepad++, like this...

    2f2fc734-f436-4a4c-9aae-7383f53b1493-immagine.png

    ...or simply replace this non-printable chars with the "$" + the integer number of char. As result I'll see "$002"

    I'm "googled" a lot but without finding a solution...
    Do you know how to do that ??

    Thank you


  • Lifetime Qt Champion

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

    or simply replace this non-printable chars with the "$" + the integer number of char

    QString::replace() is your friend



  • 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



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



  • 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.


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

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



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



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


  • Lifetime Qt Champion

    @addebito Is ui->textEdit a valid pointer?



  • @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! ;-)



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


  • Moderators

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



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


  • Lifetime Qt Champion

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



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


Log in to reply