Reputation: 71
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 ?
Upvotes: 5
Views: 4074
Reputation: 71
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'
Upvotes: 2
Reputation: 56479
This code move cursor to the first line and then select it until end of line, next it will delete the line:
widget.textEdit->moveCursor(QTextCursor::Start, QTextCursor::MoveAnchor);
widget.textEdit->moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
widget.textEdit->textCursor().deleteChar();
widget.textEdit->textCursor().deleteChar();
Upvotes: 0
Reputation: 7891
One simple way is to turn the vertical scroll-bar off:
textEdit_console->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
Upvotes: 0