Results 1 to 6 of 6

Thread: QPlainTextEdit auto scroll

  1. #1
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QPlainTextEdit auto scroll

    Hi!

    I made MyTextEdit derived from QPlainTextEdit with added line numbers widget. I also changed style by setting:
    Qt Code:
    1. setStyle(new QCleanlooksStyle);
    To copy to clipboard, switch view to plain text mode 
    I also have a word wrapping on. Now, when Im typing some text on the last line of the document (the last on the bottom) and I reach the end of line, text is wrapped but text edit doesn't want to scroll. Whats more it even doesn't want to extend vertical scrollbar. You cannont scroll with scrollbar or mouse wheel unless you resize the window or type enter to make a new line, because making new lines is scrolling the text edit. It's quite annoying as I'm writing LaTeX document so dont need to make new lines, cause I have pdflatex for that job and line wrapping is all I need.

    Any suggestions?
    (I'm using Qt 4.4.3 on Vista with msvc2005)

    Thanks in advance.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QPlainTextEdit auto scroll

    Could we see some code?

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPlainTextEdit auto scroll

    There's one relevant task-tracker entry: AutoScrolling does not seem to work when using the QPlainTextEdit::appendPlainText method. Give it a try with a recent 4.5 snapshot..
    J-P Nurmi

  4. The following user says thank you to jpn for this useful post:

    faldzip (31st January 2009)

  5. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QPlainTextEdit auto scroll

    Thanks for your answers, I compiled with Qt 4.5.0-beta1 and it's working but... now i have different problem (which did not appear with 4.4.3). The font settings are not propagating to my LineNumberWidget :/
    Here's the mytextedit.cpp source code:
    Qt Code:
    1. #include "mytextedit.h"
    2. #include "linenumberwidget.h"
    3.  
    4. MyTextEdit::MyTextEdit(QWidget * parent)
    5. : QPlainTextEdit(parent)
    6. , m_lineNumbersWidth(LN_BASE_WIDTH)
    7. {
    8. QFont font;
    9. #if defined(Q_WS_X11)
    10. font.setFamily("Monospace");
    11. #else
    12. font.setFamily("Courier New");
    13. #endif
    14. font.setPointSize(9);
    15. setFont(font);
    16. setViewportMargins(m_lineNumbersWidth, 0, 0, 0);
    17. //setFrameStyle(QFrame::NoFrame);
    18. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    19. m_lineNumberWidget = new LineNumberWidget(this);
    20. font.setPointSize(8); // <- added in build with 4.5.0
    21. m_lineNumberWidget->setFont(font); // <- 4.5.0
    22. QRect vr = viewport()->rect();
    23. m_lineNumberWidget->setGeometry(vr.left() + frameWidth(), vr.top() + frameWidth(), m_lineNumbersWidth, vr.height());
    24. connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(slot_updateRequest(QRect,int)));
    25. }
    26.  
    27. void MyTextEdit::resizeEvent(QResizeEvent * e)
    28. {
    29. QPlainTextEdit::resizeEvent(e);
    30. QRect vr = viewport()->rect();
    31. m_lineNumberWidget->setGeometry(vr.left() + frameWidth(), vr.top() + frameWidth(), m_lineNumbersWidth, vr.height());
    32. }
    33.  
    34. void MyTextEdit::lineNumberWidgetPaintEvent(QPaintEvent * e)
    35. {
    36. QPainter p(m_lineNumberWidget);
    37. QColor c(240, 240, 240);
    38. p.fillRect(e->rect(), c);
    39. p.setPen(QColor(175, 175, 75));
    40. QRect vr = viewport()->rect();
    41. QFontMetrics fm(p.fontMetrics());
    42. QTextBlock block = firstVisibleBlock();
    43. int top = (int)blockBoundingGeometry(block).translated(contentOffset()).top();
    44. int bottom = top;
    45. while (block.isValid() && top <= e->rect().bottom())
    46. {
    47. top = bottom;
    48. bottom = top + (int)blockBoundingRect(block).height();
    49. QTextBlock nextBlock = block.next();
    50. QString number = QString::number(block.blockNumber() + 1);
    51. int offset = (fontMetrics().height() - fm.height())/2; // <- 4.5.0
    52. p.drawText(2, top + offset, m_lineNumbersWidth - 4, fm.height(), Qt::AlignRight, number);
    53. block = nextBlock;
    54. }
    55. }
    56.  
    57. void MyTextEdit::slot_updateRequest(const QRect & rect, int dy)
    58. {
    59. m_lineNumberWidget->scroll(0, dy);
    60. m_lineNumberWidget->update(0, rect.y(), 36, rect.height());
    61. }
    To copy to clipboard, switch view to plain text mode 

    because the minimum font point size in my app is 8 i set that size in my LineNumberWidget and now i'm using an offset to center line numbers vertically. But I was happy with the effect I had in 4.4.3 where font of LineNumberWidget was changing to the same font as it was set in MyTextEdit - the parent widget for LineNumberWidget.
    Do I have to tell that child widget explicitly that it should copy parents font settings?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QPlainTextEdit auto scroll

    Probably has something to do with this change: Font and Palette propagation in Qt... Does it make any difference if you create the child widget first and then change the font?
    J-P Nurmi

  7. #6
    Join Date
    Jul 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QPlainTextEdit auto scroll

    Hi

    Quote Originally Posted by faldżip View Post
    #include "mytextedit.h"
    #include "linenumberwidget.h"
    Can you please post code for above .h(s) and how did you implemented LineNumberWidget. And also can you show code for firstVisibleBlock() ?

    Thanks
    Pritpal Bedi
    Pritpal Bedi
    a student of software analysis and design

Similar Threads

  1. Auto scroll down in QScrollArea
    By EricF in forum Qt Programming
    Replies: 2
    Last Post: 29th December 2008, 12:51
  2. Continously Scroll QPlainTextEdit
    By GimpMaster in forum Newbie
    Replies: 2
    Last Post: 12th September 2008, 17:35
  3. QTextEdit + auto scroll
    By jbpvr in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2008, 09:30
  4. how to add scroll bar on QGridLayout in QT??
    By sharvari in forum Newbie
    Replies: 3
    Last Post: 19th February 2008, 14:31
  5. QScrollArea's Scroll Bars
    By ToddAtWSU in forum Qt Programming
    Replies: 5
    Last Post: 19th September 2006, 13:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.