Figured another one out thanks to Stackoverflow.

https://stackoverflow.com/questions/43454882/paint-over-qlabel-with-pyqt

My paintEvent was done on QWidget, which was behind QLabel. Once the QLabel 
got stylesheet'd it covered up my image I suppose. So paintEvent needed to 
be implemented on the QLabel instead.

Attached is a working code. Not sure how many methods I should keep in 
QWidget class vs move to QLabel class but here is what I have.

Next step I'll try out QPixmapCache.



-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to python_inside_maya+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/5cc937ea-b3a3-4d66-ac71-2f5981ae20d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
# -*- coding: utf-8 -*-
import sys
from os import environ
from os.path import dirname, realpath, join, isfile
import qdarkstyle
from PySide import QtCore
from PySide.QtGui import (QApplication, QVBoxLayout, QLabel, QPixmap, QWidget,
                          QPainter)

environ['QT_API'] = 'pyside'

class ScrubbaleWidget(QWidget):
    """ Exercize. A widget that users can scrub to play the turntable image
    sequence.

    Args:
        images (str list): Full path to sequential images.
        scrub_threshold (int): Scrub distance for image change.

    Attributes:
        disable (bool): stop the Widget from doing anything
        tracking (bool): A switch to tell Widget when to track mouse movement.
        mouse_start (float): Mouse starting X position.

    """
    def __init__(self, images=None, scrub_threshold=30, parent=None):
        super(ScrubbaleWidget, self).__init__(parent)

        self.setMinimumSize(100, 50)
        self.resize(250, 200)

        self.scrub_threshold = scrub_threshold
        self.disable = False
        self.tracking = False
        self.mouse_start = 0

        self.label = ImageSequenceLabel()
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        layout = QVBoxLayout()
        layout.setContentsMargins(10, 10, 10, 10)
        layout.addWidget(self.label)
        self.setLayout(layout)

        pics = []
        default_img = join(dirname(realpath(__file__)), 'mystery2.png')
        if not images or not isinstance(images, list):
            self.disable = True
            self.label.set_opacity(0.2)
            pics.append(default_img)
        else:
            for image in images:
                if not isfile(image):
                    pics.append(default_img)
                else:
                    pics.append(image)
        self.label.set_pics(pics)


    def mousePressEvent(self, event):
        if self.disable:
            return
        if event.button() == QtCore.Qt.LeftButton:
            self.mouse_start = event.x()
            self.tracking = True

    def mouseReleaseEvent(self, event):
        if self.disable:
            return
        if event.button() == QtCore.Qt.LeftButton:
            self.tracking = False

    def mouseMoveEvent(self, event):
        if self.disable:
            return
        if not self.tracking:
            return
        distance = self.mouse_start - event.x()
        if distance >= self.scrub_threshold:
            self.mouse_start = event.x()
            self.label.index_step(1)
        elif distance <= -self.scrub_threshold:
            self.mouse_start = event.x()
            self.label.index_step(-1)


class ImageSequenceLabel(QLabel):
    def __init__(self, parent=None):
        super(ImageSequenceLabel, self).__init__(parent=parent)
        self.pics = []
        self.pic_index = 0
        self.pic_opacity = 1.0

    def set_pics(self, images):
        self.pics = []
        default_img = QPixmap(join(dirname(realpath(__file__)), 'mystery2.png'))
        if images is None or not isinstance(images, list):
            self.pics = [default_img]
        else:
            for img in images:
                if isfile(img):
                    self.pics.append(QPixmap(img))
        if not self.pics:
            # In case none of the image sequence exists
            self.pics = [default_img]

    def set_opacity(self, opacity=1.0):
        self.pic_opacity = opacity

    def index_step(self, step=1):
        # update and loop the image index
        if len(self.pics) == 0:
            return
        self.pic_index += step
        if self.pic_index >= len(self.pics):
            self.pic_index = 0
        elif self.pic_index < 0:
            self.pic_index = len(self.pics) - 1
        self.repaint()

    def paintEvent(self, e):
        super(ImageSequenceLabel, self).paintEvent(e)
        if not self.pics:
            return
        painter = QPainter()
        painter.begin(self)
        painter.setOpacity(self.pic_opacity)
        scaled_pic = self.pics[self.pic_index].scaled(
                self.width(), self.height(),
                QtCore.Qt.KeepAspectRatio, QtCore.Qt.SmoothTransformation)
        x_offset = (self.width() - scaled_pic.width()) / 2
        y_offset = (self.height() - scaled_pic.height()) / 2
        painter.drawPixmap(x_offset, y_offset, scaled_pic)
        painter.end()


if __name__=='__main__':
    # Passing list of example images as argument.
    current_path = dirname(realpath(__file__))
    images = []
    for i in range(1,8):
        images.append('%s/turn%s.jpg' % (current_path, i))
    app = QApplication(sys.argv)

    SCRUB = ScrubbaleWidget(images)
    # SCRUB = ScrubbaleImageSequenceWidget('random string')
    app.setStyleSheet(qdarkstyle.load_stylesheet_pyside())
    SCRUB.show()
    sys.exit(app.exec_())

Reply via email to