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();

      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?


  • Qt Champions 2016

    Hi
    "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.



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


  • Qt Champions 2016

    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.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.