1

I have a function called:

bool game_init(void* window);

takes a pointer to window handle,basically it just initialize opengl, and do rendering from c++(I export this function to python using boost::python),now I created a simple hello word window,to try it.

app = QtWidgets.QApplication(["",""])
window = QtWidgets.QMainWindow()
winid = window.winId()
ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object, ctypes.c_char_p]
handle = ctypes.pythonapi.PyCapsule_GetPointer(winid.ascapsule() , None)

Interesting that All I did the handle is just an integer something like 85983252 which I guess it is the handle of the hello world window,but how do I pass this integer as void * so that my c++ code can create an opengl window using that? Currently the error gives me: python parameter bool game_init(int); does not match the cpp code.

environment:
Linux,pyqt5 boost::python gcc,python 3.4
user1051003
  • 1,211
  • 5
  • 16
  • 24

3 Answers3

1

winId() returns sip.voidptr object. To use it in WinAPIs such as MessageBoxA, you need to convert it by int().

hWnd = int(QWidget.winId())
ctypes.windll.user32.MessageBoxA(hWnd,"Hi","Hi", 0x40)
Mehdi
  • 999
  • 13
  • 11
0

The winid returned from window.winId() is an unsigned integer pointer(in C/C++ terms). And so your handle is also.

Hopefully you already loaded the shared object library, which has the "C" functions.

Then just pass the handle by calling your C function using your library object.

something like,

lib.game_init(handle)
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • How does ownership behave? I mean python has ARC. If the python object goes out of scope the handle pointer is dangling right? – ManuelSchneid3r Dec 24 '22 at 12:27
  • The window object is QMainWindow built around the native window. As long as the native window exists the handle cannot be dangling. Its all how an API is using that handle for its operations. When the native window is destroyed the handle also will be gone. mostly the respective OS will take care of it. – Pavan Chandaka Dec 26 '22 at 02:40
  • Is there also a way to get a widget pointer. I mean not a main window. Just a subwidget. – ManuelSchneid3r Dec 26 '22 at 10:13
  • The object of QmainWindow. But for that you may need to follow this answer. https://stackoverflow.com/questions/9040669/how-can-i-implement-a-c-class-in-python-to-be-called-by-c – Pavan Chandaka Dec 26 '22 at 23:39
0

I have got winID handle for windows so

class CamViewWidget(QtGui.QWidget):
...
def on_sync_message(self, bus, message):
    if message.get_structure().get_name() == 'prepare-window-handle':
        message.src.set_property('force-aspect-ratio', True)
        message.src.set_window_handle(self.winId().__int__())
Alexej
  • 1
  • 1