4

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

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

6

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)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I tried that, but not working with me. Still the scrollbar hidden – Moataz El-Ibiary Aug 26 '17 at 22:44
  • @MoatazEl-Ibiary What is your goal? The margins or the scrollbar? – eyllanesc Aug 26 '17 at 22:45
  • My goal is to make the scrollbar enabled. I was trying to do that using margins. But the margin is not my target – Moataz El-Ibiary Aug 26 '17 at 22:51
  • I recommend you put as a title something more direct because it should show your main objective, in your explanation you can describe what you tried. – eyllanesc Aug 26 '17 at 22:57
  • Thanks, I figured out why it wasn't working. The problem was that 10 in `format.setBottomMargin(10)` is so small, so the bottom margin wasn't enough to show the scrollbar. `textEdit.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)` was unnecessary after adjusting the bottom margin. – Moataz El-Ibiary Aug 26 '17 at 23:28