代码之家  ›  专栏  ›  技术社区  ›  Sijith

关闭父级以显式调用隐藏或关闭时,Messagebox未关闭

  •  0
  • Sijith  · 技术社区  · 8 年前

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

    // 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 回复  |  直到 8 年前
        1
  •  2
  •   eyllanesc    8 年前

    QMessageBox::question() 是静态方法吗 m_warningMsg 不是 QMessageBox 这会显示出来,因为您已经作为一个参数作为父对象传递给它,那么我们可以找到 Q消息框 (请注意,无需使用 m\u警告消息 )使用 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();
    }