繁体   English   中英

PyQt6 中的 QPixmapCache

[英]QPixmapCache in PyQt6

您好,我正在尝试创建一个 PyQt6 小部件,它可以在一组图像上进行动画处理,但其精确的帧速率会随着实时参数的 function 的变化而变化。 我将QPixmap插入到 for 循环中的缓存中,并且能够在同一个 for 循环中通过QPixmap.find()找到QPixmap对象,但在循环之外.find()返回None

下面是脚本。 如果需要,我可以包含图像系列。 感谢任何帮助。

import sys
from PyQt6.QtWidgets import (
    QApplication,
    QMainWindow,
    QLabel
)
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPixmap, QPixmapCache
from pathlib import Path

class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()
        self.animating_label = QLabel('animation here')
        self.animating_label.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
        self.animating_label.pixmap().size().setWidth(200)
        self.animating_label.pixmap().size().setHeight(200)
        self.setCentralWidget(self.animating_label)

        parent_path = Path(__file__).parent
        self.images_path = parent_path / 'images/animation_set/'
        self.animating_label.setPixmap(QPixmap(self.images_path.__str__() + "/clock01.png"))

        self.pixmapcache_keys: [str] = []

        self.load_images()
        test = QPixmapCache.find("clock02")
        try:
            self.animating_label.setPixmap(test)
        except:
            if test == None:
                print("Failure: QPixMapCache.find() returned", test)

    def load_images(self):
        image_suffixes = ['.jpg', '.png', '.gif', '.bmp']
        imgs_found_count = 0
        for filepath in self.images_path.iterdir():
            if filepath.suffix in image_suffixes:
                imgs_found_count += 1
                cache_key = filepath.stem
                self.pixmapcache_keys.append(cache_key)

                if not QPixmapCache.find(cache_key):
                    pixmap = QPixmap(filepath.__str__())
                    QPixmapCache.insert(cache_key, pixmap)

                print("pixmap %s" % cache_key, QPixmapCache.find(cache_key))


        print(imgs_found_count, "image(s) found in animation_set directory.", len(self.pixmapcache_keys),
              "keys loaded into QPixmapCache")

app = QApplication(sys.argv)

window = MainWindow()
window.show()

# start Qt event loop
app.exec()

print("Script complete.")
sys.exit(1)

这是 output:

pixmap clock04 <PyQt6.QtGui.QPixmap object at 0x101c84b30>
pixmap clock10 <PyQt6.QtGui.QPixmap object at 0x101c84970>
pixmap clock11 <PyQt6.QtGui.QPixmap object at 0x101c84b30>
pixmap clock05 <PyQt6.QtGui.QPixmap object at 0x101c84970>
...

24 image(s) found in animation_set directory. 24 keys loaded into QPixmapCache
Failure: QPixMapCache.find() returned None
Script complete.

我能够让 animation 使用 QPixmaps 列表而不是 QPixmapCache 工作,但我不确定这是否是最佳方法。

从文件生成 Qpixmap 列表:

        for filepath in sorted(self.images_path.iterdir()):
            if filepath.suffix in image_suffixes:
                qpixmap = QPixmap(filepath.__str__())
                self.pixmaps.append(qpixmap)

在计时器上的其他地方调用

    def advance(self):
        if self.pixmap_current_index >= len(self.pixmaps) - 1:
            self.pixmap_current_index = 0
        else:
            self.pixmap_current_index += 1
        self.animating_label.setPixmap(self.pixmaps[self.pixmap_current_index])
        self.timer.start(self.refresh_interval)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM