How to get the main window of the editor, to parent Qt or PySide application to it?

Hey :slight_smile:

is there a way to get the main editor window (or any other editor window) with Python, to use it as a parent for a Qt or PySide application?
I just started looking into the Shotgun integration code, but couldn’t find a solution.

Thank you in advance for your help and time!

I found a solution and will answer it here. Hopefully it will be useful for some people.

There is a python function called


unreal.parent_external_window_to_slate()

with this function, you can parent any window to the unreal window. We used it to parent our pipeline (PyQt) Qt widgets to Unreal.

After parenting external windows to Unreal, they will automatically minimize/ maximize and shut down with Unreal. Furthermore they will be displayed above the Unreal window, on which you parented it.

You get the window Id from (PyQt) with the following function:


YourWidget.winId()

Just use the resulting Id as input of the unreal function:


unreal.parent_external_window_to_slate(YourWidget.winId())

2 Likes

This is really great and useful info – thanks for posting it so everyone can see!

This was great, thank you for the insight.

I do have a follow up question:
How do you send commands from the connected Qt window to Unreal?
The UI still evaluates using the external python exe you used to create it with.

Thank you for the help!

Hi Elias

Since UE4 4.26 update, in python3 , unreal.parent_external_window_to_slate() function is not working anymore. I guess, You might have same situation.

Do you have any solution for this?

Thank you!

1 Like

Hi

long time ago … :smiley:
Haven’t used Python for Unreal for a while now. But I noticed, that there is now a new description for the function and it takes 2 arguments now (not sure if this is new or if it was not documented in 2018)

This is the new function

unreal.parent_external_window_to_slate(external_window, parent_search_method=SlateParentWindowSearchMethod.ACTIVE_WINDOW)

I don’t have a proper setup for testing right now, but maybe it will work with the MAIN_WINDOW as second argument?

All best

Yes this worked. The exact syntax is -
unreal.parent_external_window_to_slate(widget.winId(), unreal.SlateParentWindowSearchMethod.ACTIVE_WINDOW)

Thank you @Elias_Kremer

1 Like

Hello, is this still valid for Unreal Engine 5? I tried this:

unreal.parent_external_window_to_slate(window.winId(), unreal.SlateParentWindowSearchMethod.ACTIVE_WINDOW)
print(window.parent())

when printing window parent → returns None
So after this the widget disappears, because there is no reference for it and garbage collector deletes it.

To partially fix this I can store my window widget into global variable, but that is not the best approach.

global ui
ui = window

Also it seems when storing widget into variable and then deleting it:

window.setParent(None)
window.close()
window.deleteLater()

It still stays in memory ( calling qt_app.allWidgets() returns increasing number of widgets). I know this has something to do with the fact that I am not calling qt_app.exec() ( this will make main loop for my window and freeze unreal which is unwanted behavior). Does anyone know how to remove widgets from the memory in unreal, or what will be the general approach?

1 Like

I’m interested in this too, but I would prefer a Native Gui solution in unreal-python.

What I do at the moment:

def tool_function_called_in_a_button():
    app     = QtWidgets.QApplication.instance() or QtWidgets.QApplication( sys.argv )  # unreal crashes without this
    
    global window   # window disappears without this
    window  = QtWidgets.QWidget()  # crash without app line
    window.show()
    unreal.parent_external_window_to_slate( window.winId() )    # attach to unreal, keep it on top of main window.
1 Like

after a lot of research, I was unable to properly Delete the object from memory.
Like you said already, best thing I could do was to do widget.close()
which is hiding the widget but not deleting it.