file_preview.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:git-annex-metadata-gui 作者: alpernebbi 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号