def preview_image_file(self, path):
filename = path.split('/')[-1]
if self.graphics_preview is None:
msg = "Graphics preview widget not created yet."
logger.critical(msg)
return
if not self.isVisible():
msg = "Preview widget invisible, not previewing image."
logger.info(msg)
return
self.setCurrentWidget(self.graphics_preview)
scene = QtWidgets.QGraphicsScene(self)
self.graphics_preview.setScene(scene)
# Using QImage instead of directly creating the QPixmap
# prevents a segmentation fault in my container setup
image = QtGui.QImage(path)
if image.isNull():
fmt = "File '{}' should be an image, but isn't."
msg = fmt.format(filename)
logger.error(msg)
return
pixmap = QtGui.QPixmap.fromImage(image)
if pixmap.isNull():
fmt = "Failed to generate pixmap from image '{}'."
msg = fmt.format(filename)
logger.critical(msg)
return
pixmap_item = QtWidgets.QGraphicsPixmapItem(pixmap)
scene.addItem(pixmap_item)
self.graphics_preview.fitInView(
pixmap_item,
Qt.Qt.KeepAspectRatio,
)
fmt = "Previewed file '{}' as an image."
msg = fmt.format(filename)
logger.info(msg)
评论列表
文章目录