Trying to embed an application in my Qt app
-
Hello, I was following this thread: Qt forum thread to embed a unity app into Qt. I am using PySyde 2 so I had to made some changes.
For now, I launch the unity app but I can't embed it into the Qt app. They launch separately.
This is the code I am using:
if __name__ == '__main__': # Create unique QApplication instance app = QApplication(sys.argv) # Create a QWidget main_widget= QWidget() vl = QVBoxLayout() main_widget.setLayout(vl) # Create QProcess that runs Unity .exe unity = QProcess() unity.setProgram("C:\\Users\\UJA\\Documents\\Unity\\Ejecutable Proyecto GJ (bandera)\\Proyecto GJ.exe") unity.start() unity.waitForStarted() print("Unity id: " + unity.processId().__str__()) # Create the Unity widget that contains the Unity window UnitySceneWindow = QWindow.fromWinId(unity.processId()) unityWidget = QWidget.createWindowContainer(UnitySceneWindow, main_widget, Qt.FramelessWindowHint) unityWidget.setFocusPolicy(Qt.TabFocus) vl.addWidget(unityWidget) main_widget.show() # Start the event loop. app.exec_() # Your application won't reach here until you exit and the event # loop has stopped.
The Unity process Id is being recovering well. What can be happening?
Thanks!
-
The process id QWindow.fromWinId(unity.processId()) you are passing is not a valid Window handle. You need to transfer the top level windows ID from the process to the caller somehow (print to stdout), or iterate all top level windows (WIndows API) to find it.
-
Hi,
What if you don't give it a parent ?
By the way, you should not name your QWidget window since you are also manipulating QWindow objects. That makes your can harder to reason about.
-
The process id QWindow.fromWinId(unity.processId()) you are passing is not a valid Window handle. You need to transfer the top level windows ID from the process to the caller somehow (print to stdout), or iterate all top level windows (WIndows API) to find it.
-
Hi, thanks for your answer.
@SGaist said in Trying to embed an application in my Qt app:Hi,
What if you don't give it a parent ?
Same result
By the way, you should not name your QWidget window since you are also manipulating QWindow objects. That makes your can harder to reason about.
Your are right, I was tryting to do the same with QMainWindow, thats why I called it window but I change it right now
-
@friedemannkleint said in Trying to embed an application in my Qt app:
The process id QWindow.fromWinId(unity.processId()) you are passing is not a valid Window handle. You need to transfer the top level windows ID from the process to the caller somehow (print to stdout), or iterate all top level windows (WIndows API) to find it.
Hi, thanks for your answer.
I tried different things to get the top level windows id and finally I get it and it works!
These are the modifications from the code above.#I added this time.sleep because Unity takes a while to launch and you can't get the window (or at least I haven't been able to) until it's open. time.sleep(5) hwnd = win32gui.FindWindow(0, "Proyecto GJ") # Create the Unity widget that contains the Unity window UnitySceneWindow = QWindow.fromWinId(hwnd)
Result:
Thank you so much!
-