Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: 'Annotation' of lines in a QTextEdit

  1. #1
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 'Annotation' of lines in a QTextEdit

    I'd like to display some text (e.g. some source code) and 'annotate' it by displaying something like icons, line numbers or so to its left.
    Is there support for something like that in QTextEdit? Are there other (Qt?) classes to achieve such a 'line based annotation'?

    Thank you for any input.
    Best regards
    Christoph

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    Well, not by default.
    I believe fullmetalcoder could give you an answer on how to do this.
    He wrote Edyuk and all...

    Regards

  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    Quote Originally Posted by marcel
    He wrote Edyuk and all...
    Sure but in this case the whole Edyuk source would be too much... QCodeEdit, which is available as a separate module, would be a lot more helpful and should fit your needs : it has highlighting, parenthesis matching, code folding, text indenting, line marks, line numbering and all such features needed by a code editor.

    P.S : I didn't mean to press the thank button but the quote one instead...
    Current Qt projects : QCodeEdit, RotiDeCode

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

    caduel (18th June 2007)

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    P.S : I didn't mean to press the thank button but the quote one instead...
    Hey, no problem. You can take "thank you"s back.
    It was possible, at least last time I checked.

    Regards

  6. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: 'Annotation' of lines in a QTextEdit

    Quote Originally Posted by fullmetalcoder View Post
    Sure but in this case the whole Edyuk source would be too much... QCodeEdit, which is available as a separate module...
    Ok, I take it QCodeEdit is a subset of Edyuk. I will take a look at it.

    Speaking of that project
    • I could not find information on its license... would it be ok to include (and then perhaps modify) it in an inhouse, or even a commercially distributed app?
    • There are no FAQ on the site...
    • I might be able to help a bit with your project (after moving my place in the next few weeks). Is there a list of open tasks anywhere?


    Best regards

  7. #6
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    Quote Originally Posted by caduel View Post
    I could not find information on its license... would it be ok to include (and then perhaps modify) it in an inhouse, or even a commercially distributed app?
    Well, until now I choose a special dual-licensing model :
    • embedding the source into a project, either verbatim or modified, make it fall under GPL because the source is part of Edyuk
    • anyway, using a shared library created from verbatim source is possible as specified by the LGPL
    • I dunno if it is possible because I don't own a commercial license myself but as the author of this code I could exceptionally grant someone the right of using a modified version into a proprietary software
    Quote Originally Posted by caduel View Post
    There are no FAQ on the site...
    Absolutely right but none asked questions that could be put on a FAQ... Remember : FAQ stands for Frequently Asked Questions...

    Quote Originally Posted by caduel View Post
    I might be able to help a bit with your project (after moving my place in the next few weeks). Is there a list of open tasks anywhere?
    Not yet but I could easily create one. Indeed, I'm now using the great WebIssues as a bug/tasks tracker. Log at http://edyuk.tuxfamily.org/webissues through a WebIssues client using login anonymous and password anonymous.
    As for contributing, you're welcome! However it might be a little complicated at the moment since I'm in the process of rewriting most of QCodeEdit from scratch (I've dropped QTextDocument which is way too slow and memory-consuming and thus not suited for code editing... Besides I'm working on two new generic syntax specs, much faster and much more flexible than the old one.)
    Last edited by fullmetalcoder; 18th June 2007 at 08:43.
    Current Qt projects : QCodeEdit, RotiDeCode

  8. #7
    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: 'Annotation' of lines in a QTextEdit

    Quote Originally Posted by marcel View Post
    Well, not by default.
    Actually there is support for creating something like this, but I couldn't get it to work

  9. #8
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    Quote Originally Posted by wysota View Post
    Actually there is support for creating something like this, but I couldn't get it to work
    Really? All I found in the QTextDocument framework which could help achieving this : QTextBlock::userData() but then you've got to render marks on your own...
    Current Qt projects : QCodeEdit, RotiDeCode

  10. #9
    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: 'Annotation' of lines in a QTextEdit

    QAbstractScrollArea::setViewportMargins( int left, int top, int right, int bottom ) - this should let you add a widget to the text edit and render the icons in there. Views use that approach for inserting headers (QHeaderView instances) but I couldn't make it work for the text edit and I didn't want to waste too much time on this. Maybe someone else has more time, luck and/or skills to make it work.

  11. #10
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: 'Annotation' of lines in a QTextEdit

    Yes, setViewportMargins() and QTextEdit::cursorRect() might be enough to determine the y-coordinates of the lines display... still I should take a look at QCodeEdit

    Christoph

  12. #11
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    I am getting a lot of:

    Qt Code:
    1. QPainter::begin: Widget painting can only begin as a result of a paintEvent
    To copy to clipboard, switch view to plain text mode 

    debug calls, and my code is not effective (it does not draw). I think that simply subclassing QTextEdit and reimplementing [b]paintEvent( QPaintEvent * event )[b/] is not enough.

    wysota:
    What part is failing for you?

  13. #12
    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: 'Annotation' of lines in a QTextEdit

    Quote Originally Posted by elcuco View Post
    wysota:
    What part is failing for you?
    I could not put my own widget in the space created by increasing the margin. It just didn't seem to work.

  14. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    Should work if you place the widgets manually( no layouts ).
    Of course, you must take care of resize events to reposition the margin widgets accordingly.

    I have seen it done for a QScrollArea.

    Regards

  15. #14
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    For my whats failing is this code, the layout system has nothing to do, as the margins are inside the widget.

    (edit: just remembered, parts of this code are based on QDevelop, so they are GPL)

    Qt Code:
    1. #include <QTextEdit>
    2.  
    3.  
    4. class newedit : public QTextEdit
    5. {
    6. Q_OBJECT
    7. public:
    8. newedit(QWidget* parent);
    9.  
    10. protected:
    11. void paintEvent ( QPaintEvent * event );
    12.  
    13. };
    14.  
    15. #include "newedit.h"
    16. #include <QApplication>
    17. #include <QPainter>
    18. #include <QTextBlock>
    19. #include <QTextLayout>
    20. #include <QScrollBar>
    21.  
    22. newedit::newedit( QWidget* parent )
    23. {
    24. setViewportMargins( 50, 0, 0, 0 );
    25. }
    26.  
    27. void newedit::paintEvent( QPaintEvent * event )
    28. {
    29. QTextEdit::paintEvent( event );
    30.  
    31. int contentsY = verticalScrollBar()->value();
    32. qreal pageBottom = contentsY + viewport()->height();
    33. int m_lineNumber = 1;
    34. const QFontMetrics fm = fontMetrics();
    35. const int ascent = fontMetrics().ascent() +1;
    36.  
    37. QPainter p( this );
    38. for ( QTextBlock block = document()->begin(); block.isValid(); block = block.next(), m_lineNumber++ )
    39. {
    40. QTextLayout* layout = block.layout();
    41. const QRectF boundingRect = layout->boundingRect();
    42. QPointF position = layout->position();
    43. if ( position.y() +boundingRect.height() < contentsY )
    44. continue;
    45. if ( position.y() > pageBottom )
    46. break;
    47.  
    48. const QString txt = QString::number( m_lineNumber );
    49.  
    50. p.drawText( width() -fm.width( txt ) - 2, qRound( position.y() ) -contentsY +ascent, txt ); // -fm.width( "0" ) is an ampty place/indent
    51. }
    52.  
    53. }
    To copy to clipboard, switch view to plain text mode 

  16. #15
    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: 'Annotation' of lines in a QTextEdit

    Quote Originally Posted by marcel View Post
    Should work if you place the widgets manually( no layouts ).
    I didn't use layouts. It just didn't want to work I probably had some simple mistake but was unable to pinpoint it, although I had the Q*View code for reference... I'll probably try it again some other day.

    Quote Originally Posted by elcuco View Post
    For my whats failing is this code, the layout system has nothing to do, as the margins are inside the widget.
    From what I see you don't use a separate widget for the margin, so the code is "slightly" different.

  17. #16
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    as far as in understand from the api, the margins are still part of the control's responsibility.

    the problem i am facing, is that when i get the paint callback called, i am trying to create a QPainter, it complains that it's not created on the paintEvent() event.

    i hope something see's what i am missing.

  18. #17
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    Quote Originally Posted by elcuco View Post
    as far as in understand from the api, the margins are still part of the control's responsibility.
    The margins are indeed drawn by the scroll area and to paint anything on them it is highly recommened to use a custom layout. Custom is important here because it implies using setViewportMargins() from the layout (actually a public wrapper function but it does not make such a difference).

    t
    Quote Originally Posted by elcuco View Post
    he problem i am facing, is that when i get the paint callback called, i am trying to create a QPainter, it complains that it's not created on the paintEvent() event.

    i hope something see's what i am missing.
    QPainter MUST be initialized on viewport()!!! Indeed (nearly) *ALL* events recieved by a QAbstractScrollArea are redirected from its viewport... Besides it is generally a bad idea to draw the margins from the widget paint event... You should consider placing this code in another widget which would be parented to the editor and managed by the aforementioned custom layout. FYI I made such a custom layout, inspired of Qt 4 Border Layout example, for use with the next version of QCodeEdit.

    Also I hope you consider me as a little more than "something"...
    Current Qt projects : QCodeEdit, RotiDeCode

  19. #18
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    I did not understand your first comment, but I disagree with you on the second comment:

    As far as I understand from the code (I looked inside the sources of Qt), the QScrollArea is composed from a a layout in which they stick in 2 scroll bars, and a viewport. The viewport is what we see as the texteditor, the corner widget will sit generally on the right buttom corner, between the scroll bars.

    The function discussed in this thread will make a margin for the central widget (the viewport). This is why my code before was not working: the main widget (which contains the main layout, which contains the scroll bars and the text editor) was nto drawn: only the view port.

    I think that the parts outside of the margins should be handeled by a special widget inserted to the scroll area. Not sure how yet, even tough I have seen code which uses this API.

  20. #19
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: 'Annotation' of lines in a QTextEdit

    Quote Originally Posted by elcuco View Post
    The function discussed in this thread will make a margin for the central widget (the viewport). This is why my code before was not working: the main widget (which contains the main layout, which contains the scroll bars and the text editor) was nto drawn: only the view port.
    Correct me if I'm wrong but I don't think the viewport can be drawn without the scroll area (thus scroll bars) being painted... The only quirk is that widget placed in the margins are NOT updated when the scroll bars move... You MUST explicitely connect the QSlider::valueChanged(int) signal to your panel widgets (widgets put in the margins).

    Quote Originally Posted by elcuco
    I think that the parts outside of the margins should be handeled by a special widget inserted to the scroll area. Not sure how yet, even tough I have seen code which uses this API.
    You've seen my code? Well, I'd be happy to know how... The last snippet I'm working on are not even available yet on SVN and the previous versions of QCodeEdit did not make use of setViewportMargins()... As with any other widget I don't quite see how you could play with the internals of QAbstractScrollArea... If you're interested in the layout code I guess I can post it...
    Current Qt projects : QCodeEdit, RotiDeCode

  21. #20
    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: 'Annotation' of lines in a QTextEdit

    FMC: If you managed to insert a "margin widget" into a scroll area, could you please present a minimal code that accomplishes that? We might be going in circles here and I'm really interested in finding a solution.

Similar Threads

  1. QTextEdit with custom space between lines
    By Lele in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2006, 08:24
  2. space between lines in QTextEdit
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 19th September 2006, 13:05
  3. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03

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.