Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Trying to embed an application in my Qt app

Trying to embed an application in my Qt app

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 3 Posters 1.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JesusM97
    wrote on last edited by JesusM97
    #1

    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.
    Ejemplo.png

    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!

    1 Reply Last reply
    1
    • F Offline
      F Offline
      friedemannkleint
      wrote on last edited by
      #3

      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.

      J 1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        J 1 Reply Last reply
        0
        • F Offline
          F Offline
          friedemannkleint
          wrote on last edited by
          #3

          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.

          J 1 Reply Last reply
          3
          • SGaistS SGaist

            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.

            J Offline
            J Offline
            JesusM97
            wrote on last edited by JesusM97
            #4

            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

            1 Reply Last reply
            0
            • F friedemannkleint

              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.

              J Offline
              J Offline
              JesusM97
              wrote on last edited by JesusM97
              #5

              @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:
              result.png

              Thank you so much!

              1 Reply Last reply
              2
              • Ronel_qtmasterR Ronel_qtmaster referenced this topic on

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved