Increase the bottom margin in QTextEdit

1.4k views Asked by At

I need to make the scrollbar enabled even if the number of lines is less than the height of the QTextEdit, like in below pic

I tried setDocumentMargin() but it makes margin in all directions (left, right, top, and bottom)

So, is there a way to increase only the bottom margin of the QTextEdit.

Edit Area with larger margin

1

There are 1 answers

5
eyllanesc On BEST ANSWER

If you observe the source code, we see that the function is defined as follows:

void QTextDocument::setDocumentMargin(qreal margin)
{
    // ...
    QTextFrame* root = rootFrame();
    QTextFrameFormat format = root->frameFormat();
    format.setMargin(margin);
    root->setFrameFormat(format);
    // ...
}

So we can do the same through the functions rootFrame() and frameFormat() as I show below:

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    textEdit = QTextEdit()

    format = textEdit.document().rootFrame().frameFormat()
    format.setBottomMargin(10)
    # format.setTopMargin(value)
    # format.setLeftMargin(value)
    # format.setRightMargin(value)
    textEdit.document().rootFrame().setFrameFormat(format)

    textEdit.show()
    sys.exit(app.exec_())

If you just want to make a QTextEdit scrollbar visible, use the following:

textEdit.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
textEdit.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)