Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

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

    General and Desktop
    delete ui-setupuithis push button dynamically customplugin
    3
    5
    7329
    Loading More Posts
    • 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.
    • Sh1gs
      Sh1gs last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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

        Sh1gs 1 Reply Last reply Reply Quote 2
        • Sh1gs
          Sh1gs @SGaist last edited by

          @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?

          mrjj 1 Reply Last reply Reply Quote 0
          • mrjj
            mrjj Lifetime Qt Champion @Sh1gs last edited by

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

            Sh1gs 1 Reply Last reply Reply Quote 0
            • Sh1gs
              Sh1gs @mrjj last edited by

              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 Reply Quote 1
              • First post
                Last post