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

3 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

Comments

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)

4 Comments

How does ownership behave? I mean python has ARC. If the python object goes out of scope the handle pointer is dangling right?
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.
Is there also a way to get a widget pointer. I mean not a main window. Just a subwidget.
The object of QmainWindow. But for that you may need to follow this answer. stackoverflow.com/questions/9040669/…
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__())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.