How to visualize LF, CR and other control pictures in QTextEdit?
-
Did you already saw the QTextOption enum ?
It seems to set what you want with caveat that the font used needs to support the stuff you want to show ?
I just stumbled upon it while remembering there was something somewhere to at least show the line return. -
Did you already saw the QTextOption enum ?
It seems to set what you want with caveat that the font used needs to support the stuff you want to show ?
I just stumbled upon it while remembering there was something somewhere to at least show the line return.@SGaist Thanks again!
I just did a little test ... here is the outcome. Unfortunately, the display shows
CR
andLF
exactly the same way:
It was a good idea, though! -
@SGaist Thanks again!
I just did a little test ... here is the outcome. Unfortunately, the display shows
CR
andLF
exactly the same way:
It was a good idea, though!@Robert-Hairgrove
But does your#define
line literally have the content shown in the visual, i.e. something like#define SAMPLE_TEXT \ "The quick brown fox\r\r\nA line ..."
i.e. you typed in a literal string including C++
\r\n
"escape" sequences, or do you mean that the macro actually contains physical CR/LF characters in those positions? These are not the same as each other...!Also while it is here, if you are intending to paste in C++ code I do not think
QTextEdit
in rich text/HTML mode is "safe" to get the correct content/output reliably. You should be usingQPlainTextEdit
, or maybeQTextEdit
but forced to be in text/non-HTML mode. -
@Robert-Hairgrove
But does your#define
line literally have the content shown in the visual, i.e. something like#define SAMPLE_TEXT \ "The quick brown fox\r\r\nA line ..."
i.e. you typed in a literal string including C++
\r\n
"escape" sequences, or do you mean that the macro actually contains physical CR/LF characters in those positions? These are not the same as each other...!Also while it is here, if you are intending to paste in C++ code I do not think
QTextEdit
in rich text/HTML mode is "safe" to get the correct content/output reliably. You should be usingQPlainTextEdit
, or maybeQTextEdit
but forced to be in text/non-HTML mode.@JonB I don't understand the question ... in C++, you must escape any control codes, otherwise the compiler would complain.
The macro is like this (copied and pasted from my code):
#define SAMPLE_TEXT \ "The quick brown fox\r\r\nA line with\tTAB...\nSome more text here"
Then, in the constructor of the main window, I set the text like this:
QString x = QString::fromLocal8Bit(SAMPLE_TEXT); ui->textEdit->setText(x);
As to
QPlainTextEdit
vs.QTextEdit
, I don't know what I will eventually use. To achieve my goal, it looks like I will end up using inline pixmaps instead of actual characters, which would imply using HTML or Markdown text. The other alternative would be to create my own font, which I really don't want to do. -
@JonB I don't understand the question ... in C++, you must escape any control codes, otherwise the compiler would complain.
The macro is like this (copied and pasted from my code):
#define SAMPLE_TEXT \ "The quick brown fox\r\r\nA line with\tTAB...\nSome more text here"
Then, in the constructor of the main window, I set the text like this:
QString x = QString::fromLocal8Bit(SAMPLE_TEXT); ui->textEdit->setText(x);
As to
QPlainTextEdit
vs.QTextEdit
, I don't know what I will eventually use. To achieve my goal, it looks like I will end up using inline pixmaps instead of actual characters, which would imply using HTML or Markdown text. The other alternative would be to create my own font, which I really don't want to do.@Robert-Hairgrove
If you paste arbitrary C++ code into aQTextEdit
in HTML mode, how do you know whether there might be character sequences which would be interpreted as HTML rather than literal, e.g. if the code happens to have<pre>
or&
or similar in it? -
@Robert-Hairgrove
If you paste arbitrary C++ code into aQTextEdit
in HTML mode, how do you know whether there might be character sequences which would be interpreted as HTML rather than literal, e.g. if the code happens to have<pre>
or&
or similar in it?@JonB For my purposes here as a demo, it doesn't matter. The
QTextEdit
widget is always set to read-only, anyway. -
@JonB For my purposes here as a demo, it doesn't matter. The
QTextEdit
widget is always set to read-only, anyway.@Robert-Hairgrove said in How to visualize LF, CR and other control pictures in QTextEdit?:
The QTextEdit widget is always set to read-only, anyway.
Nothing to do with widget read-only-ness. The issue will come when you take arbitrary C++ code and use
QTextEdit::insertHtml()
or similar from your code. Anyway I will leave that thought with you. -
I am marking this as solved, since I developed a work-around for what I am doing. Basically, I am using the following characters which are supported by the most common fonts:
Description Symbol Unicode valiue Carriage return (CR, 0x0D): ¬
U+00AC Line feed (LF, 0x0A): ¶
U+0086 Space (SPC, 0x20): ·
U+0087 Tab (TAB, 0x09): →
U+2192 Invalid code point: �
U+FFFD Miscellaneous control code: ¤
U+00A4 If
Invalid code point
or aMiscellaneous control code
are detected, the user can hover over the character with the mouse and a tooltip will display the actual Unicode code point as a hexadecimal number.Thanks for all the useful suggestions!
-
-
How did you implement it in the end ?
-
How did you implement it in the end ?
I'm still working on it. :)
The app is kind of a toolkit for inspecting CSV files and correcting them to a limited extent, if necessary. These files can come from anywhere, and often the consumer of a CSV file doesn't have any control over the process which creates them. What it is supposed to do is the following:
- Determine the text encoding used, either semi-automatically (if Unicode is used, this can be 100% automatic) or by letting the user choose, or for unknown 8-bit encodings
iconv
,uchardet
oricu
can be used if the support is installed; - If the encoding cannot be determined, the file is rejected, or else the user can try different encodings. This is where the control characters can be displayed;
- As to how I am implementing this internally, a copy of the text from the file is made, converting it to
QString
and usingQTextEdit
to display it. All of the special control characters can either be displayed or hidden depending on the user's preference; also the highlighting (colors, etc.) is user-configurable; - The delimiter token and quote character can be automatically determined through heuristics, or the user can override these to something else;
- There is a limited amount of support for comments and metadata at the beginning of a file. The user can add or remove these, and the file can be saved without the extra metadata, and/or in a different encoding, lie endings, etc.
Since I don't want to turn this into a full-fledged text editor, the app will just print out a report if the CSV file is not acceptably formatted, indicating the location of invalid characters, and the user can correct this in some other application. When importing CSV data into a database, for example, often the import will fail without giving enough diagnostic information as to where the failure occurred. This app should be helpful in that respect.
- Determine the text encoding used, either semi-automatically (if Unicode is used, this can be 100% automatic) or by letting the user choose, or for unknown 8-bit encodings