Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
horizontally Alligning dynamically created widgets qt c++
Lasith last edited by
The following code is used by me to dynamically generate a QLabel and QLineEdit(vertically) based on a QStringList(named newList) !
QlineEdit *a=new QLineEdit();
QLabel *b= new QLabel();
ui->verticalLayout->addWidget(a); ui->verticalLayout_2->addWidget(b); b->setText(newList[i]);
Both the labels and line edits are generated and the items of the string list is depicted in the labels!But the problem I face is that label corresponding to each line edit is not alligned horizontally with that line edit! How can I correct this?
"not alligned horizontally "
You mean it almost works but the are not aligned to the top ?
or aligned to center or what is wrong?
Paul Thompson last edited by Paul Thompson
I'm not entirely certain what your problem is, but in this case, I would probably use a QGridLayout rather than two vertical layouts.
inaddition to previous posts, what you are require is that
- Create Horizontal Layout. Add label and LE
- Add Horizontal layout in VerticalLyaout
- Set verticalLayout to your top window.
JonB last edited by JonB
I know nothing about this(!), but why would you assume that 2 distinct vertical layouts would layout their elements at the same place horizontally? I would have thought you would need the label & edit control(s) laid out via some kind of horizontal layout to line up and/or both placed on the same vertical layout to achieve horizontal alignment?
EDIT: Whoops, I only just saw the previous replies, they too seem to be saying you won't get horizontal alignment just on two separate vertical layouts.
What you need in fact is a
QFormLayout(a subset of the grid layout) where you can insert the pairs of widgets with QFormLayout::addRow.