[Solved]add widget at runtime
-
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
-
So... who manages the position of the groupbox personGB? You are currently stacking them one on top of the other, it seems to me.
-
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.
-
"An" error is not very specific. What error does it cause?
-
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); }
}
@ -
Thank you all, now its working fine