Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22

Thread: 'Annotation' of lines in a QTextEdit

  1. #21
    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
    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)

  3. #22
    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

    Here's one. Just two buttons and a graphics view.

    The cpp:
    Qt Code:
    1. #include "scrollarea.h"
    2. #include <QGraphicsView>
    3. #include <QWidget>
    4. #include <QPushButton>
    5. #include <QScrollBar>
    6.  
    7. #define DEFAULT_MARGIN 30
    8.  
    9. scroller::scroller(QWidget* parent/* =null */)
    10. :QGraphicsView(parent)
    11. {
    12. mLeftWidget = NULL;
    13. mTopWidget = NULL;
    14.  
    15. setScene(new QGraphicsScene(this));
    16. scene()->setSceneRect(QRectF(0, 0, 3000.0f, 3000.0f)); //just for scrolling purposes
    17.  
    18. QScrollBar* hbar = verticalScrollBar();
    19. QScrollBar* vbar = horizontalScrollBar();
    20. connect(hbar, SIGNAL(valueChanged(int)), this, SLOT(adjustMarginWidgets()));
    21. connect(vbar, SIGNAL(valueChanged(int)), this, SLOT(adjustMarginWidgets()));
    22. setViewportMargins(DEFAULT_MARGIN, DEFAULT_MARGIN, 0, 0 );
    23. createMarginWidgets();
    24. }
    25.  
    26. scroller::~scroller()
    27. {
    28.  
    29. }
    30.  
    31. void scroller::createMarginWidgets()
    32. {
    33. mLeftWidget = new QPushButton("L", this);
    34. mTopWidget = new QPushButton("T", this);
    35.  
    36. adjustMarginWidgets();
    37. }
    38.  
    39. void scroller::adjustMarginWidgets()
    40. {
    41. QRect viewportRect = viewport()->geometry();
    42. QRect lrect = QRect(viewportRect.topLeft(), viewportRect.bottomLeft());
    43. lrect.adjust(-DEFAULT_MARGIN, 0, 0, 0);
    44. mLeftWidget->setGeometry(lrect);
    45.  
    46. QRect trect = QRect(viewportRect.topLeft(), viewportRect.topRight());
    47. trect.adjust(0, -DEFAULT_MARGIN, 0, 0);
    48. mTopWidget->setGeometry(trect);
    49. }
    50.  
    51. void scroller::createCornerWidget()
    52. {
    53.  
    54. }
    55.  
    56. void scroller::resizeEvent(QResizeEvent *e)
    57. {
    58. QGraphicsView::resizeEvent(e);
    59. adjustMarginWidgets();
    60. }
    To copy to clipboard, switch view to plain text mode 

    The header:
    Qt Code:
    1. #pragma once
    2.  
    3. #include <QGraphicsView>
    4.  
    5. class QWidget;
    6.  
    7. class scroller : public QGraphicsView
    8. {
    9. Q_OBJECT
    10. public:
    11. scroller(QWidget* = NULL);
    12. ~scroller();
    13.  
    14. protected:
    15. virtual void resizeEvent(QResizeEvent *);
    16.  
    17. private slots:
    18. void adjustMarginWidgets();
    19.  
    20. private:
    21. void createMarginWidgets();
    22. void createCornerWidget();
    23. QPushButton *mLeftWidget;
    24. QPushButton *mTopWidget;
    25. };
    To copy to clipboard, switch view to plain text mode 


    You can just set this as the central widget in a main window, or whatever.

    Regards
    Attached Images Attached Images

  4. The following 3 users say thank you to marcel for this useful post:

    bigboard (14th January 2009), elcuco (23rd June 2007), wysota (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, 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.