Thank you Justin. Wow so by setting minimal size it allows going smaller. I 
wouldn't have suspected that.

By the way is it OK to call setPixmap over and over like I did in 
resizeEvent? It seems a bit redundant.

def resizeEvent(self, event):
    self.label.setPixmap(
        self.pic.scaled(self.label.width(), self.label.height(),
                        QtCore.Qt.KeepAspectRatio))


But I tried doing self.pic.scaled and self.label.repaint but those 2 didn't 
seem to do anything.

On Tuesday, June 26, 2018 at 2:49:13 AM UTC+7, Justin Israel wrote:
>
>
>
> On Tue, Jun 26, 2018, 12:39 AM Panupat Chongstitwattana <panu...@gmail.com 
> <javascript:>> wrote:
>
>> Question on Pixmap resizing. 
>>
>> I tried switching from fixed size to allow resizing.
>> This is what I have at the moment and what happens is that I can increase 
>> my widget size,
>> but it won't let me decrease. How should I approach this?
>>
>
> Try self.label.setMinimumSize(minX, minY) 
>
>
>> in __init__
>>
>> self.pic = QPixmap(self.images[self.image_index])
>>
>> # self.label.setScaledContents(True)
>> # self.label.setPixmap(self.pic.scaled(300, 200, QtCore.Qt.KeepAspectRatio))
>> self.label.setPixmap(
>>     self.pic.scaled(self.label.width(), self.label.height(), 
>> QtCore.Qt.KeepAspectRatio))
>>
>>
>> and an event override.
>>
>> def resizeEvent(self, event):
>>     self.label.setPixmap(
>>         self.pic.scaled(self.label.width(), self.label.height(),
>>                         QtCore.Qt.KeepAspectRatio))
>>
>>
>>
>>
>> On Monday, June 25, 2018 at 7:32:18 PM UTC+7, Panupat Chongstitwattana 
>> wrote:
>>>
>>> Thank you for your suggestion Justin. Did you mean the mouseMoveEvent 
>>> method? Made some adjustments and is working as intended.
>>>
>>> def mouseMoveEvent(self, event):
>>>     if not self.tracking:
>>>         return
>>>     distance = self.mouse_start - event.x()
>>>     if distance >= self.mouse_threshold:
>>>         self.mouse_start = event.x()
>>>         self.frame_step(1)
>>>     elif distance <= -self.mouse_threshold:
>>>         self.mouse_start = event.x()
>>>         self.frame_step(-1)
>>>
>>>
>>> On Monday, June 25, 2018 at 3:39:18 PM UTC+7, Justin Israel wrote:
>>>>
>>>> One last tip about the event handlers. It might be good for you to make 
>>>> use of if/elif instead of comparing extra cases that will never be true 
>>>> since another event is going to match. It's just wasteful comparisons. 
>>>>
>>>> Justin 
>>>>
>>>> On Mon, Jun 25, 2018, 7:30 PM Marcus Ottosson <konstr...@gmail.com> 
>>>> wrote:
>>>>
>>>>> Sometimes. In the case of closeEvent, calling it would close the GUI 
>>>>> I think so you’d probably want your code to run first. In general, it 
>>>>> depends on whether you want something done before or after the superclass 
>>>>> implementation. In your case, someone overriding your mouseMoveEvent 
>>>>> would have to decide whether the code should run before or after 
>>>>> performing 
>>>>> the scrubbing. Sometimes it doesn’t matter, sometimes it does.
>>>>> ​
>>>>>
>>>>> On 25 June 2018 at 08:19, Panupat Chongstitwattana <panu...@gmail.com> 
>>>>> wrote:
>>>>>
>>>>>> I see. Does it matter if you call super first thing or last thing in 
>>>>>> the method override?
>>>>>>
>>>>>> On Monday, June 25, 2018 at 2:14:36 PM UTC+7, Marcus Ottosson wrote:
>>>>>>>
>>>>>>> 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 <
>>>>>>> panu...@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
>>>>>>>>>>>         i
>>>>>>>>>>>
>>>>>>>>>>> -- 
>> 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 <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/python_inside_maya/28081328-02ff-4d9a-b94b-77003355dbef%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/python_inside_maya/28081328-02ff-4d9a-b94b-77003355dbef%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/5cc87977-3e14-4c08-9b08-4125765feba9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to