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

QPlainTextEdit Clearing Problems



  • Hi, This kind of hard to describe. I am working on a simple front end for NASM that has a text editor and and also a hex editor. The text editor part works fine, but when I try to clear the QPlainTextEdit for the hex editor I have problems. If I use clear() or setPlainText("") I get

    FFFFFFFFFFFFFFF0
    

    inserted at the start of the page, before my header. If I just use setPlainText(header) I get the same, only the cursor is at the start of the header. The first F is only selectable if I double click the text. Backspacing on that F not only adds the text back but adds it again also. A few random clicks produces this

    FFFFFFFFFFFF FFF 0   FFF  FFF FFFF FFF  0   Offset    00 01 02 03 04 
    00000000   0 5 06 07 08 09 0A 0B 0C 0D 0E 0F
    00000000  
    00000000
    

    and this is what it should look like

    Offset    00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
    00000000
    

    The only difference is the sloppy way I format the hex editor

    if(isHex)
        {
            if(ui->plainTextEdit->textCursor().positionInBlock() == 0)
            {
                int lineNumber = (ui->plainTextEdit->textCursor().blockNumber() - 1) * 16;
                ui->plainTextEdit->insertPlainText(QString("%1").arg(lineNumber, 8, 16, QChar('0')).toUpper());
                ui->plainTextEdit->insertPlainText("  ");
            }
    
            if((ui->plainTextEdit->textCursor().positionInBlock() - 10) % 3 == 2)
            {
                ui->plainTextEdit->insertPlainText(" ");
            }
    
            if(ui->plainTextEdit->textCursor().positionInBlock() > 57)
            {
                ui->plainTextEdit->insertPlainText("\n");
            }
        }
    

    While bad form, I do not understand how this would cause the problems I am having.


  • Lifetime Qt Champion

    @admkrk Where is this code located?



  • @admkrk
    Just an observation, because I like spotting coincidences!

    FFFFFFFFFFFFFFF0

    This is -16 in hex, in an 8-byte/64-bit integer.

    int lineNumber = (ui->plainTextEdit->textCursor().blockNumber() - 1) * 16;
    ui->plainTextEdit->insertPlainText(QString("%1").arg(lineNumber, 8, 16, QChar('0')).toUpper());

    This would deliver -16 => FFFFFFFFFFFFFFF0 if .blockNumber() is 0.

    Coincidence? :)



  • @jsulm said in QPlainTextEdit Clearing Problems:

    @admkrk Where is this code located?

    It is in a getCursorPosition() function I use to track the cursor in the status bar.

    I never anticipated that it would ever read on the first line since the header takes up that line. It looks like the cursor is put back to the start before the header is inserted, after the initial time. I have been bitten like this before, but it involved when widgets were made visible.

    Thanks for the good eyes JonB.


Log in to reply