How to place a QTextEdit with left and right margins in a QVBoxLayout? I could use, of course, a QHBoxLayout, place the QTextEdit into that horizontal layout in between to spacings (addSpacing(40)) and only then the horizontal layout could add into the vertical layout, but want to know if there is a direct way of doing that.
views:
55answers:
3There is
void QLayout::setContentsMargins ( int left, int top, int right, int bottom );
but this sets a margin around the whole thing. If you want margins on just the QTextEdit
and nothing else in the layout then you have to use the QHBoxLayout
approach you mentioned. I'm not aware of any other tricks to get around that.
You don't mention if you are using Qt Designer or doing this by hand in code.
In code: The QLayout class has a setContentsMargins property that you can use to set the left and right to whatever you want. There are even two flavors, one that takes left, top, right, bottom as separate arguments and one that takes a QMargins object.
Qt Designer: Just set the margins properties directly.
If you want the margins only for your QTextEdit and not any other element in the QVerticalLayout you can use css styling for that. You just need to give a name to the QTextEdit object (like "myMarginsTextEdit") and style it, eg:
QTextEdit#myMarginsTextEdit { left-margin: 40px; right-margin: 40px; }
If you are not using css to style your application you can still use it only to style that item. You can do it like this (imagine your QtextEdit variable is call "textEditItem"):
textEditItem.setStyleSheet("QTextEdit{margin-left:40px;margin-right:40px}");
The other option is use content margins in the vertical layout but then it is applied to all elements.
Hope it helps.