Qt: How to give focus to a modeless QDialog created from the main window when the main window is blocked by a modal QDialog(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();
}
这篇关于Qt:当主窗口被模态QDialog阻塞时,如何将焦点放在从主窗口创建的无模态QDialog上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Qt:当主窗口被模态QDialog阻塞时,如何将焦点放在从主窗口创建的无模态QDialog上
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01