Qt Text Edit with a fixed number of input lines

I have Qt "Text Editing" in my Gui, and this widget is used to register something. I add each line as follows:

QString str;
str = ...
widget.textEdit_console->append(str);

thus, the height of the text editing will increase more and more after each new line. I want it to act as a terminal in this case, I mean after a certain number (which I set) of the entered lines, for each new line the first line of text editing is deleted to prevent it being too big! should I use the counter with each new line entered and delete the first after the counter reaches the top or is there a better way to do this automatically after

widget.textEdit_console->append(str);

is called?

+5
source share
3 answers

thanks cmannett85 for the tips, but for some reason I prefer "Edit Text", I solved the problem this way:

void mainWindow::appendLog(const QString &str)
{
    LogLines++;
    if (LogLines > maxLogLines)
    {
        QTextCursor tc = widget.textEdit_console->textCursor();
        tc.movePosition(QTextCursor::Start);
        tc.select(QTextCursor::LineUnderCursor);
        tc.removeSelectedText(); // this remove whole first line but not that '\n'
        tc.deleteChar(); // this way the first line will completely being removed
        LogLines--;
    }
    widget.textEdit_console->append(str);
}

I still don't know if there is a more optimized way using "Text Editing"

+2
source

One easy way is to disable the vertical scrollbar:

 textEdit_console->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
0
source

This code moves the cursor to the first line, and then selects it to the end of the line, then it deletes the line:

widget.textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
widget.textEdit->moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
widget.textEdit->textCursor().deleteChar();
widget.textEdit->textCursor().deleteChar();
0
source

All Articles