Maya window focus and QT

Hello!

I’m working on maya tool. It’s window(QDialog) has 2 states: edit mode and locked. When in edit mode this window can be activated and accept focus, it also overrides maya hotkeys with QActions. In “locked mode” window can only accept mouse events and should not be focused or deactivate maya window.

The only approach which is working for me is to toggle WindowDoesNotAcceptFocus flag True and False by pressing the button. The problem is that changing window flag is calling hide event and window disappears. It can be then shown with show() , but you can notice this “blink” which is very annoing. I want to solve this issue in a clean way without any blinking)

I thought that I could fix it without using window flags and I tried digging into focus policies, blocking focus or activation events with custom event filter, but none of these worked for me. It looks like focus policies don’t work for qt windows in maya. Even when I use window.setFocusPolicy(Qt.NoFocus), maya window still loses focus when I click on tool window.

Any thoughts how can I achieve keep maya window active while clicking on my tool’s window without using window flags?

A bit of a shot in the dark, but would setting the window to be always on top and then setting WindowDoesNotAcceptFocus work? I realize that you’re saying the does not accept focus flag seems to be minimizing the window, but I wonder if the the always on top flag would override the minimize.

Hi, ChemiKhazi!

This window has AlwaysOnTop flag and still window disappears while setting WindowDoesNotAcceptFocus flag True or False. Reason for that (which I’ve found on some QT forum) is setWindowFlag function also calls setParent() which causes window to be hidden. There are also WindowHide and HideToParent events triggered when changing flags. That’s why I’m trying to find method without using window flags.

And I am pretty confused that window.setFocusPolicy(Qt.NoFocus) does not work as expected.

It’s hard to tell exactly what might be happening without seeing code, but some issues I often see are:

  1. Setting the tool as “WindowStaysOnTop” but never actually parenting it to the Maya MainWindow when it’s created.
  2. Setting window flags like this…
    dialog.setWindowFlags(Qt.WindowDoesNotAcceptFocus)
    These are bit flags, so the above accidentally clears any other flags that are set. Do this instead…
    dialog.setWindowFlags(dialog.windowFlags() | Qt.WindowDoesNotAcceptFocus)

Hello, JoDaRober!

That’s not a code issue, it’s how QT applies flags to the window.

I’ve tried both dialog.setWindowFlags(dialog.flags() | Qt.WindowDoesNotAcceptFocus) and dialog.setWindowFlag(Qt.WindowDoesNotAcceptFocus, True), the result is the same. Window is not visible after applying the flags. I have to call dialog.show() to make it visible again.

Here is a video of behavior I want to avoid (slowed down):
qt

So by “issue”, what I meant was that often when people ask for help and share their code, they have accidentally set window flags incorrectly.

I read through your question again to make sure I understood. Unfortunately I don’t think there’s anything you can do about this. The QApplication handles making focus changes and while you can setFocusPolicy(Qt.NoFocus) on individual widgets, the focus of the window itself is probably happening in the low-level platform window management code. Window flags are the only way to change that behavior.

And you were right about setWindowFlags() calling setParent(). This is from the Qt docs

So it appears that the only option is to do this, and live with the flicker…

def toggle_lock(window):
    window.setWindowFlags(window.windowFlags() ^ Qt.WindowDoesNotAcceptFocus)
    window.show()

So it appears that the only option is to do this, and live with the flicker…

Yeah I guess so) Thanks for your help and explanation about focus policy. I was hoping it affects windows just like any other widgets.

I did throw together a quick test to try that method and it didn’t work for me. The focus events fire when the focus state changes, but the events themselves don’t seem to be what actually triggers the state change. They are in response to it.

IDK, my test was outside of maya so it may not have been accurate to what would happen in maya, if they override any of Qt’s focus behavior. I’d be happy to find out that I’m wrong.

Also… is that an AI generated response? I kind of reads like one.

Hello, carlos!

I’ve already tried using event filter. Unfortunately it seems like you can’t block focus events for window. Only thing that worked for me is to handle focus change event and hardfocus maya window after, which looks even worse than my initial problem, because it makes maya window header to blink every time I click on my tool’s window.

How is this answer related to the question that was asked?