You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义QLabel刷新图片时返回空Pixmap?如何处理图片文件变更?

问题:图片文件更新后QPixmap加载失败(Null Pixmap错误)

我需要并排显示两个不同尺寸的方形图片,自定义的ImageLabel类要监听指定路径的文件变更,当图片被覆盖时自动刷新显示。程序启动时图片显示正常,但更新图片文件后会返回错误:QPixmap::scaled: Pixmap is a null pixmap。

之前只更新单张图片时功能正常,尝试过添加定时器确保后台进程完成后再刷新,但没有效果。考虑过是否需要为每个文件单独添加文件监视器?

相关代码片段:

...
class Ui_MainWindow(QMainWindow):
...
     # 根据路径和尺寸设置图片和二维码
     self.image_label.set_image(IMAGE_PATH, 500)
     ...
     self.qr_label.set_image(QR_PATH, 200)
...

# 创建一个显示图片并监听文件变更的QLabel
# 当生成新图片时,刷新显示的图片
class ImageLabel(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.watcher = QFileSystemWatcher()
        self.watcher.fileChanged.connect(self.refresh_image)
        self.setAlignment(Qt.AlignCenter)

    def set_image(self, image_path, width):
        self.image_path = image_path
        self.watcher.addPath(image_path)
                
        self.refresh_image(width)

    def refresh_image(self, width):
        pixmap = QPixmap(self.image_path)
        self.setPixmap(pixmap.scaled(width, width, Qt.KeepAspectRatio, Qt.SmoothTransformation))

...

更新:
我做了隔离测试,以下代码可以正常运行,说明问题应该出在主GUI的其他部分。

测试代码:

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import QFileSystemWatcher, Qt

# 常量
IMAGE_PATH = "image.png"
QR_PATH = "qr.png"

# 应用主UI
class Ui_MainWindow(QMainWindow):
    # 设置UI
    def setup_ui(self, MainWindow):
        self.central_widget = QtWidgets.QWidget(MainWindow)
        
        self.main_layout = QtWidgets.QHBoxLayout(self.central_widget)

        # 创建图片标签        
        self.image_label = ImageLabel()
        self.image_label.setObjectName("imageLabel")
        
        # 创建二维码标签
        self.qr_label = ImageLabel()
        self.qr_label.setObjectName("qr_label")

        # 设置图片和二维码
        self.image_label.set_image(IMAGE_PATH, 500)
        self.qr_label.set_image(QR_PATH, 200)
        
        self.main_layout.addWidget(self.image_label)
        self.main_layout.addWidget(self.qr_label)
        
        MainWindow.setCentralWidget(self.central_widget)
        
# 创建一个显示图片并监听文件变更的QLabel
# 当生成新图片时,刷新显示的图片    
class ImageLabel(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.watcher = QFileSystemWatcher()
        self.watcher.fileChanged.connect(self.refresh_image)
        self.setAlignment(Qt.AlignCenter)

    def set_image(self, image_path, width):
        self.image_path = image_path
        self.watcher.addPath(image_path)
        self.width = width
        self.refresh_image()

    def refresh_image(self):
        pixmap = QPixmap(self.image_path)
        self.setPixmap(pixmap.scaled(self.width, self.width, Qt.KeepAspectRatio, Qt.SmoothTransformation))  
    
# 设置主窗口和应用
if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()

    # 设置主窗口
    ui = Ui_MainWindow()
    ui.setup_ui(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

内容的提问来源于stack exchange,提问作者avroom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 19:42:51