Qt:当主窗口被模式QDialog阻塞时,如何将焦点放在从主窗口创建的无模式QDialog上 [英] Qt: How to give focus to a modeless QDialog created from the main window when the main window is blocked by a modal QDialog

查看:673
本文介绍了Qt:当主窗口被模式QDialog阻塞时,如何将焦点放在从主窗口创建的无模式QDialog上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Qt应用程序中,我面临以下情况: 引发特定事件时,我会显示一个无模式的QDialog,要求用户进行确认. 使用QMainWindow中的show()功能显示该对话框. 每当引发事件且未显示其他模态QDialog时,用户都可以单击确认按钮. 不幸的是,如果引发事件时模态QDialog可见,则无模态QDialog将不可访问.这意味着用户无法单击确认按钮. 以下代码是导致相同问题的简化版本 在此示例中,QMainWindow包含一个按钮,单击该按钮时,在启动QTimer的同时,使用exec()功能显示模式QDialog. 每当我在QTimer结束之前关闭模式QDialog时,都可以使用无模式对话框.如果我等到显示无模式对话框而没有关闭模式对话框,则无法访问无模式对话框(我需要先关闭模式对话框).

In my Qt Application I'm facing the following scenario: When a specific event is raised I show a modeless QDialog which asks the user for a confirmation. The Dialog is showed using show() function from a QMainWindow. Anytime the event is raised and no other modal QDialog are shown, the user is able to click the confirmation button. Unfortunately if a modal QDialog is visible when the event is raised, the modeless QDialog is unaccessible. This means that the user cannot click the confirmation button. The following code is a simplified version that causes the same problem In this example the QMainWindow contains a button, when the button is clicked a modal QDialog is shown using the exec() function in the meanwhile a QTimer has been started. Anytime I close the modal QDialog before the QTimer is elapsed the modeless dialog is accesible. If I wait until the modeless dialog is shown without closing the modal one, the modeless dialog is inaccessible (I need to close the modal one first).

MainWindows代码:

MainWindows code:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_pModeless = new DialogModal(this);
    connect(&m_qTimer,SIGNAL(timeout()),this,SLOT(TimerElapsed()));

}

MainWindow::~MainWindow()
{
    delete m_pModeless;
    delete ui;
} 

void MainWindow::TimerElapsed()
{
    m_qTimer.stop();
    m_pModeless->show();
    m_pModeless->activateWindow();
    m_pModeless->raise();
    m_pModeless->setFocus();
}

void MainWindow::on_pbStartTest_clicked()
{
    m_qTimer.start(10000);
    DialogModal d(this);
    d.exec();
}

MainWindow标头:

MainWindow Header:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTimer>
#include "dialogmodal.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QTimer m_qTimer;
    DialogModal *m_pModeless;
private:
    Ui::MainWindow *ui;
private slots:
    void TimerElapsed();
    void on_pbStartTest_clicked();
};

#endif // MAINWINDOW_H

DialogModal标头:

DialogModal Header:

#ifndef DIALOGMODAL_H
#define DIALOGMODAL_H

#include <QDialog>

namespace Ui {
class DialogModal;
}

class DialogModal : public QDialog
{
    Q_OBJECT

public:
    explicit DialogModal(QWidget *parent = 0);
    ~DialogModal();

private slots:
    void on_pbExit_clicked();

private:
    Ui::DialogModal *ui;
};

#endif // DIALOGMODAL_H

DialogModal来源:

DialogModal source:

#include "dialogmodal.h"
#include "ui_dialogmodal.h"

DialogModal::DialogModal(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogModal)
{
    ui->setupUi(this);
}

DialogModal::~DialogModal()
{
    delete ui;
}

void DialogModal::on_pbExit_clicked()
{
    close();
}

即使存在一个或多个模态对话框,是否仍然可以将焦点放在无模对话框上?

Is there anyway to give focus to the modeless dialog even when one or more modal dialog are present?

推荐答案

我找到了可行的解决方案:

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

这篇关于Qt:当主窗口被模式QDialog阻塞时,如何将焦点放在从主窗口创建的无模式QDialog上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆