Panda in PyQt

I don’t know if this wasn’t available before, but I managed to embed a Panda window in QT simply by passing it the window handle of the container.

I wrote this small script with PyQT to test if the keyboard focus was working correctly as with wxPython on Ubuntu there are problems:

# -*- coding: utf-8-*-

from pandac.PandaModules import loadPrcFileData
loadPrcFileData("", "window-type none")

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import WindowProperties

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import sys

P3D_WIN_WIDTH = 400
P3D_WIN_HEIGHT = 240

class QTTest(QDialog):
    def __init__(self, pandaCallback, parent=None):
        super(QDialog, self).__init__(parent)
        self.setWindowTitle("Test")
        self.setGeometry(0,0,400,300)
        
        self.pandaContainer = QWidget(self)
        self.pandaContainer.setGeometry(0,0,P3D_WIN_WIDTH,P3D_WIN_HEIGHT)

        self.lineedit = QLineEdit("Write something...does it work?")
        
        layout = QVBoxLayout()
        layout.addWidget(self.pandaContainer)
        layout.addWidget(self.lineedit)
        
        self.setLayout(layout)
        
        # this basically creates an idle task
        timer =  QTimer(self)
        self.connect( timer, SIGNAL("timeout()"), pandaCallback )
        timer.start(0)

    
class World(DirectObject):   
    def __init__(self):
        self.accept("a", self.pressedA)
        self.accept("escape", sys.exit)
    
    def pressedA(self):
        print "a pressed, keyboard focus ok"
        
    def step(self):
        taskMgr.step()
    
    def bindToWindow(self, windowHandle):
        wp = WindowProperties().getDefault()
        wp.setOrigin(0,0)
        wp.setSize(P3D_WIN_WIDTH, P3D_WIN_HEIGHT)
        wp.setParentWindow(windowHandle)
        base.openDefaultWindow(props=wp)
        self.wp = wp
        
    
if __name__ == '__main__':
    world = World()

    app = QApplication(sys.argv)
    form = QTTest(world.step)
    world.bindToWindow(int(form.winId()))
    
    form.show()
    app.exec_()