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. [Solved]add widget at runtime
Forum Updated to NodeBB v4.3 + New Features

[Solved]add widget at runtime

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 7.0k Views 1 Watching
  • 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.
  • T Offline
    T Offline
    toho71
    wrote on last edited by
    #1

    Hello
    I'm trying the example of Nokia where I try to loop out a groupboxes with lineedits at runtime
    When I have done my loop it's only on groupbox visible.

    @void ParseXMLExample::addPersonsToUI(QList< QMap<QString,QString> >& persons) {

    while(!persons.isEmpty()) {
                QGroupBox* personGB = new QGroupBox("Person",this);
                QFormLayout* layout = new QFormLayout;
                QMap<QString,QString> person = persons.takeFirst();
                layout->addRow("First name", new QLineEdit(person["firstname"]));
                layout->addRow("Surname", new QLineEdit(person["surname"]));
                layout->addRow("E-mail", new QLineEdit(person["email"]));
                layout->addRow("Website", new QLineEdit(person["website"]));
                personGB->setLayout(layout);
    
                //this->_layout->addWidget(personGB); //This causes error
                personGB->show();
    
    
        }
    

    }@

    whats wrong I know that it is 3 posts in the list

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      So... who manages the position of the groupbox personGB? You are currently stacking them one on top of the other, it seems to me.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        toho71
        wrote on last edited by
        #3

        It was this code who was manage the position and the _layout is a Qpointer but it causes an error

        @this->_layout->addWidget(personGB);@ //This causes error

        I dont know how to complete this code so it works again.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          "An" error is not very specific. What error does it cause?

          1 Reply Last reply
          0
          • S Offline
            S Offline
            stevenceuppens
            wrote on last edited by
            #5

            You have to make sure that the base layout/widget where you want to put this widget on,
            is setup well...

            in this line:

            @QGroupBox* personGB = new QGroupBox("Person",this);@

            you are already parenting your QGroupBox to the underlaying widget..

            try this:

            mainwindow.cpp

            @
            #include "mainwindow.h"
            #include "ui_mainwindow.h"

            #include <QGroupBox>
            #include <QFormLayout>
            #include <QLineEdit>

            MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::CQMainWindow)
            {
            ui->setupUi(this);

            vbox = new QVBoxLayout(); // var declared in headerfile as "QVBoxLayout *vbox;"
            centralWidget()->setLayout(vbox); // set the base layout on the central widget
            
            // Test Data
            QMap<QString,QString> people;
            people.insert("firstname", "Steven");
            people.insert("surname", "Ceuppens");
            
            QMap<QString,QString> people2;
            people2.insert("firstname", "Jan");
            people2.insert("surname", "Janssens");
            
            QList< QMap<QString,QString> > data;
            data << people << people2;
            
            addPersonsToUI(data);
            

            }

            MainWindow::~MainWindow()
            {
            delete ui;
            }

            void MainWindow::addPersonsToUI(QList< QMap<QString,QString> >& persons) {

            while(!persons.isEmpty()) {
                QGroupBox *personGB = new QGroupBox("Person", this);
                QFormLayout *layout = new QFormLayout;
                QMap<QString,QString> person = persons.takeFirst();
                layout->addRow("First name", new QLineEdit(person["firstname"]));
                layout->addRow("Surname", new QLineEdit(person["surname"]));
                layout->addRow("E-mail", new QLineEdit(person["email"]));
                layout->addRow("Website", new QLineEdit(person["website"]));
                personGB->setLayout(layout);
            
                vbox->addWidget(personGB);
            }
            

            }
            @

            Steven CEUPPENS
            Developer &#x2F; Architect
            Mobile: +32 479 65 93 10

            1 Reply Last reply
            0
            • T Offline
              T Offline
              toho71
              wrote on last edited by
              #6

              Thank you all, now its working fine

              1 Reply Last reply
              0

              • Login

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