Qt6 topics merged with General

horizontally Alligning dynamically created widgets qt c++

  • The following code is used by me to dynamically generate a QLabel and QLineEdit(vertically) based on a QStringList(named newList) !

    for(int i=0;i<newList.size();i++)
    QlineEdit *a=new QLineEdit();
    QLabel *b= new QLabel();



    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?

  • Lifetime Qt Champion

    "not alligned horizontally "

    You mean it almost works but the are not aligned to the top ?
    or aligned to center or what is wrong?

  • I'm not entirely certain what your problem is, but in this case, I would probably use a QGridLayout rather than two vertical layouts.

  • Qt Champions 2017

    inaddition to previous posts, what you are require is that

    1. Create Horizontal Layout. Add label and LE
    2. Add Horizontal layout in VerticalLyaout
    3. Set verticalLayout to your top window.

  • 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.

  • Moderators

    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.