PDA

View Full Version : QTextEdit, QPlainTextEdit text padding - how?



0null0
3rd February 2013, 14:51
Hello. I have another question connected with my application - a Ms Notepad-ish thing. I use QPlainTextEdit to work with text files. Unlike QTextEdit, QPlainTextEdit seems to have this nice property of scrolling text line by line by default, so that no line is displayed cut in half. The only problem is, the first displayed line is uncomfortably close to QTextEdit's top edge. What I'd like to do is to set padding for the text inside the control, just like in Ms Notepad, but text only - I still want the scrollbar to fit snugly in its place, with no gaps.

I tried using QPlainTextEdit::setContentsMargins(), QPlainTextEdit::setStyleSheet() with margins and padding, and some things that I even don't remember. I went through documentation of QPlainTextEdit, QTextEdit, and their ancestors, but didn't find anything that looked promising.

Is there any way of doing this without reimplementing text edit?

I attached my little app; everything's in mainwindow.cpp, if you care to have a look. Thanks.

Zlatomir
3rd February 2013, 16:52
You can use:

textedit->setStyleSheet("QTextEdit { padding-left:10; padding-top:10; padding-bottom:10; padding-right:10}");

0null0
3rd February 2013, 17:18
Thank you for your answer!

Unfortunately it doesn't seem to do anything to the control's appearance. It sometimes makes a difference in web browsers, so I tried both CSS as you suggested, and with "px" added after numerical values, but neither worked.

Now, I would expect that would change at least something. Are you sure this method should work? Could it be that something else is messed up with my QPlainTextEdit?

Zlatomir
3rd February 2013, 19:00
This code:


#include <QApplication>
#include <QTextEdit>

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

QTextEdit textEdit;
textEdit.setStyleSheet("QTextEdit { padding-left:10; padding-top:10; padding-bottom:10; padding-right:10}");

textEdit.show();
return a.exec();
}

Gives the following window:
8678
So make sure you set the padding to the textedit you really want to.

LE: On my system it works for QPlainTextEdit too: textEdit.setStyleSheet("QPlainTextEdit { padding-left:10; padding-top:10; padding-bottom:10; padding-right:10}"); (the padding just has a grayer background - i don't know why)

0null0
3rd February 2013, 19:35
Great! Thanks for bolding QPlainTextEdit, I forgot to change that:o I'm embarassed.

This way it works, almost like I'd like it to. However, this way padding also affects the scrollbar. Is there a way to style only the text area, without scrollbars? What I'm trying to achieve is to move just the text away from the border. Padding the whole thing leaves an ugly gap between scrollbar and control frame.

I'll just use external scrollbar if I have to, but it would be easier if it was possible to avoid that.

0null0
4th February 2013, 13:20
Ok, I ended up styling the text edit w/o scrollbars, and adding external scrollbar. It still doesn't look best, but I guess it can be fixed with styling the scrollbar.

Thanks for your help, Zlatomir!

wysota
4th February 2013, 13:29
You can subclass the widget and reimplement its resizeEvent to call setViewportMargins(). Then you can position the scrollbars and everything anywhere you want.

0null0
4th February 2013, 13:58
Fantastic! Works like a charm. Thank you, wysota!