You are most welcome. in what instances would you want, or need, to do that?
If you’re inheriting from a class that already subclasses those, it’d be useful for when you want to add to the original behavior. Otherwise you’d be replacing it. For example, anyone inheriting from the class you’re just made may want to call that so as to still be able to get the scrubbing functionality. Sometimes you may want to call it because the superclass has implemented something for it. For example, overriding the closeEvent and not calling its superclass would override - and thus block - the GUI from closing. I’m not entirely sure when and why event handlers require this, so I make it a habit out of always calling it for Qt events. On 25 June 2018 at 07:57, Panupat Chongstitwattana <panup...@gmail.com> wrote: > Thank you Marcus. Here's the updated methods and it's working as intended. > > def mousePressEvent(self, event): > if event.button() == QtCore.Qt.LeftButton: > self.mouse_start = event.x() > self.tracking = True > > def mouseReleaseEvent(self, event): > if event.button() == QtCore.Qt.LeftButton: > self.tracking = False > > def mouseMoveEvent(self, event): > 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) > > > A little question please. From some example I saw, they would also add > this line to the events. > > super(ClassName, self).mousePressEvent(event) > > in what instances would you want, or need, to do that? > > On Monday, June 25, 2018 at 1:44:19 PM UTC+7, Marcus Ottosson wrote: > >> Using an event filter is a little heavy handed for what you’re trying to >> achieve, I think. >> >> The problem with it is that every event, including draw events, passes >> through your event filter waiting for it to either block the event - return >> True - or pass it through - return False. And the number of events can >> be quite a lot - sometimes tens of thousands of events per second. >> >> An alternative is to instead override mousePressEvent, mouseReleaseEvent >> and mouseMoveEvent. That way you wouldn’t inadvertently interfere with >> events you aren’t interested in, and handling them would remain quick even >> as complexity increases. >> >> >> On 25 June 2018 at 07:18, Panupat Chongstitwattana <panu...@gmail.com> >> wrote: >> >>> Got it working now with more returns added to the eventFilter. >>> >>> 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() >>> return True >>> if event.type() == event.MouseButtonRelease: >>> if event.button() == QtCore.Qt.LeftButton: >>> self.tracking = False >>> event.accept() >>> return True >>> 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 >>> return False >>> >>> >>> On Monday, June 25, 2018 at 12:42:57 PM UTC+7, Panupat Chongstitwattana >>> wrote: >>>> >>>> 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 <panu...@gmail.com> >>>>> 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 python_inside_maya+unsubscr...@googlegroups.com. >>>>>> To view this discussion on the web visit >>>>>> https://groups.google.com/d/msgid/python_inside_maya/d0541c0 >>>>>> 0-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 python_inside_maya+unsubscr...@googlegroups.com. >>> To view this discussion on the web visit https://groups.google.com/d/ms >>> gid/python_inside_maya/d72dce4e-4833-4aa1-a7b9-d2fbe9ee9bb8% >>> 40googlegroups.com >>> <https://groups.google.com/d/msgid/python_inside_maya/d72dce4e-4833-4aa1-a7b9-d2fbe9ee9bb8%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 python_inside_maya+unsubscr...@googlegroups.com. > To view this discussion on the web visit https://groups.google.com/d/ > msgid/python_inside_maya/b498c554-8cbc-4b3e-9550- > 497170a627c5%40googlegroups.com > <https://groups.google.com/d/msgid/python_inside_maya/b498c554-8cbc-4b3e-9550-497170a627c5%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 python_inside_maya+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODxALSyK5M701A7%3DStd97zbfR1eNL3QhfAPCyKVeta4Tg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.