I implemented the QPainter using this set of code...
class frameclass
: public QFrame{
Q_OBJECT
public:
{
}
{
p.fillRect(r,Qt::lightGray);
}
};
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);
}
};
To copy to clipboard, switch view to plain text mode
And this is the code shared in the example for line numbering.
void CodeEditor
::lineNumberAreaPaintEvent(QPaintEvent *event
) {
painter.fillRect(event->rect(), Qt::lightGray);
//line numbering logic
int blockNumber = block.blockNumber();
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
int bottom = top + (int) blockBoundingRect(block).height();
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
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;
}
}
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(contentOffset()).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;
}
}
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?
Bookmarks