Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

should QPushButton added to a QMessageBox be freed from memory?



  • Hi,

    I have the following code:

            QMessageBox* box= new QMessageBox();
    	box->setAttribute(Qt::WA_DeleteOnClose, true);
    	box->setWindowTitle(tr("msg_box"));
    	box->setText(tr("random text"));
    	QAbstractButton* btn_a= box->addButton(tr("btn_a"), QMessageBox::YesRole);
            QAbstractButton* btn_b= box->addButton(tr("btn_b"), QMessageBox::YesRole);
            QAbstractButton* btn_close = box->addButton(tr("Close"), QMessageBox::NoRole);
    

    It is not specified in the documentation if the memory allocated for the added buttons should be freed by the user that called addButton. Or maybe they are freed after the QMessageBox is deleted because adding them to the QMessageBox define them as children of the QMessageBox?

    Or should I explicitly free them from memory once finished with the QMessageBox?

    Thanks for your time.



  • @walle19 said in should QPushButton added to a QMessageBox be freed from memory?:

    Or maybe they are freed after the QMessageBox is deleted because adding them to the QMessageBox define them as children of the QMessageBox?

    Bingo! if they are sub-widgets then they are child and will be deleted with the parent


Log in to reply