For my whats failing is this code, the layout system has nothing to do, as the margins are inside the widget.
(edit: just remembered, parts of this code are based on QDevelop, so they are GPL)
#include <QTextEdit>
{
Q_OBJECT
public:
protected:
};
#include "newedit.h"
#include <QApplication>
#include <QPainter>
#include <QTextBlock>
#include <QTextLayout>
#include <QScrollBar>
newedit
::newedit( QWidget* parent
) {
setViewportMargins( 50, 0, 0, 0 );
}
{
int contentsY = verticalScrollBar()->value();
qreal pageBottom = contentsY + viewport()->height();
int m_lineNumber = 1;
const int ascent = fontMetrics().ascent() +1;
for ( QTextBlock block
= document
()->begin
(); block.
isValid(); block
= block.
next(), m_lineNumber
++ ) {
const QRectF boundingRect
= layout
->boundingRect
();
QPointF position
= layout
->position
();
if ( position.y() +boundingRect.height() < contentsY )
continue;
if ( position.y() > pageBottom )
break;
p.drawText( width() -fm.width( txt ) - 2, qRound( position.y() ) -contentsY +ascent, txt ); // -fm.width( "0" ) is an ampty place/indent
}
}
#include <QTextEdit>
class QPaintEvent;
class newedit : public QTextEdit
{
Q_OBJECT
public:
newedit(QWidget* parent);
protected:
void paintEvent ( QPaintEvent * event );
};
#include "newedit.h"
#include <QApplication>
#include <QPainter>
#include <QTextBlock>
#include <QTextLayout>
#include <QScrollBar>
newedit::newedit( QWidget* parent )
: QTextEdit()
{
setViewportMargins( 50, 0, 0, 0 );
}
void newedit::paintEvent( QPaintEvent * event )
{
QTextEdit::paintEvent( event );
int contentsY = verticalScrollBar()->value();
qreal pageBottom = contentsY + viewport()->height();
int m_lineNumber = 1;
const QFontMetrics fm = fontMetrics();
const int ascent = fontMetrics().ascent() +1;
QPainter p( this );
for ( QTextBlock block = document()->begin(); block.isValid(); block = block.next(), m_lineNumber++ )
{
QTextLayout* layout = block.layout();
const QRectF boundingRect = layout->boundingRect();
QPointF position = layout->position();
if ( position.y() +boundingRect.height() < contentsY )
continue;
if ( position.y() > pageBottom )
break;
const QString txt = QString::number( m_lineNumber );
p.drawText( width() -fm.width( txt ) - 2, qRound( position.y() ) -contentsY +ascent, txt ); // -fm.width( "0" ) is an ampty place/indent
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks