Shiboken6: Python/C++ Bindings Helper

6.11.0 · active · verified Sat Apr 11

Shiboken6 is a core component of Qt for Python (PySide6), responsible for generating Python bindings for C++ libraries. It enables seamless, low-level interaction between Python and C++ objects, allowing Python to call C++ functions and access C++ data structures directly. It is currently at version 6.11.0 and is actively maintained, with updates released in conjunction with PySide6 and Qt releases.

Warnings

Install

Imports

Quickstart

Shiboken6 is primarily used for low-level interaction between Python and C++ objects, specifically within the Qt/PySide ecosystem. This example demonstrates how to retrieve the underlying C++ pointer from a PySide6 QObject using `getCppPointer`. While `wrapInstance` is also a key function, its typical use involves receiving a raw C++ pointer from an external C++ source, which is difficult to simulate in a simple, self-contained Python snippet. Note that `PySide6` is a prerequisite to run this quickstart.

from shiboken6 import getCppPointer, wrapInstance
# PySide6 is required to run this example. Install with: pip install PySide6
from PySide6.QtCore import QObject 

# Shiboken6 is typically used with PySide6 objects
obj = QObject()
obj.setObjectName("MyQObject")

# Get the underlying C++ pointer from a PySide6 object
cpp_ptr_long = getCppPointer(obj)
print(f"Python QObject: {obj}")
print(f"C++ pointer (long): {cpp_ptr_long:#x}")

print("\n--- Conceptual use of wrapInstance ---")
print("wrapInstance is used to convert a raw C++ pointer into a Python object.")
print("Example: `python_object = wrapInstance(raw_cpp_ptr, PySide6.ClassName, ownership=shiboken6.Ownership.CppOwnership)`")
print("It requires a valid C++ pointer (e.g., from an external C++ library call)")
print("and the expected PySide6 class type for correct wrapping. Careful memory")
print("management with `shiboken6.Ownership` is crucial to prevent crashes or leaks.")

view raw JSON →