python - QLabel 不会重新绘制像素图

标签 python pyside qlabel

我正在尝试制作一个转盘播放器,当按住鼠标左键并向左或向右拖动时,它可以翻转图像序列。它几乎可以工作并且正在打印出正确的图像名称。但图像本身不会更新/重新绘制。

如果我从 eventFilter 方法的最后一行删除 return True ,它就会起作用。然而,它也会产生很多关于 eventFilter 想要 bool 值返回的提示。

3个问题。我该如何解决这个问题?有比我所做的更好的方法吗?还有没有办法提前加载图像序列,这样就不会中途减慢速度?

谢谢。

__name__ == '__main__'中使用的示例图像序列:https://drive.google.com/open?id=1_kMf0bVZ5jMKdCQXzmOk_34nwwHOyXqz

# -*- coding: utf-8 -*-
import sys
from os.path import dirname, realpath, join
from PySide.QtGui import (QApplication, QVBoxLayout, QLabel, QPixmap,
    QWidget)
from PySide import QtCore

class PlayTurntable(QWidget):
    def __init__(self, images, mouse_threshold=50, parent=None):
        super(PlayTurntable, self).__init__(parent)

        self.label = QLabel()
        self.label.setFixedWidth(300)
        self.label.setFixedHeight(200)
        layout = QVBoxLayout()
        layout.addWidget(self.label)
        self.setLayout(layout)

        # init variables
        self.tracking = False
        self.mouse_start = 0
        self.mouse_threshold = mouse_threshold
        self.images = images
        self.image_index = 0
        self.pic = QPixmap(self.images[self.image_index])
        self.label.setPixmap(self.pic.scaled(300, 200, QtCore.Qt.KeepAspectRatio))
        self.installEventFilter(self)

    def eventFilter(self, obj, event):
        if event.type() == event.MouseButtonPress:
            if event.button() == QtCore.Qt.LeftButton:
                self.mouse_start = event.x()
                self.tracking = True
                event.accept()
        if event.type() == event.MouseButtonRelease:
            if event.button() == QtCore.Qt.LeftButton:
                self.tracking = False
                event.accept()
        if event.type() == event.MouseMove:
            if self.tracking:
                mouse_x = event.x()
                distance = self.mouse_start - mouse_x
                if abs(distance) >= self.mouse_threshold:
                    self.mouse_start = mouse_x
                    if distance > 0:
                        self.frame_step(1)
                    else:
                        self.frame_step(-1)
                event.accept()
        return True

    def frame_step(self, amount):
        self.image_index += amount
        if self.image_index >= len(self.images):
            self.image_index = 0
        elif self.image_index < 0:
            self.image_index = len(self.images) - 1
        print 'switching to: %s' % self.images[self.image_index]

        self.pic.load(self.images[self.image_index])
        self.label.setPixmap(
            self.pic.scaled(300, 200, QtCore.Qt.KeepAspectRatio))
        self.label.repaint()


if __name__=='__main__':
    current_path = dirname(realpath(__file__))
    images = ['turn1.jpg', 'turn2.jpg', 'turn3.jpg', 'turn4.jpg']
    for index, value in enumerate(images):
        images[index] = join(current_path, value)

    app = QApplication(sys.argv)
    PT = PlayTurntable(images)
    PT.show()
    sys.exit(app.exec_())

最佳答案

只有您不希望传播给子级的事件必须返回 True,还有其他事件您不应该返回。在您的特定情况下,有一些特定事件会强制您更新 GUI,其中之一是 mouseevent,当您返回 True 时,您将阻止它们更新。您的目标不是过滤元素,只是监听这些事件,因此建议返回父级返回的内容。

# -*- coding: utf-8 -*-
import sys
from os.path import dirname, realpath, join
from PySide.QtGui import (QApplication, QVBoxLayout, QLabel, QPixmap,
    QWidget)
from PySide.QtCore import Qt

class PlayTurntable(QWidget):
    def __init__(self, images, mouse_threshold=50, parent=None):
        super(PlayTurntable, self).__init__(parent)

        self.label = QLabel()
        self.label.setFixedSize(300, 200)

        layout = QVBoxLayout(self)
        layout.addWidget(self.label)

        # init variables
        self.tracking = False
        self.mouse_start = 0
        self.mouse_threshold = mouse_threshold
        self.images = images
        self.image_index = 0
        self.pic = QPixmap(self.images[self.image_index])
        self.label.setPixmap(self.pic.scaled(300, 200, Qt.KeepAspectRatio))
        self.installEventFilter(self)

    def eventFilter(self, obj, event):
        if event.type() == event.MouseButtonPress:
            if event.button() == Qt.LeftButton:
                self.mouse_start = event.x()
                self.tracking = True
        elif event.type() == event.MouseButtonRelease:
            if event.button() == Qt.LeftButton:
                self.tracking = False
        elif event.type() == event.MouseMove:
            if self.tracking:
                mouse_x = event.x()
                distance = self.mouse_start - mouse_x
                if abs(distance) >= self.mouse_threshold:
                    self.mouse_start = mouse_x
                    if distance > 0:
                        self.frame_step(1)
                    else:
                        self.frame_step(-1)
        return QWidget.eventFilter(self, obj, event)

    def frame_step(self, amount):
        self.image_index += amount
        self.image_indes = (self.image_index + amount) % len(self.images)
        print('switching to: %s' % self.images[self.image_index])

        self.pic.load(self.images[self.image_index])
        self.label.setPixmap(self.pic.scaled(300, 200, Qt.KeepAspectRatio))

if __name__=='__main__':
    current_path = dirname(realpath(__file__))
    images = ['turn1.jpg', 'turn2.jpg', 'turn3.jpg', 'turn4.jpg']
    for index, value in enumerate(images):
        images[index] = join(current_path, value)

    app = QApplication(sys.argv)
    PT = PlayTurntable(images)
    PT.show()
    sys.exit(app.exec_())

关于python - QLabel 不会重新绘制像素图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51017058/

相关文章:

python - 将 QListView 与 Pyside 中定义的模型一起使用

python-2.7 - 在 Angstrom 上安装 Qt 和 PySide

python - QAction 触发信号未传递检查参数

python - 根据条件获取 DataFrame 列中的最后一个字符串元素

python - 需要帮助理解 python : 中的此错误文本

python - Qlabels 在最后被剪掉

qt - 使用Qt的QLabel点击事件?

python - 如何将字符串转换为在 Django 中调用模型的名称?

python - 在 python 3.4.3 上安装 pandas 时,出现错误 - No module tempita