How to change alignment of widget
-
Hey everyone,
I have a widget name textedit and I want the textedit in the middle of the Window
So I have a question that how can we align widget in qt as I found that we can QLayout::setalignment(textedit,Qt::AlignCenter) but as QLayout is a Absract class I can't use it so can anyone help.
Thanks in advance -
@UG-SEP said in How to change alignment of widget:
as QLayout is a Absract class
You can use any of its subclasess:
QVBoxLayout
/QHBoxLayout
/QGridLayout
/etc -
It's doesn't work
Here is the piece of codeui -> textEdit -> resize(216*3.8,this->height()); QVBoxLayout layout; layout.setAlignment(ui->textEdit,Qt::AlignCenter);
I first resize the TextEdit and then try to center align the widget but I did show any changes...
-
@UG-SEP
Hi
You have to assign the layout to the widget holding the LineEdit.
Often that is the form. or some other widget.
Then the layout offers adjustment of the widgets it holds in relation to its parent.So right-click beside the LineEdit (on the form / window) and use the Layout menu to assign a layout.
Then you can adjust layouts setting via its properties.If you have other widgets on the form, you might want to use the red layouts in the left side (where the widgets are ) to insert a sub layout to the main layout so you sort of creates a section that can have other options.
Here i Rightclicked the centralwidget and use the layout menu
then i dragged red layout to this layout and then dragged the lineEdit to the sub layout -
Yes. That is normal. they have to be linked to the central so they scale with window.
If you don't add layout to central. the red layout will just float around and nnot react to you scaling the window.The normal way to design forms is to have all widgets under the control of a layout. Having them float
around is not going to work good across multi resolutions etc. -
Odd. Mine will center just fine.
Are you sure the TextEdit is inside the layout ?
Mine looks far more indented than yours so i think maybe TextEdit is not inside
If you resize the red layout it should also move around-
-
I try some advanced in this
I added a button and when it's pressed then
I resize TextEdit and then and apply layout.set alignment(ui->textedit,Qt::aligncenter);
but i textedit is not at center
Code:ui->textEdit->resize(32,54); QHBoxLayout layout; layout.setAlignment(ui->textEdit,Qt::AlignCenter);
Image of ui:
-
Hi
It's good you play around to learn the layout. Best way to learn.The Qt::AlignCenter only works if the widget is not using all the space of the layout which is the normal
setting. So here it will use all space and alignment won't really make sense.Is the goal to have space around it?
or why are you so dedicated to it must be center if its the size of the area anyway? -
Ok so it's like a page preview ? as a real A4 / A3 would be waaay bigger than the layout.
You can control its size in a layout by calling
ui->textXx->setMaximumSize(100,100);
to not allow it to grow to full size of layout.
-
@UG-SEP said in How to change alignment of widget:
ui->textEdit->resize(32,54);
QHBoxLayout layout;
layout.setAlignment(ui->textEdit,Qt::AlignCenter);ui->textEdit->resize(32,54);
auto layout = new QHBoxLayout( this ); /* do not do: QHBoxLayout layout; which may cause crash */
layout->setAlignment(ui->textEdit,Qt::AlignCenter); -
@UG-SEP
Hi
Well there is
https://doc.qt.io/qt-5/qtextdocument.html#pageSize-prop
(TextEdit uses this for the page etc)Concrete sizes like A4/A3 is more when you print and QPrinter does know them/support it.
Its a paper sizeHowever, you can set the page size of the Document to the size of A4 or what ever you want.
So if the goal is to resize the TextEdit so it be the size of a A4. then using layout seems a bit odd
as it cant be bigger than the layout and a layout cant scroll.So iM not 100ยค sure what you are trying to do.
Seems you try to make a really small page so that confuses me :)