【问题标题】:How to consume QEvent::WindowBlocked so that specific window is always active?如何使用 QEvent::WindowBlocked 以便特定窗口始终处于活动状态?
【发布时间】:2018-07-19 02:54:18
【问题描述】:

我为调试目的创建了自己的简单窗口。它是完全独立的QMainWindow,因此我可以在测试应用程序时将其放在另一台显示器上。但是当我的应用程序中有一个对话框时,我无法访问该窗口。现在,正是我想要访问它的时候。

我试图像这样覆盖QWidget::event

DebugWindow.h

#include <QtWidgets/QMainWindow>
QT_BEGIN_NAMESPACE
class QEvent;
QT_END_NAMESPACE
class DebugWindow : public QMainWindow
{
    Q_OBJECT
public:
    DebugWindow(QWidget *parent = 0);
    virtual ~DebugWindow();

protected:
    virtual bool event(QEvent*);
};

DebugWindow.cpp

bool TechadminScript::event(QEvent* e)
{
    if (e->type() == QEvent::WindowBlocked) {
        // accept the event to stop propagation
        e->accept();
        return true;
    }
    return false;
}

我在被覆盖的函数中设置了断点,它被命中了——这意味着我做对了。但是窗口还是像以前一样被挡住了。

所以我可能遗漏了一些东西,有人可以帮我完成这个吗?

【问题讨论】:

  • 对话框阻塞了应用程序的事件循环。在执行返回到应用程序的事件循环之前,我认为您无法对调试窗口进行任何操作。
  • 它肯定不会阻塞事件循环——这也会停止渲染。 AFAIK,在小部件代码中运行嵌套的事件循环,直到它关闭。我们编写了类似的小部件,它们也使用事件循环,但不使用整个应用程序。
  • 如果你将它用于调试目的,你可以将它移动到单独的应用程序中,并使用 udp 而不是信号/插槽与其通信。
  • That actually worked and I used TCP 向应用发送简单的命令。但是我现在需要的东西无法在合理的时间内使用简单的 telnet 协议覆盖。
  • 尝试设置对话框的window modality

标签: c++ qt qwidget qmainwindow qevent


【解决方案1】:

按照@king_nak 的建议设置窗口模式对我很有用。请注意,根据文档,您必须事先hide()show()https://doc.qt.io/qt-5/qwidget.html#windowModality-prop

bool Object::eventFilter(QObject *, QEvent * const event)
{
  if (event->type() != QEvent::WindowBlocked)
    return false;

  for (auto&& widget : qApp->topLevelWidgets())
  {
    if (!widget->isModal())
      continue;

    widget->hide();
    widget->setWindowModality(Qt::NonModal);
    widget->show();
  }

  return true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 2021-01-30
    相关资源
    最近更新 更多