Results 1 to 8 of 8

Thread: QTextEdit, QPlainTextEdit text padding - how?

  1. #1
    Join Date
    Feb 2013
    Location
    Warsaw, Poland
    Posts
    10
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QTextEdit, QPlainTextEdit text padding - how?

    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.
    Attached Files Attached Files

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTextEdit, QPlainTextEdit text padding - how?

    You can use:
    Qt Code:
    1. textedit->setStyleSheet("QTextEdit { padding-left:10; padding-top:10; padding-bottom:10; padding-right:10}");
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Zlatomir for this useful post:

    0null0 (4th February 2013)

  4. #3
    Join Date
    Feb 2013
    Location
    Warsaw, Poland
    Posts
    10
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit, QPlainTextEdit text padding - how?

    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?

  5. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTextEdit, QPlainTextEdit text padding - how?

    This code:
    Qt Code:
    1. #include <QApplication>
    2. #include <QTextEdit>
    3.  
    4. int main(int argc, char** argv)
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. QTextEdit textEdit;
    9. textEdit.setStyleSheet("QTextEdit { padding-left:10; padding-top:10; padding-bottom:10; padding-right:10}");
    10.  
    11. textEdit.show();
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Gives the following window:
    padding.jpg
    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)
    Last edited by Zlatomir; 3rd February 2013 at 19:08.

  6. The following user says thank you to Zlatomir for this useful post:

    0null0 (4th February 2013)

  7. #5
    Join Date
    Feb 2013
    Location
    Warsaw, Poland
    Posts
    10
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit, QPlainTextEdit text padding - how?

    Great! Thanks for bolding QPlainTextEdit, I forgot to change that 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.
    Last edited by 0null0; 3rd February 2013 at 19:40.

  8. #6
    Join Date
    Feb 2013
    Location
    Warsaw, Poland
    Posts
    10
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit, QPlainTextEdit text padding - how?

    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!

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextEdit, QPlainTextEdit text padding - how?

    You can subclass the widget and reimplement its resizeEvent to call setViewportMargins(). Then you can position the scrollbars and everything anywhere you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. The following user says thank you to wysota for this useful post:

    0null0 (4th February 2013)

  11. #8
    Join Date
    Feb 2013
    Location
    Warsaw, Poland
    Posts
    10
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit, QPlainTextEdit text padding - how?

    Fantastic! Works like a charm. Thank you, wysota!

Similar Threads

  1. Replies: 6
    Last Post: 1st October 2012, 14:05
  2. Selecting Text QPlainTextEdit
    By smhall316 in forum Newbie
    Replies: 0
    Last Post: 7th December 2010, 20:09
  3. margins in QTextEdit/QPlainTextEdit
    By corrado in forum Qt Programming
    Replies: 2
    Last Post: 22nd April 2010, 09:35
  4. Maximum input length for QTextEdit or QPlainTextEdit ??
    By b_ginner in forum Qt Programming
    Replies: 2
    Last Post: 22nd August 2009, 20:57
  5. QPlainTextEdit reload text
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2009, 14:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.