PDA

View Full Version : How to manipulate a text in QTextEdit



Ryan111
29th March 2015, 08:36
Hi folk!
I'm trying to make a program to manipulate a text in a QTextEdit and show it after changes in the QTextEdit again. the first problem is that I'm completely newbie. the data is this:


00000000000000000000000000000000FEFFFF01FEFFFF03FF FFFF07FFFFFF070F0080070F0080070F0080070F0080070F00 80070F0080070F0080070F0080070F0080070F0080070F0080 070F0080070F0080070F0080071F00C007FFFFFF07FFFFFF03 FEFFFF03FCFFFF010000000000000000000000000000000000 000000
00000000000000000000000000000000FFFFFF07FFFFFF07FF FFFF07FFFFFF07000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 000000
000000000000000000000000000000000FE0FF070FF0FF070F F0FF070FF0FF070FF880070FF880070FF880070FF880070FF8 80070FF880070FF880070FF880070FF880070FF880070FF880 070FF880070FF880070FF880070F788007FF7F8007FF7F8007 FE3F8007FC1F80070000000000000000000000000000000000 000000
000000000000000000000000000000000FF880070FF880070F F880070FF880070FF880070FF880070FF880070FF880070FF8 80070FF880070FF880070FF880070FF880070FF880070FF880 070FF880070FF880070FF880070FF8C007FFFFFF07FFFFFF07 FEFFFF03FCFFFF010000000000000000000000000000000000 000000
00000000000000000000000000000000FF3F0000FF7F0000FF 7F0000FF7F0000007800000078000000780000007800000078 00000078000000780000007800000078000000780000007800 0000780000007800000078000000780000FFFFFF07FFFFFF07 FFFFFF07FFFFFF070000000000000000000000000000000000 000000
00000000000000000000000000000000FF3F8007FF7F8007FF 7F8007FF7F80070F7880070FF880070FF880070FF880070FF8 80070FF880070FF880070FF880070FF880070FF880070FF880 070FF880070FF880070FF880070FF8C0070FF0FF070FF0FF07 0FF0FF030FC0FF010000000000000000000000000000000000 000000
00000000000000000000000000000000FCFFFF01FEFFFF03FF FFFF07FFFFFF070FF880070FF880070FF880070FF880070FF8 80070FF880070FF880070FF880070FF880070FF880070FF880 070FF880070FF880070FF880070FF8C0070FF0FF070FF0FF07 0FF0FF030FC0FF010000000000000000000000000000000000 000000
000000000000000000000000000000000F0000000F0000000F 0000000F0000000F0000000F0000000F0000000F0000000F00 00000F0000000F0000000F0000000F0000000F0000000F0000 000F0000000F0000000F0000001F000000FFFFFF07FFFFFF07 FEFFFF07FCFFFF070000000000000000000000000000000000 000000
00000000000000000000000000000000FCFFFF01FEFFFF03FF FFFF07FFFFFF070FF880070FF880070FF880070FF880070FF8 80070FF880070FF880070FF880070FF880070FF880070FF880 070FF880070FF880070FF880071FF8C007FFFFFF07FFFFFF07 FEFFFF03FCFFFF010000000000000000000000000000000000 000000
00000000000000000000000000000000FC3F8007FE7F8007FF 7F8007FF7F80070F7880070FF880070FF880070FF880070FF8 80070FF880070FF880070FF880070FF880070FF880070FF880 070FF880070FF880070FF880070FF8C007FFFFFF07FFFFFF03 FEFFFF03FCFFFF010000000000000000000000000000000000 000000

the scenario is this:
When I put the data(above data) in the QTextEdit and push the button, the program put this string ",0x" after each 8 character. somthings like this:



000000000,0x000000000,0x00000000,0x0000000F,0xEFFF F01F,0xEFFFF03F,...

and show it on the QTextEdit.(replace)

I need to a simple guidance.

anda_skoa
29th March 2015, 09:58
That's not really related to QTextEdit, just basic string manipulation.
http://doc.qt.io/qt-5/qstring.html#insert

Cheers,
_

Ryan111
29th March 2015, 10:37
That's not really related to QTextEdit, just basic string manipulation.
http://doc.qt.io/qt-5/qstring.html#insert

Cheers,
_
Thanks!
that works but not correctly! :( look:

http://upload.tehran98.com/upme/uploads/b09dca1818270e821.png

as you can see, after several insert, it cannot put ",0x" string anymore. why? the code is this:


void MainWindow::on_pushButton_clicked()
{
QString txt = ui->textEdit->toPlainText();

for (quint32 t = 0 ; t <= (txt.size()/8) ; t = t+11 )
{
txt.insert( t , QString(",0x"));

}

ui->textEdit->setText(txt);
}

stampede
29th March 2015, 11:04
for (quint32 t = 0 ; t <= (txt.size()/8) ; t = t+11 )
{
txt.insert( t , QString(",0x"));

}
You are comparing the count of characters (stored in "t" and moved by 11 positions on each iteration) with the number of "octets" in the string. The former will always be greater and the loop will end sooner than you expect.
Look at this simple example with the string "0123456789abcdef" (16 chars):

iteration 0:
t=0, string = "0123456789abcdef"
t <= 2 ? true

iteration 1:
t=11, string = ",0x0123456789abcdef"
t <= (19/8) ? false


IMHO this would be a lot easier this way:


QString txt = ui->textEdit->toPlainText();
QStringList tmp;
while (txt.isEmpty()==false) {
tmp << txt.left(8);
txt.remove(0,8);
}
ui->textEdit->setText(tmp.join(",0x"));