Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. horizontally Alligning dynamically created widgets qt c++
QtWS25 Last Chance

horizontally Alligning dynamically created widgets qt c++

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreatorqlabelqlinehorizontal allinment
6 Posts 6 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lasith
    wrote on last edited by
    #1

    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?

    kshegunovK 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Paul Thompson
        wrote on last edited by Paul Thompson
        #3

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

        1 Reply Last reply
        2
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by dheerendra
          #4

          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.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            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.

            1 Reply Last reply
            0
            • L Lasith

              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?

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              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.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              3

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved