Results 1 to 6 of 6

Thread: Line numbering of a plaintextedit

  1. #1
    Join Date
    Nov 2013
    Location
    Chandigarh, India
    Posts
    62
    Thanks
    8
    Thanked 11 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Line numbering of a plaintextedit

    I have been following this link for line numbering in a plaintextedit..

    http://qt-project.org/doc/qt-4.8/wid...odeeditor.html

    And i have been able to paint the area where line numbers will be displayed using this link...

    http://www.qtcentre.org/threads/4455...-of-mainwindow..


    But I am unable to get as to how to introduce the line numbering on the area and which event should I use to implement this? Can anyone help me with some code?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Line numbering of a plaintextedit

    The example you have been following will exactly do what you want. Could you show what have you tried and what is that you are not able to understand from the example.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Nov 2013
    Location
    Chandigarh, India
    Posts
    62
    Thanks
    8
    Thanked 11 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Line numbering of a plaintextedit

    I implemented the QPainter using this set of code...

    Qt Code:
    1. class frameclass : public QFrame
    2. {
    3.  
    4. Q_OBJECT
    5. public:
    6. frameclass( QWidget * parent) : QFrame(parent)
    7. {
    8.  
    9.  
    10. }
    11.  
    12.  
    13. void paintEvent( QPaintEvent * event )
    14. {
    15. QFrame::paintEvent(event);
    16. QPainter p(this);
    17. QRect r = rect();
    18.  
    19.  
    20.  
    21. p.fillRect(r,Qt::lightGray);
    22.  
    23.  
    24. }
    25. };
    To copy to clipboard, switch view to plain text mode 


    And this is the code shared in the example for line numbering.
    Qt Code:
    1. void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(lineNumberArea);
    4. painter.fillRect(event->rect(), Qt::lightGray);
    5.  
    6.  
    7. //line numbering logic
    8.  
    9. QTextBlock block = firstVisibleBlock();
    10.  
    11.  
    12.  
    13. int blockNumber = block.blockNumber();
    14. int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
    15. int bottom = top + (int) blockBoundingRect(block).height();
    16.  
    17. while (block.isValid() && top <= event->rect().bottom()) {
    18. if (block.isVisible() && bottom >= event->rect().top()) {
    19. QString number = QString::number(blockNumber + 1);
    20. painter.setPen(Qt::black);
    21. painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
    22. Qt::AlignRight, number);
    23. }
    24.  
    25. block = block.next();
    26. top = bottom;
    27. bottom = top + (int) blockBoundingRect(block).height();
    28. ++blockNumber;
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    But I am unable to implement this code into my set of code to get line numbering done. Can you help me with this?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Line numbering of a plaintextedit

    Can you help me with this?
    You need to read and understand the example to see how the line numbers actually get in to the view.

  5. #5
    Join Date
    Nov 2013
    Location
    Chandigarh, India
    Posts
    62
    Thanks
    8
    Thanked 11 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Line numbering of a plaintextedit

    @ChrisW67.. I tried all possible input codes but I am not able to paint any text on the line number area. Thus I posted this problem .. If you can help me with some simpler code would be very beneficial for me.. I am very new to Qt....So I am not able to properly get the code...

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Line numbering of a plaintextedit

    All the code to do this is in the codeeditor.h and codeeditor.cpp that make up the example. I cannot provide a simpler set of code.

    The LineNumberArea class paints numbers onto itself when its paintEvent() is called by Qt. It uses the code in the function CodeEditor::lineNumberAreaPaintEvent() in its parent CodeEditor object to do this. The logic for setting the size, position and content of the line number area is in the CodeEditor.

    We have no idea what role your class frameclass is supposed to have, but it currently makes no attempt to draw anything other than a blank background.

  7. The following user says thank you to ChrisW67 for this useful post:

    aaditya190 (10th December 2013)

Similar Threads

  1. line numbering in textedit
    By aaditya190 in forum Newbie
    Replies: 6
    Last Post: 6th December 2013, 06:38
  2. line numbering in text editor.
    By parulkalra in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2013, 20:21
  3. Line Numbering in textedit
    By aaditya190 in forum Newbie
    Replies: 2
    Last Post: 15th November 2013, 08:58
  4. Replies: 2
    Last Post: 13th August 2010, 10:08

Tags for this Thread

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.