Added PyQt6 support

This commit is contained in:
Andrew Murray 2021-02-10 21:12:30 +11:00
parent cc4ed21f19
commit 57d6e8ca43
3 changed files with 19 additions and 9 deletions

View File

@ -14,7 +14,9 @@ def test_rgb():
# typedef QRgb # typedef QRgb
# An ARGB quadruplet on the format #AARRGGBB, # An ARGB quadruplet on the format #AARRGGBB,
# equivalent to an unsigned int. # equivalent to an unsigned int.
if ImageQt.qt_version == "side6": if ImageQt.qt_version == "6":
from PyQt6.QtGui import qRgb
elif ImageQt.qt_version == "side6":
from PySide6.QtGui import qRgb from PySide6.QtGui import qRgb
elif ImageQt.qt_version == "5": elif ImageQt.qt_version == "5":
from PyQt5.QtGui import qRgb from PyQt5.QtGui import qRgb

View File

@ -7,7 +7,9 @@ from .helper import assert_image_equal, hopper
if ImageQt.qt_is_installed: if ImageQt.qt_is_installed:
from PIL.ImageQt import QPixmap from PIL.ImageQt import QPixmap
if ImageQt.qt_version == "side6": if ImageQt.qt_version == "6":
from PyQt6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
elif ImageQt.qt_version == "side6":
from PySide6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget from PySide6.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
elif ImageQt.qt_version == "5": elif ImageQt.qt_version == "5":
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget from PyQt5.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget

View File

@ -23,6 +23,7 @@ from . import Image
from ._util import isPath from ._util import isPath
qt_versions = [ qt_versions = [
["6", "PyQt6"],
["side6", "PySide6"], ["side6", "PySide6"],
["5", "PyQt5"], ["5", "PyQt5"],
["side2", "PySide2"], ["side2", "PySide2"],
@ -32,7 +33,10 @@ qt_versions = [
qt_versions.sort(key=lambda qt_version: qt_version[1] in sys.modules, reverse=True) qt_versions.sort(key=lambda qt_version: qt_version[1] in sys.modules, reverse=True)
for qt_version, qt_module in qt_versions: for qt_version, qt_module in qt_versions:
try: try:
if qt_module == "PySide6": if qt_module == "PyQt6":
from PyQt6.QtCore import QBuffer, QIODevice
from PyQt6.QtGui import QImage, QPixmap, qRgba
elif qt_module == "PySide6":
from PySide6.QtCore import QBuffer, QIODevice from PySide6.QtCore import QBuffer, QIODevice
from PySide6.QtGui import QImage, QPixmap, qRgba from PySide6.QtGui import QImage, QPixmap, qRgba
elif qt_module == "PyQt5": elif qt_module == "PyQt5":
@ -63,7 +67,8 @@ def fromqimage(im):
(given either as Python string or a PyQt string object) (given either as Python string or a PyQt string object)
""" """
buffer = QBuffer() buffer = QBuffer()
buffer.open(QIODevice.ReadWrite) qt_openmode = QIODevice.OpenMode if qt_version == "6" else QIODevice
buffer.open(qt_openmode.ReadWrite)
# preserve alpha channel with png # preserve alpha channel with png
# otherwise ppm is more friendly with Image.open # otherwise ppm is more friendly with Image.open
if im.hasAlphaChannel(): if im.hasAlphaChannel():
@ -132,25 +137,26 @@ def _toqclass_helper(im):
if isPath(im): if isPath(im):
im = Image.open(im) im = Image.open(im)
qt_format = QImage.Format if qt_version == "6" else QImage
if im.mode == "1": if im.mode == "1":
format = QImage.Format_Mono format = qt_format.Format_Mono
elif im.mode == "L": elif im.mode == "L":
format = QImage.Format_Indexed8 format = qt_format.Format_Indexed8
colortable = [] colortable = []
for i in range(256): for i in range(256):
colortable.append(rgb(i, i, i)) colortable.append(rgb(i, i, i))
elif im.mode == "P": elif im.mode == "P":
format = QImage.Format_Indexed8 format = qt_format.Format_Indexed8
colortable = [] colortable = []
palette = im.getpalette() palette = im.getpalette()
for i in range(0, len(palette), 3): for i in range(0, len(palette), 3):
colortable.append(rgb(*palette[i : i + 3])) colortable.append(rgb(*palette[i : i + 3]))
elif im.mode == "RGB": elif im.mode == "RGB":
data = im.tobytes("raw", "BGRX") data = im.tobytes("raw", "BGRX")
format = QImage.Format_RGB32 format = qt_format.Format_RGB32
elif im.mode == "RGBA": elif im.mode == "RGBA":
data = im.tobytes("raw", "BGRA") data = im.tobytes("raw", "BGRA")
format = QImage.Format_ARGB32 format = qt_format.Format_ARGB32
else: else:
raise ValueError(f"unsupported image mode {repr(im.mode)}") raise ValueError(f"unsupported image mode {repr(im.mode)}")