Skip to main content

A single-module Qt binding compatibility layer for PyQt6, PySide6, PyQt5, PySide2, PyQt4, and PySide.

Project description

A single-module Qt binding compatibility layer for PyQt6, PySide6, PyQt5, PySide2, PyQt4, and PySide.

Installation

Install the package with:

pip install pyqtcompat

You must also have at least one supported Qt binding installed in your Python environment:

  • PyQt6
  • PySide6
  • PyQt5
  • PySide2
  • PyQt4
  • PySide

The module uses detect-qt-binding to choose the active binding.

Usage

Import the normalized Qt names from pyqtcompat instead of importing directly from a specific binding:

from pyqtcompat import QApplication, QLabel, QT_ALIGN_CENTER, execute

application = QApplication([])
label = QLabel('Hello from pyqtcompat')
label.setAlignment(QT_ALIGN_CENTER)
label.show()
raise SystemExit(execute(application))

You can also inspect which binding was selected:

from pyqtcompat import QT_BINDING

print(QT_BINDING)

Public API

pyqtcompat defines __all__ at the top of the module. These names are the intended application-facing imports.

Binding and module-level names:

  • QtBindings
  • QT_BINDING
  • IS_PYQT, IS_PYSIDE, IS_QT6
  • QtSignal

Common Qt classes exported directly:

  • QTimer
  • QImage, QPixmap, QColor, QPainter, QPen, QScreen
  • QAbstractSlider
  • QApplication
  • QCheckBox, QComboBox, QDesktopServices, QDesktopWidget, QFileDialog
  • QFrame, QGroupBox, QHBoxLayout, QLabel, QLineEdit
  • QMainWindow, QMessageBox, QPushButton, QScrollArea
  • QSizePolicy, QSlider, QVBoxLayout, QWidget

Compatibility constants exported directly:

  • Format_RGB32, Format_ARGB32, Format_RGB888
  • QT_KEEP_ASPECT_RATIO, QT_SMOOTH_TRANSFORMATION, QT_ALIGN_CENTER, QT_HORIZONTAL
  • QT_SCROLLBAR_ALWAYS_OFF, QT_SCROLLBAR_AS_NEEDED
  • QSIZEPOLICY_EXPANDING
  • SLIDER_SINGLE_STEP_ADD, SLIDER_SINGLE_STEP_SUB, SLIDER_PAGE_STEP_ADD, SLIDER_PAGE_STEP_SUB, SLIDER_MOVE

Helper functions exported directly:

  • exec_qapplication(app)
    • Purpose: Run the Qt application event loop with the correct cross-binding call style.
    • Returns: the integer exit code returned by the event loop.
  • execute(q_application)
    • Purpose: Alias for exec_qapplication() for codebases already using that name.
    • Returns: the integer exit code returned by the event loop.
  • get_or_create_q_application(argv=None)
    • Purpose: Return the existing QApplication instance, or create one if none exists yet.
    • Returns: a QApplication instance.
  • get_buffer(qimage)
    • Purpose: Return a Python buffer-compatible view of the pixel memory of a QImage, handling PyQt/PySide differences.
    • Returns: a binding-specific buffer object suitable for zero-copy access.
  • qimage_get_buffer(qimage)
    • Purpose: Same as get_buffer(); provided as the explicit QImage-named form.
    • Returns: a binding-specific buffer object suitable for zero-copy access.
  • get_primary_screen_geometry()
    • Purpose: Read the geometry of the primary screen using the correct API for old and new Qt bindings.
    • Returns: a Qt rectangle object describing the primary screen bounds.
  • get_primary_screen_size()
    • Purpose: Get the primary screen width and height as plain Python values.
    • Returns: a (width, height) tuple of integers.
  • grab_primary_screen_pixmap()
    • Purpose: Capture the full primary screen into a pixmap using the correct API for the active binding.
    • Returns: a QPixmap containing the screen capture.

Example using QImage buffer access:

from pyqtcompat import QImage, Format_RGB32, get_buffer

image = QImage(64, 64, Format_RGB32)
buffer_object = get_buffer(image)
print(type(buffer_object))

Example using screen-grab helpers:

from pyqtcompat import get_or_create_q_application, get_primary_screen_size, grab_primary_screen_pixmap

application = get_or_create_q_application()
width, height = get_primary_screen_size()
pixmap = grab_primary_screen_pixmap()
print(width, height, pixmap.width(), pixmap.height())

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyqtcompat-0.1.0a0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyqtcompat-0.1.0a0-py2.py3-none-any.whl (6.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pyqtcompat-0.1.0a0.tar.gz.

File metadata

  • Download URL: pyqtcompat-0.1.0a0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for pyqtcompat-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 9b900e05bf5fc05e5799c2eb8a2295f2524c7100d76a375cf7235fe28fbc75cd
MD5 b4ff89a164c0da7702302fe7d3734691
BLAKE2b-256 876ad7c969a73a24d1ce8f16ccafd36eed09f4c5cdeb3ed6cb4aede9c54bd091

See more details on using hashes here.

File details

Details for the file pyqtcompat-0.1.0a0-py2.py3-none-any.whl.

File metadata

  • Download URL: pyqtcompat-0.1.0a0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for pyqtcompat-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0378ec5460b7e53fe139e2f07a86fa915d8e6358ed7323158b8e117ec48d2a39
MD5 fbd927fa6cfa161973389ea957ae4962
BLAKE2b-256 c9f9c3fbaaf4db5eb5130c9b6f6b796ef6238b672c2ad548f5dced15be06d5c0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page