Thank you for your suggestion Marcus.
I added MouseButtonPress and MouseButtonRelease event based on your
suggestion and it almost work. I cannot get the image to repaint. It prints
out the image name correctly however.
I way to get the repaint working is to remove return True (line 51) from
the eventFilter method, but then it would produce error about eventFilter
wanting a boolean result over and over.
# -*- 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_())
On Friday, June 22, 2018 at 5:16:30 PM UTC+7, Marcus Ottosson wrote:
>
> Here's what I'd do.
>
> 1. On mouse press, capture the position of the mouse and register that
> "scrubbing mode" is active.
> 2. When scrubbing mode is active, in your `mouseMoveEvent` compare the
> current position to the one stored on mouse press
> 3. The distance (e.g. Manhattan length
> <http://doc.qt.io/archives/qt-4.8/qpoint.html#manhattanLength>) is how
> much to scrub.
> 4. If the delta is positive, then you're scrubbing to the right. Negative
> means scrubbing to the left.
> 5. On mouse release, disable scrubbing mode.
>
> This should help produce a smooth and predictable scrubbing behaviour.
>
> On 22 June 2018 at 11:06, Panupat Chongstitwattana <[email protected]
> <javascript:>> wrote:
>
>> Hi.
>>
>> I'm trying to create a turntable viewer and in need of some guidance
>> please. In this view, I want to be able to click and hold left mouse button
>> then drag left/right to turn the turntable, which are image sequences.
>>
>> My questions:
>>
>> - Is there other way I should approach this? The mouse move event
>> triggers too fast and when I try to print out new image names they appear
>> too rapidly.
>>
>> - How can I detect if I am moving mouse left or right?
>>
>> - The pixmap doesn't seem to repaint even if I explicitly tells it to.
>>
>> Appreciate any help. Thank you!
>>
>> --
>> 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 [email protected] <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/d0541c00-46a5-42da-9cef-36e66ae76813%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/python_inside_maya/d0541c00-46a5-42da-9cef-36e66ae76813%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/50596066-6081-48a1-88db-fab759ccda1c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.