io: qt4 plugin: allow multiple images to be displayed. Keep track of Windows so that they don't get deallocated before they are closed.

This commit is contained in:
Stefan van der Walt
2009-11-01 23:05:42 +02:00
parent 2eb28f4e85
commit bdcce9123a
+41 -17
View File
@@ -1,31 +1,55 @@
import plugin
import numpy as np
import sys
app = [None]
windows = []
try:
from PyQt4.QtGui import QApplication, QImage, QPixmap, QLabel
from PyQt4.QtGui import (QApplication, QMainWindow, QImage, QPixmap,
QLabel)
except ImportError:
have_qt = False
pass
else:
have_qt = True
def show(arr):
arr = np.ascontiguousarray(arr.astype(np.uint8))
if arr.ndim != 3:
raise ValueError("Qt only displays colour images.")
class ImageWindow(QMainWindow):
def __init__(self, arr):
QMainWindow.__init__(self)
if arr.shape[-1] == 4:
raise ValueError("Alpha channels not yet supported.")
img = QImage(arr.data, arr.shape[1], arr.shape[0],
QImage.Format_RGB888)
pm = QPixmap.fromImage(img)
img = QImage(arr.data, arr.shape[1], arr.shape[0], QImage.Format_RGB888)
pm = QPixmap.fromImage(img)
label = QLabel()
label.setPixmap(pm)
label.show()
app = QApplication([])
self.label = label
self.setCentralWidget(self.label)
label = QLabel()
label.setPixmap(pm)
label.show()
def closeEvent(self, event):
# Allow window to be destroyed by removing any
# references to it
windows.remove(self)
app.exec_()
def show(arr, block=True):
arr = np.ascontiguousarray(arr.astype(np.uint8))
if arr.ndim != 3:
raise ValueError("Qt only displays colour images.")
if arr.shape[-1] == 4:
raise ValueError("Alpha channels not yet supported.")
if not '-qt4thread' in sys.argv and app[0] is None:
app[0] = QApplication([])
iw = ImageWindow(arr)
iw.show()
# Keep track of window so that it doesn't get destroyed
windows.append(iw)
if app[0] and block:
app[0].exec_()
if have_qt:
plugin.register('qt', show=show)