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



我有QMessageBox作为小部件类的成员如果消息框保持打开状态并通过程序,如果我们关闭小部件,则消息框不会关闭。我也为消息框设置了父级

// 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() 是一个静态方法m_warningMsg所以不是显示的QMessageBox,因为您作为父级作为参数传递给它,那么我们可以找到QMessageBox(注意没有必要使用 m_warningMsg ( 使用 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();
}

最新更新