Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to properly delete a dynamically created pushbutton from a UI form?
QtWS25 Last Chance

How to properly delete a dynamically created pushbutton from a UI form?

Scheduled Pinned Locked Moved Solved General and Desktop
deleteui-setupuithispush buttondynamicallycustomplugin
5 Posts 3 Posters 8.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Sh1gsS Offline
    Sh1gsS Offline
    Sh1gs
    wrote on last edited by
    #1

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

    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)
    {
         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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Sh1gsS 1 Reply Last reply
      2
      • SGaistS SGaist

        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 ?

        Sh1gsS Offline
        Sh1gsS Offline
        Sh1gs
        wrote on last edited by
        #3

        @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
        num->setAttribute(Qt::WA_DeleteOnClose);

        and then later in my code:
        num->Close();

        or is deleteLater still the better option?

        mrjjM 1 Reply Last reply
        0
        • Sh1gsS Sh1gs

          @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
          num->setAttribute(Qt::WA_DeleteOnClose);

          and then later in my code:
          num->Close();

          or is deleteLater still the better option?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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#close

          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.

          Sh1gsS 1 Reply Last reply
          0
          • mrjjM mrjj

            @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#close

            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.

            Sh1gsS Offline
            Sh1gsS Offline
            Sh1gs
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved