我的项目中有一个Window
子类,在运行时该实例被创建并完全显示在 QML 端。我知道我可以通过不包含 来防止窗口被最小化WindowMinimizeButtonHint
,flags:
但我实际上需要存在并启用最小化按钮,但能够拦截最小化按钮单击,取消实际最小化,并执行其他操作(仅供参考,我的客户需要这种非标准的窗口行为,而不是我)。
到目前为止,我唯一能够实现的就是处理onWindowStateChanged:
事件,检查是否windowState === Qt.WindowStateMinimized
并show()
从计时器调用(在事件处理程序中直接调用它什么都不做)。这导致窗口向下移动到系统托盘,然后突然恢复正常。
有没有办法做到这一点,比如OnMinimized
可以取消的活动?
编辑:根据 Benjamin T 的回答,我至少是 OSX 解决方案的一部分:
#import <AppKit/AppKit.h>
bool NativeFilter::nativeEventFilter(const QByteArray &eventType,
void *message, long *result)
{
if (eventType == "mac_generic_NSEvent") {
NSEvent *event = static_cast<NSEvent *>(message);
if ([event type] == NSKeyDown) {
return true;
}
}
return false;
}
在这个例子中,我能够拦截和取消所有 NSKeyDown 事件(同时让其他事件如鼠标点击等仍然有效)。剩下的问题是我仍然不知道要拦截最小化事件- NSEvent.h 似乎没有任何内容。也许我需要转换到不同类型的事件?
编辑 2 - 工作解决方案:
我无法找到任何方法来正确拦截最小化事件并取消它,所以我的解决方法是拦截窗口上的点击,确定点击是否在最小化按钮(或关闭或缩放按钮)上并取消如果是这样的事件(并向我的 qml 窗口发送点击发生的通知)。我还处理了双击标题栏以缩放窗口并使用 Command-M 键最小化窗口的情况。
第一步是实现一个QAbstractNativeEventFilter
. 在您的标题中:
#include <QAbstractNativeEventFilter>
class NativeFilter : public QAbstractNativeEventFilter {
public:
bool nativeEventFilter(const QByteArray &eventType, void *message,
long *result);
};
实施:
#import <AppKit/AppKit.h>
#import <AppKit/NSWindow.h>
#import <AppKit/NSButton.h>
bool NativeFilter::nativeEventFilter(const QByteArray &eventType, void
*message, long *result)
{
if (eventType == "mac_generic_NSEvent") {
NSEvent *event = static_cast<NSEvent *>(message);
NSWindow *win = [event window];
// TODO: determine whether or not this is a window whose
// events you want to intercept. I did this by checking
// [win title] but you may want to find and use the
// window's id instead.
// Detect a double-click on the titlebar. If the zoom button
// is enabled, send the full-screen message to the window
if ([event type] == NSLeftMouseUp) {
if ([event clickCount] > 1) {
NSPoint pt = [event locationInWindow];
CGRect rect = [win frame];
// event coordinates have y going in the opposite direction from frame coordinates, very annoying
CGFloat yInverted = rect.size.height - pt.y;
if (yInverted <= 20) {
// TODO: need the proper metrics for the height of the title bar
NSButton *btn = [win standardWindowButton:NSWindowZoomButton];
if (btn.enabled) {
// notify qml of zoom button click
}
return true;
}
}
}
if ([event type] == NSKeyDown) {
// detect command-M (for minimize app)
if ([event modifierFlags] & NSCommandKeyMask) {
// M key
if ([event keyCode] == 46) {
// notify qml of miniaturize button click
return true;
}
}
// TODO: we may be requested to handle keyboard actions for close and zoom buttons. e.g. ctrl-cmd-F is zoom, I think,
// and Command-H is hide.
}
if ([event type] == NSLeftMouseDown) {
NSPoint pt = [event locationInWindow];
CGRect rect = [win frame];
// event coordinates have y going in the opposite direction from frame coordinates, very annoying
CGFloat yInverted = rect.size.height - pt.y;
NSButton *btn = [win standardWindowButton:NSWindowMiniaturizeButton];
CGRect rectButton = [btn frame];
if ((yInverted >= rectButton.origin.y) && (yInverted <= (rectButton.origin.y + rectButton.size.height))) {
if ((pt.x >= rectButton.origin.x) && (pt.x <= (rectButton.origin.x + rectButton.size.width))) {
// notify .qml of miniaturize button click
return true;
}
}
btn = [win standardWindowButton:NSWindowZoomButton];
rectButton = [btn frame];
if (btn.enabled) {
if ((yInverted >= rectButton.origin.y) && (yInverted <= (rectButton.origin.y + rectButton.size.height))) {
if ((pt.x >= rectButton.origin.x) && (pt.x <= (rectButton.origin.x + rectButton.size.width))) {
// notify qml of zoom button click
return true;
}
}
}
btn = [win standardWindowButton:NSWindowCloseButton];
rectButton = [btn frame];
if ((yInverted >= rectButton.origin.y) && (yInverted <= (rectButton.origin.y + rectButton.size.height))) {
if ((pt.x >= rectButton.origin.x) && (pt.x <= (rectButton.origin.x + rectButton.size.width))) {
// notify qml of close button click
return true;
}
}
}
return false;
}
return false;
}
然后在 main.cpp 中:
Application app(argc, argv);
app.installNativeEventFilter(new NativeFilter());