PDA

View Full Version : Line numbering of a plaintextedit



aaditya190
9th December 2013, 09:50
I have been following this link for line numbering in a plaintextedit..

http://qt-project.org/doc/qt-4.8/widgets-codeeditor.html

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

http://www.qtcentre.org/threads/44555-pls-help-Need-Qpainter-to-draw-over-a-background-image-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?

Santosh Reddy
9th December 2013, 10:22
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.

aaditya190
9th December 2013, 10:27
I implemented the QPainter using this set of code...



class frameclass : public QFrame
{

Q_OBJECT
public:
frameclass( QWidget * parent) : QFrame(parent)
{


}


void paintEvent( QPaintEvent * event )
{
QFrame::paintEvent(event);
QPainter p(this);
QRect r = rect();



p.fillRect(r,Qt::lightGray);


}
};




And this is the code shared in the example for line numbering.


void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{
QPainter painter(lineNumberArea);
painter.fillRect(event->rect(), Qt::lightGray);


//line numbering logic

QTextBlock block = firstVisibleBlock();



int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOff set()).top();
int bottom = top + (int) blockBoundingRect(block).height();

while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number);
}

block = block.next();
top = bottom;
bottom = top + (int) blockBoundingRect(block).height();
++blockNumber;
}
}



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

ChrisW67
9th December 2013, 20:23
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.

aaditya190
10th December 2013, 05:10
@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...

ChrisW67
10th December 2013, 05:44
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.