Qt: How to give focus to a modeless QDialog created from the main window when the main window is blocked by a modal QDialog

为君一笑 提交于 2019-12-01 08:49:49

I found a working solution:

void MainWindow::TimerElapsed()
{
    QWidget *pwidget=NULL;
    m_qTimer.stop();
    foreach(pwidget,QApplication::topLevelWidgets())
    {
        if ((pwidget->isWindow())&&(pwidget->isModal()))
        {
            m_pModeless->setParent(pwidget);
        }
    }
    if (pwidget==NULL)
    {
        m_pModeless->setParent(this);
    }
    m_pModeless->show();
    m_pModeless->activateWindow();
    m_pModeless->raise();
    m_pModeless->setFocus();
}

When modal dialog to be activated (about to show) you may try change parent of modeless dialog to this modal dialog. As modeless dialog will have the modal dialog as parent then there is a chance to have it on top. Still it may also depend on platform.

Of course ideally to avoid such situations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!