how to delete dynamically created widget, when "clear" button pressed?
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?
Code:
void fileopen::on_pushButton_ADD_clicked()
{
static int LayoutCount;
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;
}
Re: how to delete dynamically created widget, when "clear" button pressed?
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:
Code:
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 }
and in your implementation:
Code:
...
void fileOpen::AddWidget{
...
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
Re: how to delete dynamically created widget, when "clear" button pressed?
Quote:
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?
Re: how to delete dynamically created widget, when "clear" button pressed?
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.
Re: how to delete dynamically created widget, when "clear" button pressed?
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.
Quote:
Originally Posted by
stefanadelbert
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.
Re: how to delete dynamically created widget, when "clear" button pressed?
Guys....would u mind to tell what i need to do now please ? i'm confused.....
without pointer means...i think its impossible...
Re: how to delete dynamically created widget, when "clear" button pressed?
Try something like that:
mainwindow.h:
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
{
Q_OBJECT
public slots:
void addRow( void );
public:
~MainWindow();
private:
};
#endif // MAINWINDOW_H
mainwindow.cpp
Code:
#include "mainwindow.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLineEdit>
MainWindow
::MainWindow(QWidget *parent
){
connect(add, SIGNAL(clicked()), this, SLOT(addRow()));
m_layout->addWidget(add);
m_layout->setAlignment(Qt::AlignTop);
w->setLayout(m_layout);
this->setCentralWidget(w);
}
MainWindow::~MainWindow()
{
}
void MainWindow::addRow( void )
{
layout->setMargin(0);
layout->addWidget(line);
layout->addWidget(clear);
layout->addWidget(remove);
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.
Re: how to delete dynamically created widget, when "clear" button pressed?
Check this out
Code:
//main.cpp
#include <QtGui>
#include "MyWizard.h"
{
Q_OBJECT
public:
{
layout->addWidget(add, layout->rowCount(), 0);
connect(add, SIGNAL(clicked()), this, SLOT(addWidgets()));
}
private slots:
void addWidgets()
{
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:
};
int main(int argc, char *argv[])
{
MyWidget w;
w.show();
return a.exec();
}
#include "main.moc"
Re: how to delete dynamically created widget, when "clear" button pressed?
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...
Code:
MainWindow
::MainWindow(QWidget *parent
) ui(new Ui::MainWindow),
{
ui->setupUi(this);
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
Re: how to delete dynamically created widget, when "clear" button pressed?
You're probably missing
Code:
#include "ui_mainwindow.h"
.