@@ -50,6 +50,7 @@ void QCodeEditor::initFont()
5050void QCodeEditor::performConnections ()
5151{
5252 connect (document (), &QTextDocument::blockCountChanged, this , &QCodeEditor::updateLineNumberAreaWidth);
53+ connect (document (), &QTextDocument::blockCountChanged, this , &QCodeEditor::updateBottomMargin);
5354
5455 connect (verticalScrollBar (), &QScrollBar::valueChanged, [this ](int ) { m_lineNumberArea->update (); });
5556
@@ -119,6 +120,14 @@ void QCodeEditor::resizeEvent(QResizeEvent *e)
119120 QTextEdit::resizeEvent (e);
120121
121122 updateLineGeometry ();
123+ updateBottomMargin ();
124+ }
125+
126+ void QCodeEditor::changeEvent (QEvent *e)
127+ {
128+ QTextEdit::changeEvent (e);
129+ if (e->type () == QEvent::FontChange)
130+ updateBottomMargin ();
122131}
123132
124133void QCodeEditor::updateLineGeometry ()
@@ -127,6 +136,20 @@ void QCodeEditor::updateLineGeometry()
127136 m_lineNumberArea->setGeometry (QRect (cr.left (), cr.top (), m_lineNumberArea->sizeHint ().width (), cr.height ()));
128137}
129138
139+ void QCodeEditor::updateBottomMargin ()
140+ {
141+ auto doc = document ();
142+ if (doc->blockCount () > 1 )
143+ {
144+ // calling QTextFrame::setFrameFormat with an empty document makes the application crash
145+ auto rf = doc->rootFrame ();
146+ auto format = rf->frameFormat ();
147+ int margin = doc->documentMargin ();
148+ format.setBottomMargin (qMax (margin, viewport ()->height () - fontMetrics ().height ()) - margin);
149+ rf->setFrameFormat (format);
150+ }
151+ }
152+
130153void QCodeEditor::updateLineNumberAreaWidth (int )
131154{
132155 setViewportMargins (m_lineNumberArea->sizeHint ().width (), 0 , 0 , 0 );
@@ -818,7 +841,7 @@ bool QCodeEditor::tabReplace() const
818841void QCodeEditor::setTabReplaceSize (int val)
819842{
820843 m_tabReplace.fill (' ' , val);
821- setTabStopDistance (QFontMetrics ( font () ).horizontalAdvance (QString (val * 1000 , ' ' )) / 1000.0 );
844+ setTabStopDistance (fontMetrics ( ).horizontalAdvance (QString (val * 1000 , ' ' )) / 1000.0 );
822845}
823846
824847int QCodeEditor::tabReplaceSize () const
0 commit comments