2 Attachment(s)
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:
Code:
textEdit->setStyleSheet("QTextEdit{margin-left: 30px; (...)}");
the result looks like this:
Attachment 10262
QTextEdit is resized, and scroll bar is moved with it.
I would like to achieve something like this:
Attachment 10263
Margins/indents are within QTextEdit and scrollbar is on the right just by window frame.
I tried with:
Code:
textEdit->setStyleSheet("QTextEdit{text-indent: 60px}");
but that doesn't work.
I also tried with QTextBlockFormat:
Code:
textBlockFormat = textCursor->blockFormat();
textBlockFormat.setTopMargin(50);
textBlockFormat.setLeftMargin(50);
but that also doesn't work.
Anyone can guide me how to achieve this?
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.
Re: QTextEdit - margins, indents within QTextEdit
Yes, I place QTextEdit in QVBoxLayout:
Code:
TestApp::TestApp()
{
layoutMain->addWidget(textEdit);
layoutMain->setMargin(0);
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.
Re: QTextEdit - margins, indents within QTextEdit
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).
Code:
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:
Code:
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)?
Re: QTextEdit - margins, indents within QTextEdit
This approach works here:
Code:
#include <QtGui>
Q_OBJECT
public:
setViewportMargins(30, 30, 30, 30); // margins are in pixels
setStyleSheet("TextEdit { background: white; }");
}
};
int main(int argc, char **argv) {
// 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.
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:
Code:
#include <QtGui>
#include <QTextEdit>
{
Q_OBJECT
public:
{
setViewportMargins(30,30,30,30);
}
};
Then I try to put it onto layout (TestApp.cpp):
Code:
#include <TestApp.h>
#include <testdocument.h>
TestApp::TestApp()
{
//textEdit = new QTextEdit;
TestDocument doc;
layoutMain->addWidget(doc);
layoutMain->setMargin(0);
window->setLayout(layoutMain);
setCentralWidget(window);
}
When I want to compile I get just one error:
Quote:
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?
Re: QTextEdit - margins, indents within QTextEdit
Quote:
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.
Re: QTextEdit - margins, indents within QTextEdit
The QWidget parameter must be a pointer
Code:
layoutMain->addWidget(&doc);
Re: QTextEdit - margins, indents within QTextEdit
Code:
layoutMain->addWidget(&doc);
"doc" is gonna go out of scope very soon :)
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.
http://i.imgur.com/KoW1YBh.png
This is all the code I have for this app:
TestApp.h
Code:
#ifndef TESTAPP_H
#define TESTAPP_H
#include <QMainWindow>
#include <QVBoxLayout>
#include <QScrollBar>
#include <QAbstractScrollArea>
{
Q_OBJECT
public:
TestApp();
private:
};
#endif // TESTAPP_H
TestDocument.h
Code:
#ifndef TESTDOCUMENT_H
#define TESTDOCUMENT_H
#include <QtGui>
#include <QTextEdit>
{
Q_OBJECT
public:
{
setViewportMargins(30,30,30,30);
}
};
#endif // TESTDOCUMENT_H
TestApp.cpp
Code:
#include <TestApp.h>
#include <TestDocument.h>
#include <QtGui>
TestApp::TestApp()
{
TestDocument doc;
layoutMain->addWidget(&doc);
//layoutMain->setMargin(0);
window->setLayout(layoutMain);
setCentralWidget(window);
}
main.cpp
Code:
#include <QApplication>
#include <TestApp.h>
int main(int argc, char *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).
Re: QTextEdit - margins, indents within QTextEdit
Quote:
doc won't show up.
because it goes out of scope and is deleted.
Quote:
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:
Code:
TestApp::TestApp()
{
TestDocument * doc = new TestDocument;
layoutMain->addWidget(doc);
//layoutMain->setMargin(0);
window->setLayout(layoutMain);
setCentralWidget(window);
}
Quote:
(second method showed some errors, so it had to be wrong idea)
usually those errors will tell you why it is a "wrong idea"...
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!
http://i.imgur.com/KpjaG0U.png
Thank you all for your help.