Results 1 to 20 of 22

Thread: 'Annotation' of lines in a QTextEdit

Threaded View

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

    Default Re: 'Annotation' of lines in a QTextEdit

    Quote Originally Posted by wysota View Post
    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.
    Minimal? well... my code is not really minimal because the layout used has some complexities and I use a custom class, QPanel (inheriting from QWidget). As I don't feel like spending too much time on trimming down my code so I'll give you a layout-less sample... Fell free to ask for the layout code if you want something a little more flexible but then don't expect something that will work out of box...

    So here comes the minimum (not very handy nor fully functionnal but works...) :
    Qt Code:
    1. #include <QPainter>
    2. #include <QPaintEvent>
    3.  
    4. #include <QTextEdit>
    5. #include <QTextBlock>
    6. #include <QTextLayout>
    7.  
    8. #include <QScrollBar>
    9. #include <QApplication>
    10.  
    11. class SampleArea : public QTextEdit
    12. {
    13. public:
    14. SampleArea(QWidget *p = 0)
    15. : QTextEdit(p)
    16. {
    17. QString txt = "eat me...\ndrink me...\n";
    18.  
    19. for ( int i = 0; i < 8; ++i )
    20. txt += txt;
    21.  
    22. document()->setPlainText(txt);
    23. }
    24.  
    25. inline void setMargins(int l, int r, int t, int b)
    26. { setViewportMargins(l, r, t, b); }
    27. };
    28.  
    29. class SamplePanel : public QWidget
    30. {
    31. public:
    32. SamplePanel(SampleArea *a)
    33. : QWidget(a), m_area(a)
    34. {
    35. setFixedWidth(50);
    36. setFixedHeight(a->height());
    37.  
    38. connect(a->verticalScrollBar() , SIGNAL( valueChanged(int) ),
    39. this , SLOT ( update() ));
    40.  
    41. a->setMargins(50, 0, 0, 0);
    42. }
    43.  
    44. protected:
    45. virtual void paintEvent(QPaintEvent *e)
    46. {
    47. e->accept();
    48.  
    49. QPainter p(this);
    50.  
    51. int m_lineNumber = 1;
    52. const QFontMetrics fm = fontMetrics();
    53. const int ascent = fontMetrics().ascent() + 1;
    54. QTextBlock block = m_area->document()->begin();
    55. int contentsY = m_area->verticalScrollBar()->value();
    56. qreal pageBottom = contentsY + m_area->viewport()->height();
    57.  
    58. for ( ; block.isValid(); block = block.next(), ++m_lineNumber )
    59. {
    60. QTextLayout* layout = block.layout();
    61. QPointF position = layout->position();
    62. const QRectF boundingRect = layout->boundingRect();
    63.  
    64. if ( position.y() + boundingRect.height() < contentsY )
    65. continue;
    66.  
    67. if ( position.y() > pageBottom )
    68. break;
    69.  
    70. const QString txt = QString::number( m_lineNumber );
    71.  
    72. p.drawText(width() - fm.width(txt) - 2, qRound(position.y()) - contentsY + ascent, txt);
    73. }
    74. }
    75.  
    76. private:
    77. SampleArea *m_area;
    78. };
    79.  
    80. int main (int argc, char **argv)
    81. {
    82. QApplication app(argc, argv);
    83.  
    84. SampleArea area;
    85. SamplePanel panel(&area);
    86.  
    87. area.show();
    88.  
    89. return app.exec();
    90. }
    To copy to clipboard, switch view to plain text mode 
    Current Qt projects : QCodeEdit, RotiDeCode

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

    elcuco (23rd June 2007)

Similar Threads

  1. QTextEdit with custom space between lines
    By Lele in forum Qt Programming
    Replies: 4
    Last Post: 22nd November 2006, 09:24
  2. space between lines in QTextEdit
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 19th September 2006, 14:05
  3. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 07: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
  •  
Qt is a trademark of The Qt Company.