Qt::WindowStaysOnTopHint 小部件始终处于活动状态 [英] Qt::WindowStaysOnTopHint widget is always active

查看:58
本文介绍了Qt::WindowStaysOnTopHint 小部件始终处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主窗口 (QMainWindow) 和一个带有标志 Qt::WindowStaysOnTopHint 的小部件,它提供辅助信息和选项(我们称之为助手).如果我使用主窗口,然后单击任何其他应用程序,该窗口将停止活动(isActiveWindow() == false).但是,如果我先使用助手,然后切换到其他应用程序/窗口(而不是主窗口),它会保持活动状态.如何处理从帮助程序窗口到其他应用程序的切换?甚至 QApplication::activeWindow() 也因此返回 true.

I have a main window (QMainWindow) and a widget with flag Qt::WindowStaysOnTopHint that provides auxiliary information and options (let's call it helper). If I work with main window and then click on any other application this window ceases to be active (isActiveWindow() == false). But if I work with the helper first and then I switch to other application/window (not to Main Window) it stays to be active. How can I handle switching from the helper window to other application? Even QApplication::activeWindow() returns true because of this.

.h 文件:

//! Popup structure that contains listw_popup that shows tips
struct PopupWidget {
    PopupWidget(QWidget*);

    QWidget *base;
    QListWidget *listw_popup;
};

class MainWindow : public QMainWindow
{
    ...
private :
    PopupWidget popup_spec;
    ...
}

.cpp 文件:

PopupWidget::PopupWidget(QWidget* parent)
{
    base = new QWidget(parent, Qt::SplashScreen | Qt::WindowStaysOnTop);
    listw_popup = new QListWidget(base);
}

MainWindow::MainWindow(QWidget *parent) : ... popup_spec(this) ...
{
    ...
}

当 popup_spec.base.show() 被调用时,这个小部件就会出现.如果我点击这个小部件(意味着小部件变为活动状态),即使我切换到其他应用程序,它也会保持活动状态.

When popup_spec.base.show() is called this widget appears. And if I click on this widget (means the widget becomes active) it stays to be active even if I switch to other application.

推荐答案

好的,如果我正确地遵循了您的要求,

Ok so if I follow your requirements correctly,

您希望在应用程序失去焦点时隐藏辅助小部件(具有 Qt::WindowStaysOnTopHint).

You want you helper widget(which has Qt::WindowStaysOnTopHint) to be hidden when the application loses focus.

你可以试试这样的:

在您的 MainWindow.cpp 中,在构造函数中添加:

In your MainWindow.cpp say in the constructor add:

qApp->installEventFilter(this);

并在 MainWindow.cpp 中添加一个事件过滤器(不要忘记在 .h 中声明):

and add an event filter in MainWindow.cpp (don't forget to declare in .h as well):

bool MainWindow::eventFilter(QObject* object, QEvent* event) {
  if (event->type() == QEvent::ApplicationDeactivate)
    popup_spec->base->hide();
  return QWidget::eventFilter(object, event);
}

这将在切换应用程序时隐藏助手小部件.如果您希望反向功能在应用程序被激活时显示辅助小部件,请在同一事件过滤器中检查 QEvent::ApplicationActivate 并调用 popup_spec->base->show();

This will hide the helper widget when switching apps. If you want the reverse functionality to show the helper widget when application gets activated, In the same event filter check for QEvent::ApplicationActivate and call popup_spec->base->show();

这篇关于Qt::WindowStaysOnTopHint 小部件始终处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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