PDA

View Full Version : QTextEdit - margins, indents within QTextEdit



Acamapichtli
5th April 2014, 17:41
I want to have possibility of changing margins/indents in QTextEdit, without resizing QTextEdit.

What I mean is that when I use stylesheets:

textEdit->setStyleSheet("QTextEdit{margin-left: 30px; (...)}");
the result looks like this:
10262
QTextEdit is resized, and scroll bar is moved with it.

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

I tried with:

textEdit->setStyleSheet("QTextEdit{text-indent: 60px}"); but that doesn't work.

I also tried with QTextBlockFormat:

textCursor = new QTextCursor(textEdit->textCursor());
textBlockFormat = textCursor->blockFormat();
textBlockFormat.setTopMargin(50);
textBlockFormat.setLeftMargin(50); but that also doesn't work.

Anyone can guide me how to achieve this?

rawfool
7th April 2014, 08:08
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.

Acamapichtli
7th April 2014, 14:59
Yes, I place QTextEdit in QVBoxLayout:

TestApp::TestApp()
{
textEdit = new QTextEdit;

layoutMain = new QVBoxLayout;
layoutMain->addWidget(textEdit);
layoutMain->setMargin(0);

window = new QWidget;
window->setLayout(layoutMain);
setCentralWidget(window);
}
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.

Radek
7th April 2014, 18:25
Perhaps, this could help:

http://stackoverflow.com/questions/3053667/qt-how-to-place-a-qtextedit-with-left-and-right-margins-in-a-qvboxlayout

Acamapichtli
7th April 2014, 19:06
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).

textEdit->setFrameStyle(QFrame::NoFrame);
textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
textEdit->setStyleSheet("QTextEdit"
"{margin-left: 40px;"
"margin-right: 40px;"
"margin-top: 20px;"
"margin-bottom: 20px}");
But I have problem with setting position of new scrollbar. When I use:

scrollBar = new QScrollBar(textEdit);
scrollBar->setStyleSheet("background-color: gray;");
scrollBar->show();
There is small ScrollBar on the left side, which doesn't work.
http://i.imgur.com/1juK3eH.png
Anyone know how to put it on the right, alongside textEdit (by layoutMain edge)?

ChrisW67
8th April 2014, 02:07
This approach works here:


#include <QtGui>

class TextEdit: public QTextEdit {
Q_OBJECT
public:
TextEdit(QWidget *p = 0): QTextEdit(p) {
setViewportMargins(30, 30, 30, 30); // margins are in pixels
setStyleSheet("TextEdit { background: white; }");
}
};

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

const QString text(
// long text omitted
);

TextEdit e;
e.setText(text);
e.show();

return app.exec();
}
#include "main.moc"


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.

Acamapichtli
9th April 2014, 15:44
Thank you for reply. However I have one problem using this method.

I created a following class testdocument.h, as you showed me:

#include <QtGui>
#include <QTextEdit>

class TestDocument : public QTextEdit
{
Q_OBJECT
public:
TestDocument(QWidget *p = 0) : QTextEdit(p)
{
setViewportMargins(30,30,30,30);
}
};Then I try to put it onto layout (TestApp.cpp):

#include <TestApp.h>
#include <testdocument.h>

TestApp::TestApp()
{
//textEdit = new QTextEdit;
TestDocument doc;

layoutMain = new QVBoxLayout;
layoutMain->addWidget(doc);
layoutMain->setMargin(0);

window = new QWidget;
window->setLayout(layoutMain);
setCentralWidget(window);
}

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?

stampede
9th April 2014, 16:14
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.

Radek
9th April 2014, 16:16
The QWidget parameter must be a pointer


layoutMain->addWidget(&doc);

stampede
9th April 2014, 16:18
layoutMain->addWidget(&doc);
"doc" is gonna go out of scope very soon :)

Acamapichtli
9th April 2014, 17:04
Thank you all for your contribution, now code will finally compile without errors, but unfornately doc (new TestDocument widget) won't show up.
http://i.imgur.com/KoW1YBh.png
This is all the code I have for this app:

TestApp.h

#ifndef TESTAPP_H
#define TESTAPP_H
#include <QMainWindow>
#include <QVBoxLayout>
#include <QScrollBar>
#include <QAbstractScrollArea>

class TestApp : public QMainWindow
{
Q_OBJECT

public:
TestApp();

private:
QVBoxLayout *layoutMain;
QWidget *window;
QScrollBar *scrollBar;
QAbstractScrollArea *scrollArea;
};
#endif // TESTAPP_H

TestDocument.h

#ifndef TESTDOCUMENT_H
#define TESTDOCUMENT_H
#include <QtGui>
#include <QTextEdit>

class TestDocument : public QTextEdit
{
Q_OBJECT
public:
TestDocument(QWidget *p = 0) : QTextEdit(p)
{
setViewportMargins(30,30,30,30);
}
};
#endif // TESTDOCUMENT_H

TestApp.cpp

#include <TestApp.h>
#include <TestDocument.h>
#include <QtGui>

TestApp::TestApp()
{
TestDocument doc;

layoutMain = new QVBoxLayout;
layoutMain->addWidget(&doc);
//layoutMain->setMargin(0);

window = new QWidget;
window->setLayout(layoutMain);
setCentralWidget(window);
}
main.cpp

#include <QApplication>
#include <TestApp.h>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

TestApp test;
test.show();
return app.exec();
}

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).

stampede
9th April 2014, 17:17
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:


TestApp::TestApp()
{
TestDocument * doc = new TestDocument;

layoutMain = new QVBoxLayout;
layoutMain->addWidget(doc);
//layoutMain->setMargin(0);

window = new QWidget;
window->setLayout(layoutMain);
setCentralWidget(window);
}



(second method showed some errors, so it had to be wrong idea)

usually those errors will tell you why it is a "wrong idea"...

Acamapichtli
9th April 2014, 17:37
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!
http://i.imgur.com/KpjaG0U.png
Thank you all for your help.