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

时间:2018-03-09 06:54:33

标签: c++ qt qt4 qt5 qmessagebox

我有QMessageBox作为widget类的成员 如果我们关闭小部件消息框未关闭,则消息框保持打开并通过程序。我也为消息框

做了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();
}

1 个答案:

答案 0 :(得分:2)

QMessageBox::question()是一种静态方法,因此m_warningMsg不是显示的QMessageBox,因为您作为父参数作为参数传递给我们,我们可以找到{{1} (请注意,没有必要使用findchild()

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