Qt - How to place a QTextEdit with left and right margins in a QVBoxLayout?

How to place QTextEditwith left and right margins in QVBoxLayout? I could use, of course, QHBoxLayoutto put QTextEditin this horizontal layout between intervals ( addSpacing(40)), and only then the horizontal layout can add to the vertical layout, but want to know if there is a direct way to do this.

+3
source share
3 answers

If you want to use fields only for your QTextEdit, and not for any other element in QVerticalLayout, you can use QT stylesheets for this. You just need to specify a name for the QTextEdit object (for example, "myMarginsTextEdit") and create it, for example:

QTextEdit#myMarginsTextEdit
{
    margin-left: 40px;
    margin-right: 40px;
}

If you do not use QT table styles to style your application, you can still use it only to style this element. You can do it like this (imagine your QTextEdit variable is called "textEditItem"):

textEditItem.setStyleSheet("QTextEdit {margin-left:40px; margin-right:40px}");

Another option is to use content fields in a vertical layout, but then it applies to all elements.

+5
source

Exist

void QLayout::setContentsMargins ( int left, int top, int right, int bottom );

. QTextEdit , QHBoxLayout. , .

+7

You do not mention whether you use Qt Designer or do it manually in the code.

In code: The QLayout class has the setContentsMargins property, which you can use to set the left and right 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 Constructor: Just set the field properties directly.

0
source

Source: https://habr.com/ru/post/1750299/


All Articles