A PySide2 question abot window flags

hey everyone, I recently started learning PySide2 to create better UIs in maya.
while building a tool I wanted to add a “minimize window” button to the GUI, and after some looking found the following solution:

self.setWindowFlag(QtCore.Qt.WindowMinimizeButtonHint, True)

this worked fine for me. I worked using maya 2020.
the problem is that when I tried it out in maya 2019 and below for some reason it doesn’t recognize that “setWindowFlag” flag.
so… in order to make it compatible to those versions as well and because I don’t want to altogether give up the minimize button, can someone help me out with a different way to approach it?

btw - I’m using windows

thanks in advance!

Did you set the CustomizeWindowHint flag as well? Apparently you need to.

From the Qt Docs: https://doc.qt.io/qt-5/qt.html

The CustomizeWindowHint flag is used to enable customization of the window controls. This flag must be set to allow the WindowTitleHint , WindowSystemMenuHint , WindowMinimizeButtonHint , WindowMaximizeButtonHint and WindowCloseButtonHint flags to be changed.

On a separate note: What are you doing that the minimize button needs to be manually added in the first place? I’ve always just inherited from QMainWindow QDialog or QDockWidget and Qt takes care of all that for me.

Or are you making a completely separate button other than the one in the top-right that will minimize the window?

If so, there’s apparently this method: https://doc.qt.io/qt-5/qwidget.html#setWindowState, and you pass WindowMinimized to minimize it. (this flag is also documented in that first link)

1 Like

I’m currenly using the QDialog module that for some reason doesn’t have it loaded by deafult… only the close button.
I tried the CustomizeWindowHint, but it’s also an attribute of the setWindowFlag that for some reason isn’t available in maya 2019 and below so it doesn’t really help me…
I see that the QMainWindow does have it loaded by default but it kind of breaks my tool for some reason (I think I just need to figure out how I set my main layout to it now).
anyway I guess I’ll just kind of deal with it for now and in the future just use QtDesigner that also takes care of it automatically.
thanks for the help!

I’ve never got setWindowFlag to work before. Must be a relatively new development.

Use setWindowFlags (with an ‘s’) instead.

# in the dialog class
self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowMinimizeButtonHint)

These are using bit flags, so the pipe ( | ) is a bitwise “AND” “OR” that joins multiple flags together. If you want the maximize button to work as well you would use QtCore.Qt.WindowMinMaxButtonsHint instead. The available options are found here

NOTE: In some versions of Maya you have to remind dialogs that they are windows with the Qt.Window flag because they will function as widgets with no title bar after you parent them to the maya main window. That took me a while to figure out when I started. Also, the default appearance of dialogs does not have a min or max button so I’ve gotten used to setting what I want explicitly.

On the topic of parenting dialogs to maya, you might find this useful:

from PySide2 import QtWidgets
from maya.OpenMayaUI import MQtUtil
from shiboken2 import wrapInstance

def getMayaMainWindow():
    """ Get Maya's Main Window as a QMainWindow """
    window_ptr = MQtUtil.mainWindow()
    return wrapInstance(long(window_ptr), QtWidgets.QMainWindow)

Then in your dialog class:

# parent the dialog to maya

# set up the dialog as a window with a minimize button
self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.WindowMinimizeButtonHint)

Now the dialog will be on top of Maya, close / minimize with the application, and minimize to the bottom left corner like the script editor, node editor, or other editors do.

1 Like

thank you! that solved it :slight_smile:
I wasn’t aware that " | " is “add”, that’s great to know.

1 Like