Skip to content

Commit 11e63b6

Browse files
authored
Merge pull request #5167 from radarhere/pyside2
Moved QApplication into one test
2 parents 41462d8 + 62693b7 commit 11e63b6

File tree

5 files changed

+63
-89
lines changed

5 files changed

+63
-89
lines changed

Tests/test_imageqt.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,6 @@
88
from PIL.ImageQt import qRgba
99

1010

11-
@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
12-
class PillowQPixmapTestCase:
13-
@classmethod
14-
def setup_class(self):
15-
try:
16-
if ImageQt.qt_version == "5":
17-
from PyQt5.QtGui import QGuiApplication
18-
elif ImageQt.qt_version == "side2":
19-
from PySide2.QtGui import QGuiApplication
20-
except ImportError:
21-
pytest.skip("QGuiApplication not installed")
22-
return
23-
24-
self.app = QGuiApplication([])
25-
26-
@classmethod
27-
def teardown_class(self):
28-
self.app.quit()
29-
self.app = None
30-
31-
3211
@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
3312
def test_rgb():
3413
# from https://doc.qt.io/archives/qt-4.8/qcolor.html

Tests/test_qt_image_fromqpixmap.py

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import pytest
2+
3+
from PIL import ImageQt
4+
5+
from .helper import assert_image_equal, hopper
6+
7+
if ImageQt.qt_is_installed:
8+
from PIL.ImageQt import QPixmap
9+
10+
if ImageQt.qt_version == "5":
11+
from PyQt5 import QtGui
12+
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
13+
elif ImageQt.qt_version == "side2":
14+
from PySide2 import QtGui
15+
from PySide2.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
16+
17+
class Example(QWidget):
18+
def __init__(self):
19+
super().__init__()
20+
21+
img = hopper().resize((1000, 1000))
22+
23+
qimage = ImageQt.ImageQt(img)
24+
25+
pixmap1 = QtGui.QPixmap.fromImage(qimage)
26+
27+
QHBoxLayout(self) # hbox
28+
29+
lbl = QLabel(self)
30+
# Segfault in the problem
31+
lbl.setPixmap(pixmap1.copy())
32+
33+
34+
def roundtrip(expected):
35+
result = ImageQt.fromqpixmap(ImageQt.toqpixmap(expected))
36+
# Qt saves all pixmaps as rgb
37+
assert_image_equal(result, expected.convert("RGB"))
38+
39+
40+
@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
41+
def test_sanity(tmp_path):
42+
# Segfault test
43+
app = QApplication([])
44+
ex = Example()
45+
assert app # Silence warning
46+
assert ex # Silence warning
47+
48+
for mode in ("1", "RGB", "RGBA", "L", "P"):
49+
# to QPixmap
50+
data = ImageQt.toqpixmap(hopper(mode))
51+
52+
assert isinstance(data, QPixmap)
53+
assert not data.isNull()
54+
55+
# Test saving the file
56+
tempfile = str(tmp_path / f"temp_{mode}.png")
57+
data.save(tempfile)
58+
59+
# from QPixmap
60+
roundtrip(hopper(mode))
61+
62+
app.quit()
63+
app = None

Tests/test_qt_image_toqimage.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111
if ImageQt.qt_is_installed:
1212
from PIL.ImageQt import QImage
1313

14-
try:
15-
from PyQt5 import QtGui
16-
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
17-
except (ImportError, RuntimeError):
18-
from PySide2 import QtGui
19-
from PySide2.QtWidgets import QApplication, QHBoxLayout, QLabel, QWidget
20-
2114

2215
def test_sanity(tmp_path):
2316
for mode in ("RGB", "RGBA", "L", "P", "1"):
@@ -49,29 +42,3 @@ def test_sanity(tmp_path):
4942
# Check that it actually worked.
5043
with Image.open(tempfile) as reloaded:
5144
assert_image_equal(reloaded, src)
52-
53-
54-
def test_segfault():
55-
app = QApplication([])
56-
ex = Example()
57-
assert app # Silence warning
58-
assert ex # Silence warning
59-
60-
61-
if ImageQt.qt_is_installed:
62-
63-
class Example(QWidget):
64-
def __init__(self):
65-
super().__init__()
66-
67-
img = hopper().resize((1000, 1000))
68-
69-
qimage = ImageQt.ImageQt(img)
70-
71-
pixmap1 = QtGui.QPixmap.fromImage(qimage)
72-
73-
QHBoxLayout(self) # hbox
74-
75-
lbl = QLabel(self)
76-
# Segfault in the problem
77-
lbl.setPixmap(pixmap1.copy())

Tests/test_qt_image_toqpixmap.py

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)