PDA

View Full Version : how to delete dynamically created widget, when "clear" button pressed?



aurora
26th October 2011, 13:07
This code will create line box and a combox when clicked on a "ADD" button, but at the same time i would like to provide "CLEAR "button, which will delete all these creations...how can i able to do that?


void fileopen::on_pushButton_ADD_clicked()
{

static int LayoutCount;

QLineEdit *lineEdit = new QLineEdit;


QLabel *label = new QLabel;
QLabel *label2 =new QLabel;
QComboBox *comboBox =new QComboBox;
comboBox->addItem("COUNT");
label->setText("ENTER SEARCH KEY");
label2->setText("UNDER THE TAG");

ui->gridLayout_3->alignment();
ui->gridLayout->addWidget( label,LayoutCount,0 );
ui->gridLayout->addWidget( lineEdit,LayoutCount,1 );
ui->gridLayout->addWidget( label2,LayoutCount,2 );
ui->gridLayout->addWidget(comboBox,LayoutCount,3);

lineEditList.append(lineEdit);
TagList.append(comboBox);

LayoutCount=LayoutCount+1;

}

nudels
26th October 2011, 16:38
hello :)

hm maybe one possible solution is, to create a "QWidget-Field" in your Class and add all Widgets u create in this Field and u need to have some pointer to move through the list to delete the Widgets.
e.g:


class myClass{
...
private:
QWidget Field[20]; // can have 20 Widgets
QWidget* fieldStart; // temp pointer to sace the startadress
QWidget* posWidget; // has the actual position of the last widget
QWidget* posToDelete;
}



and in your implementation:




...
void fileOpen::AddWidget{
...
QPushButton* button = new QPushButton();
Field[0] = *button;
posWidget++;
...
}

Maybe there is also a Widget/Class in Qt that have the described behavior ;)

i hope i could inspire you ;)

with best regards

nudels

d_stranz
26th October 2011, 17:16
This code will create line box and a combox when clicked on a "ADD" button, but at the same time i would like to provide "CLEAR "button, which will delete all these creations...how can i able to do that?

That would be a very strange design for a user interface. Are you sure you want to do that, and not just show or hide the widgets when you click the two buttons?

stefanadelbert
28th October 2011, 05:45
I agree with d_stranz. Create a composite widget that contains those editing widgets you require and then enable or unhide that composite widget when ADD is clicked. You could connect a Clear() slot on the composite widget to a clear() signal (e.g. clicked() from the clear button) that would call clear() on the individual widgets and then also hide the composite widget.

If you absolutely wanted to do it your way, you could connect the clear() signal (e.g. clicked() from the clear button) to deleteLater() on each of the created UI elements, but I REALLY don't recommend doing it that way. It doesn't seem to be the Qt way.

Also, be careful with the parenting of QObjects/QWidgets when you create them on the heap. It's best to parent the objects explicitly, e.g. QWidget* myWidget = new QWidget(someValidParent). You're passing them into addWidget functions in the case above, so the parenting should be sorted out properly. But memory management can become a problem if you don't ensure that QObjects/QWidgets are properly parented.

Spitfire
28th October 2011, 10:35
Show/Hide approach won't work when number of widgets to show is unknown/changing.
It's a common approach in this case to create new elements rather than show part of predefined set.


you could connect the clear() signal (e.g. clicked() from the clear button) to deleteLater() on each of the created UI elements
Imho that's perfectly fine and that's how I would do it. Only thing to remember here is NOT to keep any pointers to those objects.

aurora
31st October 2011, 10:56
Guys....would u mind to tell what i need to do now please ? i'm confused.....
without pointer means...i think its impossible...

Spitfire
31st October 2011, 14:26
Try something like that:

mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>

class QVBoxLayout;

class MainWindow : public QMainWindow
{
Q_OBJECT

public slots:
void addRow( void );

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:
QVBoxLayout* m_layout;
};

#endif // MAINWINDOW_H

mainwindow.cpp
#include "mainwindow.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
m_layout( new QVBoxLayout() )
{
QPushButton* add = new QPushButton("Add");
connect(add, SIGNAL(clicked()), this, SLOT(addRow()));

m_layout->addWidget(add);
m_layout->setAlignment(Qt::AlignTop);

QWidget* w = new QWidget();
w->setLayout(m_layout);

this->setCentralWidget(w);
}

MainWindow::~MainWindow()
{

}

void MainWindow::addRow( void )
{
QLineEdit* line = new QLineEdit();
QPushButton* clear = new QPushButton("Clear");
QPushButton* remove = new QPushButton("Remove");

QHBoxLayout* layout = new QHBoxLayout();
layout->setMargin(0);
layout->addWidget(line);
layout->addWidget(clear);
layout->addWidget(remove);

QWidget* w = new QWidget();
w->setLayout(layout);

connect(clear, SIGNAL(clicked()), line, SLOT(clear()));
connect(remove, SIGNAL(clicked()), w, SLOT(deleteLater()));

m_layout->addWidget(w);
}
It should work exacly as you want it.

Santosh Reddy
1st November 2011, 00:52
Check this out

//main.cpp
#include <QtGui>
#include "MyWizard.h"


class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget* parent = 0)
: QWidget(parent)
, layout(new QGridLayout(this))
, add(new QPushButton("Add Widgets", this))
{
layout->addWidget(add, layout->rowCount(), 0);
connect(add, SIGNAL(clicked()), this, SLOT(addWidgets()));
}
private slots:
void addWidgets()
{
QPushButton* clear = new QPushButton("Clear Widgets", this);
QLineEdit* lineEdit = new QLineEdit;
QLabel* label = new QLabel("ENTER SEARCH KEY");
QLabel* label2 = new QLabel("UNDER THE TAG");
QComboBox* comboBox = new QComboBox;


layout->addWidget(clear, layout->rowCount(), 0);
layout->addWidget(label, layout->rowCount() - 1, 1);
layout->addWidget(lineEdit,layout->rowCount() - 1, 2);
layout->addWidget(label2, layout->rowCount() - 1, 3);
layout->addWidget(comboBox,layout->rowCount() - 1, 4);


connect(clear, SIGNAL(clicked()), label, SLOT(deleteLater()));
connect(clear, SIGNAL(clicked()), lineEdit, SLOT(deleteLater()));
connect(clear, SIGNAL(clicked()), label2, SLOT(deleteLater()));
connect(clear, SIGNAL(clicked()), comboBox, SLOT(deleteLater()));
connect(clear, SIGNAL(clicked()), clear, SLOT(deleteLater()));
}
private:
QGridLayout* layout;
QPushButton* add;
};




int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();


return a.exec();
}


#include "main.moc"

aurora
2nd November 2011, 04:19
thank u so much.....but i wanted to include that form inside another form which is designed using "FORM DESIGNER".....
SO I changed it as follows.....but i'm getting error!!! please help me to know wher i'm going wrong...


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow),
m_layout( new QVBoxLayout() )
{
ui->setupUi(this);
QPushButton* add = new QPushButton("Add");
connect(add, SIGNAL(clicked()), this, SLOT(addRow()));

m_layout->addWidget(add);
.............
.............




After compiling, i got error......All errors are respect to my declaration..."MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow),"

error: class 'MainWindow' does not have any field named 'ui'
error: invalid use of incomplete type 'struct Ui::MainWindow'
error: forward declaration of 'struct Ui::MainWindow'
error: 'ui' was not declared in this scope

Spitfire
2nd November 2011, 10:12
You're probably missing
#include "ui_mainwindow.h".