Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Auto scrolling QPlainTextEdit issue with user cursor changes

Auto scrolling QPlainTextEdit issue with user cursor changes

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mirswith
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mirswith
      wrote on last edited by
      #2

      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();
      

      }
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved