Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of the distance between QPlainTextEdit's borders and its text [duplicate]

In QPlainTextEdit, the first typed symbol is drawn with a small offset, literally, there are a few pixels.

How do I know the value of this offset in the code? Look at the screenshot, distance between red line and selected character is what I need to know.

enter image description here

#include <QApplication>
#include <QPlainTextEdit>
#include <QPainter>


class TextEdit
    :   public QPlainTextEdit
{
protected:
    void paintEvent( QPaintEvent * e ) override
    {
        QPainter p( viewport() );
        p.setPen( Qt::red );
        p.drawRect( viewport()->rect() );

        QPlainTextEdit::paintEvent( e );
    }
};

int main( int argc, char ** argv )
{
    QApplication app( argc, argv );

    TextEdit e;
    e.resize( 100, 100 );
    e.show();

    return QApplication::exec();
}

viewportMargins().left(), contentsMargins().left() are 0.

What else?

like image 359
Igor Mironchik Avatar asked Oct 19 '25 11:10

Igor Mironchik


1 Answers

The property you are looking for is not held in the viewport but in the underlying document.

Here is the said property: QTextDocument::documentMargin().
You can get the underlying document through QPlainTextEdit::document().

In your example, if you display e.document()->documentMargin() you will get the value you are looking for.

The default value is 4. You can change it with QTextDocument::setDocumentMargin().

like image 197
Fareanor Avatar answered Oct 22 '25 02:10

Fareanor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!