Important: Please read the Qt Code of Conduct -

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:

    The 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)
         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());
         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){
        num = new numpad(this);
        ... // just calculating screen geometry so it pops up above or below the IP LineEdits

    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.

  • Lifetime Qt Champion


    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 ?

  • @SGaist

    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

    and then later in my code:

    or is deleteLater still the better option?

  • Lifetime Qt Champion

    @Sh1gs said in How to properly delete a dynamically created pushbutton from a UI form?:



    It 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.

  • Thank you @mrjj

    I will go ahead and mark this as solved then. I know my IP Editor isn't exactly elegant, but with the requirements I'm given and the restrictions, I've done the best I can.