Qt:当主窗口被模态QDialog阻塞时,如何将焦点放在从主窗口创建的无模态QDialog上
在我的 Qt 应用程序中,我面临以下情况:当引发特定事件时,我会显示一个无模式的 QDialog
,它要求用户进行确认.使用 QMainWindow
中的 show()
函数显示对话框.每当引发事件并且没有显示其他模式 QDialog
时,用户都可以单击确认按钮.不幸的是,如果在引发事件时模态 QDialog
可见,则无法访问非模态 QDialog
.这意味着用户不能点击确认按钮.以下代码是导致相同问题的简化版本在此示例中,QMainWindow
包含一个按钮,当单击该按钮时,将使用 exec()
函数显示模态 QDialog
,同时QTimer
已启动.每当我在 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).
主窗口代码:
#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();
}
主窗口标题:
#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();
}
相关文章