Results 1 to 20 of 22

Thread: 'Annotation' of lines in a QTextEdit

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

  2. #2
    Join Date
    Jan 2006
    Posts
    371
    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.

  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 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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.

  5. #5
    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

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

    elcuco (23rd June 2007)

  7. #6
    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

  8. 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
  •  
Qt is a trademark of The Qt Company.