Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Text Edit with a fixed number of input lines

I have a Qt "Text Edit" widget in my Gui and this widget is used to log something. I add every line(s) this way:

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

by this way the Text Edit height will increase more and more after each new line. I want it act like a terminal in this case, I mean after some number (that I set) of lines entered, for each new line the first line of Text Edit being deleted to prevent it being too big! should I use a counter with every new line entered and delete the first ones after counter reached it's top or there is better way that do this automatically after

widget.textEdit_console->append(str);

called ?

like image 393
blackfox Avatar asked Nov 03 '22 00:11

blackfox


1 Answers

thank cmannett85 for your advise but for some reason I prefer 'Text Edit', I solved my 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 is there any better more optimized way while using 'Text Edit'

like image 89
blackfox Avatar answered Nov 10 '22 04:11

blackfox