关闭父级以调用隐藏或显式关闭时消息框未关闭

Messagebox not closing while closing parent for calling Hide or close explicitly

我将 QMessageBox 作为小部件的成员 class 如果消息框保持打开状态,并且如果我们关闭小部件消息框则不会关闭程序。我也为消息框设置了 setParent

// Code local to a function
reply = m_warningMsg.question(this,"Warning","Do you really want to close the connection",QMessageBox::Yes | QMessageBox::No);
if(reply == QMessageBox::No)
{
    return;
}

//Function to close the widget
void Window::closeConnection()
{
    m_warningMsg.setParent(this);
    m_warningMsg.setVisible(true);

    // Code inside if executed but not hiding messagebox
    if(m_warningMsg.isVisible())
    {
        m_warningMsg.close();
        m_warningMsg.hide();
    }
    close();
}

QMessageBox::question() is a static method so m_warningMsg is not the QMessageBox that is displayed, as you have passed as a parameter to this as a parent then we can find that QMessageBox (note that it is not necessary to use m_warningMsg) using findchild():

QMessageBox::StandardButton reply = QMessageBox::question(this,"Warning","Do you really want to close the connection",QMessageBox::Yes | QMessageBox::No);
if(reply == QMessageBox::No)
{
    return;
}

void Window::closeConnection()
{
    QMessageBox *mbox = findChild<QMessageBox*>();
    if(mbox)
        mbox->close();
    close();
}