简体   繁体   English

Qt主窗口失去焦点并意外进入背景

[英]Qt Main window loosing focus and going to background unexpectedly

Going up the learning curve and everything has been making sense so far except for this one: 上升学习曲线,到目前为止,除此以外,其他所有方面都有意义:

I have a main window from where I call one or more dialog like this: 我有一个主窗口,从那里可以调用一个或多个对话框,如下所示:

d->setWindowFlags(Qt::CustomizeWindowHint | Qt::Window | Qt::FramelessWindowHint); d-> setWindowFlags(Qt :: CustomizeWindowHint | Qt :: Window | Qt :: FramelessWindowHint);
d->setGeometry(etc... d-> setGeometry(等...
d->show(); d-> show();

The dialogs are not bound to the main window, and they are actually set to be displayed right beside the main window looking like an extension. 对话框未绑定到主窗口,实际上它们被设置为显示在主窗口旁边,看起来像是扩展。

To close the dialogs I use a mouse 'enterEvent' on the mainwindow to send a close signal to the dialog(s) which will use hide(). 要关闭对话框,我在主窗口上使用鼠标“ enterEvent”将关闭信号发送到将使用hide()的对话框。

void MainWidget::enterEvent(QEvent *event) 无效MainWidget :: enterEvent(QEvent * event)
{ {
emit signal_related_close(); 发出signal_related_close();
} }

This works but randomly this causes a very annoying issue. 这可行,但是随机地会引起一个非常烦人的问题。 When the signal is sent to close the dialogs, the main window would loose focus and go in the background (behind whatever application that is on the screen). 发送信号以关闭对话框时,主窗口将失去焦点并进入后台(在屏幕上任何应用程序的后面)。 Note that it does not minimize but only goes in the background and like I said it is random (about one out of 3 times I do it). 请注意,它不会最小化,而只会在后台运行,就像我说的那样,它是随机的(我这样做的三分之一左右)。 I can't seem to get a pattern. 我似乎找不到模式。

I initially thought the signal was messed up and instead used a dialog::leaveEvent() but that didn't help. 我最初以为信号混乱了,而是使用了一个dialog :: leaveEvent(),但这没有帮助。 I also tried using dialog::mouseMoveEvent and using that to hide the dialog but that would still randomly loose focus on the application. 我也尝试过使用dialog :: mouseMoveEvent并使用它来隐藏对话框,但这仍然会随机地将注意力集中在应用程序上。

The only method that doesn't loose focus is using a push button on each dialog that will initiate the same hide commands but that's not great for my UI. 唯一不能松开焦点的方法是在每个对话框上使用一个按钮来启动相同的隐藏命令,但这对我的UI不利。 So it would seem that using the mouse enterEvent/leaveEvent,mouseMoveEvent will cause that random issue. 因此,似乎使用鼠标enterEvent / leaveEvent,mouseMoveEvent会导致该随机问题。

Anyone experienced this issue before? 有人遇到过这个问题吗? Any idea what could be wrong? 知道有什么问题吗? Thx. 谢谢。

I can't explain why but this problem occurred when using QEvent or any mouse related event together with recursive children widgets. 我无法解释为什么,但是在使用QEvent或任何与鼠标相关的事件以及递归子代小部件时发生了此问题。 For example take this code in the parent widget constructor: 例如,在父窗口小部件构造函数中使用以下代码:

ChildWidget c1 = new ChildWidget (this);  
ChildWidget c2 = new ChildWidget (c1);
Childwidget c3 = new ChildWidget (c2);

Using QEvent in the parent widget will randomly make the window go to the background and loose focus. 在父窗口小部件中使用QEvent将使窗口随机进入背景并失去焦点。

Setting all the ChildWidget to the same parent stopped the problem. 将所有ChildWidget设置为相同的父项可以解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM