Pyside2 Parent Window to Main Window


I’ve been trying to parent a window to another window and have found that whenever I try to parent them the child window stops showing. To simplify the problem I tried to parent the window to the main maya window but had the same result.

If we execute the following code no window will appear and both prints will show the same directory. Therefore the widget’s parent is the main maya application. If you remove “widget” from line 12 the window will show but it will no longer be parented to the main maya window.

My real intention is create secondary windows that will not hide under the main maya window. Right now all my windows come out as a separate main window and will hide whenever we click the main window. I tried setting self.setWindowFlags(core.Qt.WindowStaysOnTopHint) but that stays on top of every window on the computer which is not really the desired result. Any help would be greatly appreciated!

from maya import cmds
from maya import OpenMayaUI as omui 
from PySide2.QtCore import * 
from PySide2.QtGui import *
from PySide2 import QtWidgets as wdg
from shiboken2 import wrapInstance 
win = None
def init():
    global win
    ptr = omui.MQtUtil.mainWindow()    
    widget = wrapInstance(long(ptr), wdg.QWidget)
    win = bikes_LightingTool(widget)
    print widget

class bikes_LightingTool(wdg.QWidget):
    def __init__(self, parent=None):
        super(bikes_LightingTool, self).__init__(parent)
        self.parent_widget = parent
        lay = wdg.QHBoxLayout(self)
        print self.parent()


For some reason you have to add this flag:



Hi haggi!
Thanks for the reply.
This solution does show the window but by doing so it loses its relationship with the parent window. I believe the QtCore.Qt.Window flag converts the specified window into a primary window, and therefore loses all references to its parents?
You can check with my code. After adding your suggestion the window shows up but you get printed <None> and <PySide2 reference> instead of the same reference. This is exactly the same as directly not passing the main window as a parent and not adding the flag…
In all honesty to me it’s not such a big deal but my artists keep complaining that they lose the window while working. Does anybody know how to keep the windows on top of the main maya window the same way the script editor works?


This is my code:

import as MayaMixin 
import maya.OpenMaya as om 
import maya.OpenMayaUI 
import PySide2.QtCore as QtCore 
import PySide2.QtGui as QtGui 
import PySide2.QtWidgets as QtWid 
from shiboken2 import wrapInstance 
def getMainWindowPtr(): 
    mayaMainWindowPtr = maya.OpenMayaUI.MQtUtil.mainWindow() 
    mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QtWid.QWidget) 
    print mayaMainWindow 
    return mayaMainWindow 
class W(QtWid.QWidget): 
    def __init__(self, parent=None): 
        super(W, self).__init__(parent=parent) 
        l = QtWid.QVBoxLayout(self) 
        print self.parent() 
w = W(parent=getMainWindowPtr())

And the result is:

<PySide2.QtWidgets.QWidget object at 0x00000000524A1748> 
<PySide2.QtWidgets.QWidget object at 0x00000000524A1748>

And the window is parented to the maya main window and does not loose the parent relationship. I used Maya2018 to test it.


Thank you very much!!
The first time I looked at your code I was like whyyyyyy it’s exactly the same!!! but then I looked more carefully and noticed that I was calling
W(getMainWindowPtr()) instead of W(parent=getMainWindowPtr())
so the main window pointer was becoming self instead of parent!! I can’t believe I didn’t noticed such a silly mistake!
Thank you again for everything


Hmmm… that’s interesting. The script behaves exactly the same here with or without using the parent keyword.


Aaaaaand… you’re correct again! I’m not sure what was wrong with me last week. You only need to add the windowFlag for the window to show. no need for nothing else… thank you again for everything!