how to display non-printable characters on QTextEdit
-
@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:
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 -
@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
-
@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! ;-) -
@addebito
if thisvoid 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 = ENQQChar c = 1; ui->textEdit->setText(c);
If you try to print these chars on QTextEdit your application doesn't crash ??
-
@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.