How to properly delete a dynamically created pushbutton from a UI form?
-
Hello,
I will try to be as descriptive as possible as I am limited to what code I can post due to the nature of my job. I have created an IP Editor custom plugin that is made with 4 LineEdits. This is so the user can input/change each input mask individually. When a LineEdit hasfocus, a numberpad will popup on the screen. This numberpad is a separate class that I created using Qt Designer. The numberpad.ui consists of a QFrame - numpad.
Now, the numpad QFrame is the only thing that is clicked/dragged into the ui design form. Within the numberpad class however, I am creating QPushButtons using code so that they are added dynamically when the application starts.
My entire program contains the following files:
ipeditor.cpp
ipeditor.h
ipeditor.ui
numberpad.h
numberpad.cpp
numberpad.uiThe numberpad.cpp file contains code similar to this (this isn't exact because I can't actually copy/paste my code into here)
numberpad::numberpad(QWidget *parent) : QWidget(parent) { ui->setup(this); createNumPad(); } numberpad::createNumPad() { QPushButton *seven = new QPushButton(ui->numpad); .... connect(seven, SIGNAL(clicked()), this, SLOT(btnClicked())); } void numberpad::btnClicked() { QPushButton *btn = qobject_cast<QPushButton*>(sender()); emit sendData(btn->text()); } numberpad::~numberpad() { delete ui; }
Now in my ipeditor.cpp file, I am creating an instance of the numberpad whenever a LineEdit has focus
bool ipeditor::eventFilter(QObject *obj, QEvent *e) { if(e->type == QEvent::FocusIn){ popupNumPad(); } ipeditor::popupNumPad() { num = new numpad(this); ... // just calculating screen geometry so it pops up above or below the IP LineEdits num->show; }
Now, when the user clicks on a LineEdit, I want to delete the current instance of the numpad, and create a new one.
delete num;
By doing this, I figured that it would delete the popup number pad in it's entirety. However, when I added debug statements like
qDebug() num->isVisible();
even after "deleting" it still returns true.Am I not deleting num properly? Do I need to add more code within my numberpad.cpp in the destructor because I dynamically created the PushButtons rather than using the design form?
I apologize if this seems jumbled, please let me know if I need to clarify more things.
-
Hi,
You should rather use
deleteLater
. The next iteration of the event loop will take care of deleting the widget.By the way, why do you delete your numpad object each time ?
-
I delete it each time because it is a requirement bestowed upon upper management in my job. It's viewed as a memory leak otherwise, since a new instance is created each time a LineEdit gains focus. So essentially the first LineEdit gets focus, a numberpad pops up, a second LineEdit gains focus which then deletes the first instance of numberpad and creates a new instance. I don't have a whole lot of leeway with how I can do certain things.
Also, could I do
num->setAttribute(Qt::WA_DeleteOnClose);
and then later in my code:
num->Close();
or is
deleteLater
still the better option? -
@Sh1gs said in How to properly delete a dynamically created pushbutton from a UI form?:
Qt::WA_DeleteOnClose
Hi
http://doc.qt.io/qt-5/qwidget.html#closeIt should be just as good as deleteLater if you are sure that close are called before a new button is created and you do not call delete on the button.
setAttribute(Qt::WA_DeleteOnClose); is normally used on dialogs/windows but I guess nothing bad comes using it with a Widget.