【问题标题】:Messagebox not closing while closing parent for calling Hide or close explicitly关闭父级以显式调用隐藏或关闭时消息框未关闭
【发布时间】:2018-08-17 16:40:14
【问题描述】:

我有 QMessageBox 作为小部件类的成员 如果消息框保持打开并通过程序,如果我们关闭小部件,则消息框不会关闭。我也为消息框做了 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();
}

【问题讨论】:

  • 如果消息框仍然询问需要用户回答的问题,为什么要关闭小部件?
  • 后台小部件正在做一些活动,如果用户忘记提供任何输入活动将继续,两者都应该关闭

标签: c++ qt qt4 qt5 qmessagebox


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多