Results 1 to 13 of 13

Thread: QTextEdit - margins, indents within QTextEdit

  1. #1
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QTextEdit - margins, indents within QTextEdit

    I want to have possibility of changing margins/indents in QTextEdit, without resizing QTextEdit.

    What I mean is that when I use stylesheets:
    Qt Code:
    1. textEdit->setStyleSheet("QTextEdit{margin-left: 30px; (...)}");
    To copy to clipboard, switch view to plain text mode 
    the result looks like this:
    1.png
    QTextEdit is resized, and scroll bar is moved with it.

    I would like to achieve something like this:
    2.png
    Margins/indents are within QTextEdit and scrollbar is on the right just by window frame.

    I tried with:
    Qt Code:
    1. textEdit->setStyleSheet("QTextEdit{text-indent: 60px}");
    To copy to clipboard, switch view to plain text mode 
    but that doesn't work.

    I also tried with QTextBlockFormat:
    Qt Code:
    1. textCursor = new QTextCursor(textEdit->textCursor());
    2. textBlockFormat = textCursor->blockFormat();
    3. textBlockFormat.setTopMargin(50);
    4. textBlockFormat.setLeftMargin(50);
    To copy to clipboard, switch view to plain text mode 
    but that also doesn't work.

    Anyone can guide me how to achieve this?
    Last edited by Acamapichtli; 5th April 2014 at 21:45.

  2. #2
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QTextEdit - margins, indents within QTextEdit

    Did you place the QTextEdit widget in a layout?

    To your base widget, set a layout, to the layout add QTextEdit.
    Finally you can set content margins to your layout by setting it's content margins, if necessary.

  3. #3
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit - margins, indents within QTextEdit

    Yes, I place QTextEdit in QVBoxLayout:
    Qt Code:
    1. TestApp::TestApp()
    2. {
    3. textEdit = new QTextEdit;
    4.  
    5. layoutMain = new QVBoxLayout;
    6. layoutMain->addWidget(textEdit);
    7. layoutMain->setMargin(0);
    8.  
    9. window = new QWidget;
    10. window->setLayout(layoutMain);
    11. setCentralWidget(window);
    12. }
    To copy to clipboard, switch view to plain text mode 
    I tried using setContentsMargins in both textEdit and layoutMain, but it doesn't set margins at all or it works like in on picture 1.
    I don't know if you understood me correctly, I would like custom margins inside the textEdit, not the layoutMain margins.

  4. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextEdit - margins, indents within QTextEdit


  5. #5
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit - margins, indents within QTextEdit

    Thanks for reply, but I tried this (first code block of my first post).

    When I hide ScrollBar it looks like I want, when it is visible is what destroys my idea - the ScrollBar because it's almost in the middle of layout.

    Now I try to do this by using custom scrollbar: I hide the default scrollbar, set margins using StyleSheet, and create new scrollbar and insert it on the right side of the layoutMain (out of textEdit).
    Qt Code:
    1. textEdit->setFrameStyle(QFrame::NoFrame);
    2. textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    3. textEdit->setStyleSheet("QTextEdit"
    4. "{margin-left: 40px;"
    5. "margin-right: 40px;"
    6. "margin-top: 20px;"
    7. "margin-bottom: 20px}");
    To copy to clipboard, switch view to plain text mode 
    But I have problem with setting position of new scrollbar. When I use:
    Qt Code:
    1. scrollBar = new QScrollBar(textEdit);
    2. scrollBar->setStyleSheet("background-color: gray;");
    3. scrollBar->show();
    To copy to clipboard, switch view to plain text mode 
    There is small ScrollBar on the left side, which doesn't work.

    Anyone know how to put it on the right, alongside textEdit (by layoutMain edge)?

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTextEdit - margins, indents within QTextEdit

    This approach works here:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class TextEdit: public QTextEdit {
    4. Q_OBJECT
    5. public:
    6. TextEdit(QWidget *p = 0): QTextEdit(p) {
    7. setViewportMargins(30, 30, 30, 30); // margins are in pixels
    8. setStyleSheet("TextEdit { background: white; }");
    9. }
    10. };
    11.  
    12. int main(int argc, char **argv) {
    13. QApplication app(argc, argv);
    14.  
    15. const QString text(
    16. // long text omitted
    17. );
    18.  
    19. TextEdit e;
    20. e.setText(text);
    21. e.show();
    22.  
    23. return app.exec();
    24. }
    25. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    An alternate would be to retrieve the QTextDocument from the editor and setPageSize() to match the viewport size and setPageMargins() to give the space around. You would have to do it each time the viewport resizes and possibly convert between screen pixels and text document points. This would have a different effect when scrolling though.

  7. The following 2 users say thank you to ChrisW67 for this useful post:

    Acamapichtli (9th April 2014), rawfool (8th April 2014)

  8. #7
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit - margins, indents within QTextEdit

    Thank you for reply. However I have one problem using this method.

    I created a following class testdocument.h, as you showed me:
    Qt Code:
    1. #include <QtGui>
    2. #include <QTextEdit>
    3.  
    4. class TestDocument : public QTextEdit
    5. {
    6. Q_OBJECT
    7. public:
    8. TestDocument(QWidget *p = 0) : QTextEdit(p)
    9. {
    10. setViewportMargins(30,30,30,30);
    11. }
    12. };
    To copy to clipboard, switch view to plain text mode 
    Then I try to put it onto layout (TestApp.cpp):
    Qt Code:
    1. #include <TestApp.h>
    2. #include <testdocument.h>
    3.  
    4. TestApp::TestApp()
    5. {
    6. //textEdit = new QTextEdit;
    7. TestDocument doc;
    8.  
    9. layoutMain = new QVBoxLayout;
    10. layoutMain->addWidget(doc);
    11. layoutMain->setMargin(0);
    12.  
    13. window = new QWidget;
    14. window->setLayout(layoutMain);
    15. setCentralWidget(window);
    16. }
    To copy to clipboard, switch view to plain text mode 
    When I want to compile I get just one error:
    no matching function for call to 'QVBoxLayout::addWidget(TestDocument&)'
    layoutMain->addWidget(doc);
    And I don't know what I've done worng. No errors about new class, TestDocument is widget, can you point me what is wrong with this?

  9. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTextEdit - margins, indents within QTextEdit

    TestDocument is widget, can you point me what is wrong with this?
    Yes, but it should be a pointer to widget. Allocate it on the heap using "operator new" just like "window" in line 13.

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

    Acamapichtli (9th April 2014)

  11. #9
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextEdit - margins, indents within QTextEdit

    The QWidget parameter must be a pointer
    Qt Code:
    1. layoutMain->addWidget(&doc);
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to Radek for this useful post:

    Acamapichtli (9th April 2014)

  13. #10
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTextEdit - margins, indents within QTextEdit

    Qt Code:
    1. layoutMain->addWidget(&doc);
    To copy to clipboard, switch view to plain text mode 
    "doc" is gonna go out of scope very soon

  14. The following user says thank you to stampede for this useful post:

    Acamapichtli (9th April 2014)

  15. #11
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit - margins, indents within QTextEdit

    Thank you all for your contribution, now code will finally compile without errors, but unfornately doc (new TestDocument widget) won't show up.

    This is all the code I have for this app:

    TestApp.h
    Qt Code:
    1. #ifndef TESTAPP_H
    2. #define TESTAPP_H
    3. #include <QMainWindow>
    4. #include <QVBoxLayout>
    5. #include <QScrollBar>
    6. #include <QAbstractScrollArea>
    7.  
    8. class TestApp : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. TestApp();
    14.  
    15. private:
    16. QVBoxLayout *layoutMain;
    17. QWidget *window;
    18. QScrollBar *scrollBar;
    19. QAbstractScrollArea *scrollArea;
    20. };
    21. #endif // TESTAPP_H
    To copy to clipboard, switch view to plain text mode 
    TestDocument.h
    Qt Code:
    1. #ifndef TESTDOCUMENT_H
    2. #define TESTDOCUMENT_H
    3. #include <QtGui>
    4. #include <QTextEdit>
    5.  
    6. class TestDocument : public QTextEdit
    7. {
    8. Q_OBJECT
    9. public:
    10. TestDocument(QWidget *p = 0) : QTextEdit(p)
    11. {
    12. setViewportMargins(30,30,30,30);
    13. }
    14. };
    15. #endif // TESTDOCUMENT_H
    To copy to clipboard, switch view to plain text mode 
    TestApp.cpp
    Qt Code:
    1. #include <TestApp.h>
    2. #include <TestDocument.h>
    3. #include <QtGui>
    4.  
    5. TestApp::TestApp()
    6. {
    7. TestDocument doc;
    8.  
    9. layoutMain = new QVBoxLayout;
    10. layoutMain->addWidget(&doc);
    11. //layoutMain->setMargin(0);
    12.  
    13. window = new QWidget;
    14. window->setLayout(layoutMain);
    15. setCentralWidget(window);
    16. }
    To copy to clipboard, switch view to plain text mode 
    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <TestApp.h>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. TestApp test;
    9. test.show();
    10. return app.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    First I thought it was too big margins, but even if I set them all to 10, doc won't show up.
    I tried using doc.show(), I tried declaring "TestDocument *doc;" in TestApp.h and then "doc = new TestDocument" in TestApp.cpp, but neither works (second method showed some errors, so it had to be wrong idea).

  16. #12
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTextEdit - margins, indents within QTextEdit

    doc won't show up.
    because it goes out of scope and is deleted.
    TestDocument *doc;" in TestApp.h and then "doc = new TestDocument" in TestApp.cpp, but neither works (second method showed some errors, so it had to be wrong idea
    Why in TestApp.h ? Just change the declaration of doc in constructor:
    Qt Code:
    1. TestApp::TestApp()
    2. {
    3. TestDocument * doc = new TestDocument;
    4.  
    5. layoutMain = new QVBoxLayout;
    6. layoutMain->addWidget(doc);
    7. //layoutMain->setMargin(0);
    8.  
    9. window = new QWidget;
    10. window->setLayout(layoutMain);
    11. setCentralWidget(window);
    12. }
    To copy to clipboard, switch view to plain text mode 
    (second method showed some errors, so it had to be wrong idea)
    usually those errors will tell you why it is a "wrong idea"...

  17. The following user says thank you to stampede for this useful post:

    Acamapichtli (9th April 2014)

  18. #13
    Join Date
    Aug 2013
    Posts
    13
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit - margins, indents within QTextEdit

    Now I see.
    Sorry if I bored someone with my clumsy trying to achieve what I wanted, I'm just a newbie and I don't know some basic things about programming

    With your help it works now!

    Thank you all for your help.
    Last edited by Acamapichtli; 9th April 2014 at 17:50.

Similar Threads

  1. Axis margins
    By jcox23 in forum Qwt
    Replies: 1
    Last Post: 10th December 2010, 09:55
  2. Replies: 1
    Last Post: 10th June 2010, 05:35
  3. margins in QTextEdit/QPlainTextEdit
    By corrado in forum Qt Programming
    Replies: 2
    Last Post: 22nd April 2010, 09:35
  4. QToolButton margins
    By bjoernbg in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2008, 09:28
  5. margins in QSimpleRichText
    By Pan Wojtas in forum Qt Programming
    Replies: 3
    Last Post: 10th February 2006, 16:34

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.