QLabel没有重新绘制pixmap

问题描述 投票:1回答:1

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

如果我从eventFilter方法的最后一行删除return True,它就可以了。它也会产生很多关于eventFilter想要一个布尔返回的抱怨。

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_())
python pyside qlabel
1个回答
1
投票

只有您不希望传播给您孩子的事件必须返回True,而其他事件则不应该返回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_())
© www.soinside.com 2019 - 2024. All rights reserved.