Auto scrolling QPlainTextEdit issue with user cursor changes
-
HI. I have a simple log widget I made based on a QPlainTextEdit control that basically logs blurbs of text and always keeps the view scrolling with the latest text as long as the scroll bar is all the way down.
Here is my code to do so:
@void LogView::messageLogged(const QString & message)
{
bool doScroll = (_textView->verticalScrollBar()->isVisible() && !_scrollVisible) || _textView->verticalScrollBar()->sliderPosition()==_textView->verticalScrollBar()->maximum();_textView->textCursor().beginEditBlock(); _textView->textCursor().movePosition(QTextCursor::End); _textView->textCursor().insertText(message); _textView->textCursor().endEditBlock(); if( doScroll ) {
_textView->verticalScrollBar()->setSliderPosition(_textView->verticalScrollBar()->maximum());
}_scrollVisible = _textView->verticalScrollBar()->isVisible();
}@
This seems to be working great except for one case and that is when the user clicks in the text widget the method above will place the text where ever they clicked, it seems as if the movePosition call above is not even being used and as a matter of fact if I comment out the movePosition call my control behaves exactly the same.
Any ideas?
Thanks!
-
Fixed my problem. Reading the docs closer about textCursor() "Returns a copy of the QTextCursor".. Ah ha.. a Copy! I was doing my operations on a new copy each time. My updated and working function now looks like this:
@void LogView::messageLogged(const QString & message)
{
bool doScroll = (_textView->verticalScrollBar()->isVisible() && !_scrollVisible) || _textView->verticalScrollBar()->sliderPosition()==_textView->verticalScrollBar()->maximum();QTextCursor c = _textView->textCursor(); c.beginEditBlock(); c.movePosition(QTextCursor::End); c.insertText(message); c.endEditBlock(); if( doScroll ) {
_textView->verticalScrollBar()->setSliderPosition(_textView->verticalScrollBar()->maximum());
}_scrollVisible = _textView->verticalScrollBar()->isVisible();
}
@