PDA

View Full Version : Characters QTextEdit,QText Stream



arturs
3rd April 2015, 15:22
Hi

How to display the characters of constant width in QtextEdit window

Now In the QtextEdit is below format

USERno :0
Family :0
Model :4
SubModel :1

Hex format of text (all lines has the same number of Char)

555345526e6f2020202020202020203a300d
46616d696c792020202020202020203a300d
4d6f64656c202020202020202020203a340d
5375624d6f64656c202020202020203a310d

QTextStream problem

Why if I use if (!file.open(QIODevice::ReadOnly | QIODevice::Text))

In QTextEdit is only one line
USERno :0Family :0Model :4SubModel :1

but at the end of each line there is 0x0d :(

Platform : Windows 7

Regards
Artur

Radek
3rd April 2015, 17:22
The problem seems to consist in the newline character. Your hex output corresponds to certain Macs versions or to some old systems like ZX Spectrum. In windows, the newline is 0x0D 0x0A (2 characters). Therefore, your file needs to contain:


555345526e6f2020202020202020203a300d0a
46616d696c792020202020202020203a300d0a
4d6f64656c202020202020202020203a340d0a
5375624d6f64656c202020202020203a310d0a

in windows, if you want 4 lines. Note that the newline is different in Linux. Even then, it is not clear, which evil has taken all the blanks in the file.

If you want to process text files produced by any (most of) systems, you need to read in binary mode and parse the lines yourself. You must be prepared to
(a) newlines:
- 0x0D - Mac and some others
- 0x0A - Linux
- 0x0D 0x0A - windows, DOS
(b) code pages

arturs
3rd April 2015, 17:50
If I use the following instruction : "if (!file.open(QIODevice::ReadOnly)"
instead of "if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) "

then I have four lines but I do not uderstand why.

Radek
3rd April 2015, 18:39
The QIODevice::Text processes newlines. Because it finds none (in windows), you get one line. Without QIODevice::Text, you are reading in binary mode and the lines are made by QTextEdit, which seems to be content with 0x0D. In general, text files should be read with QIODevice::Text. Counting on "something else" doing the work is a bad idea.

arturs
3rd April 2015, 23:03
Another question :)

How to take the number of line which the cursor is on.

Regards
Artur

arturs
4th April 2015, 16:47
Hi

My idea:
signals:

connect(ui->plainTextEdit,SIGNAL(cursorPositionChanged()),this ,SLOT(sygnal()));

slots:
void signal (){
QTextCursor cursor1 = QTextCursor(ui->plainTextEdit->textCursor());
qDebug () << (cursor1.blockNumber()+1);
}

Above code works (If I move cursor then in debug window I see proper number of line ) but I am not sure if it is good way because when the signal is received It is created QTextCursor object.


Each advice would be appreciated.

Regards
Artur

Radek
4th April 2015, 18:16
The idea works as long as "line" is the thing between carriage returns and the text does not contain specialties (URLs, images, etc.) Each carriage return creates a new block and blockNuber() increases. You need to be prepared on line breaking because the line is too long. Such "line" can occupy several lines but it is the same "line".

As to the text cursor, IMO, it is not critical. But creating cursor1 is not needed. cursor1 is a copy of plainTextEdit->textCursor(), nothing more. You can:


int line = ui->plainTextEdit->textCursor().blockNumber() + 1;
qDebug () << line;

arturs
10th April 2015, 18:43
I need use function replace from QString Class .
I would like to replace 'A' or 'B' for "AB"
something like this

replace ('A' || 'B' , "AB") but It does not work.
I cannot replace first 'A' later 'B' because after first step there will be more 'B' character than before replacing 'A' for AB.

How to do it ?

Regards
Artur

wysota
10th April 2015, 22:34
Use a regular expression.

arturs
12th April 2015, 15:24
Thank you for information

I have last problem with QText Edit I described it in first post.


Improper format in QTextEdit:
USERno :0
Family :0
Model :4
SubModel :1

Hex Code:
555345526e6f2020202020202020203a300d0a
46616d696c792020202020202020203a300d0a
4d6f64656c202020202020202020203a340d0a
5375624d6f64656c202020202020203a310d0a

I would like to have last column in the same line.
I do not have any idea how to do it.

Regards
Artur

anda_skoa
12th April 2015, 17:21
You need to set a fixed-width font.

Cheers,
_

wysota
12th April 2015, 17:22
Use a fixed width font.

arturs
17th April 2015, 18:30
Hi

I have another question :) How to change colour of one line in QPlainTextEdit.
I found the following example:

QTextCursor cursor(linie->textCursor());
QTextCharFormat format;
format.setForeground( QBrush( QColor( "red" ) ) );
cursor.setCharFormat( format );

but it seems to be quite complicated and it changes colour all text (when I want black text I have to write the same code once again )

Regards
Artur