QtWidgets.QScrollBar action is int not SliderAction enum

Barry Scott barry at barrys-emacs.org
Wed Jul 6 10:35:03 BST 2022



> On 5 Jul 2022, at 11:48, Phil Thompson <phil at riverbankcomputing.com> wrote:
> 
> On 05/07/2022 10:54, Barry Scott wrote:
>> I have code like this:
>> class BemacsVerticalScrollBar(QtWidgets.QScrollBar, be_debug.EmacsDebugMixin):
>>    def __init__( self, panel ):
>>        super().__init__( QtCore.Qt.Orientation.Vertical, panel )
>>        self.actionTriggered.connect( self.handleActionTriggered )
>>    def handleActionTriggered( self, action ):
>>        # action is an int - convert to its enum
>>        action = self.SliderAction( action )
>>        # now can compare to the enum values
>>        if action == self.SliderAction.SliderSingleStepAdd:
>> The action param is an int and not one of the enum values from SliderAction.
>> Is this intended?
> 
> Yes. There are a number of cases that Qt uses int as a signal argument when the value is always an enum member. There are other cases where the signature (IMHO correctly) specifies an enum.
> 
> I agree that the behaviour is surprising but I prefer not to try and "fix" the Qt API. Other people have taken the approach of reporting it as a Qt bug, but I don't know if this has been successful.
> 
> Note that you can simplify your code by comparing your original 'action' with the enum member's 'value' attribute.

The code uses a lot of comparisons and I prefer the look of the code without the .value in lots of places.
The use of .value would be a good solution if it is a one off comparison I feel.

Barry

> 
> Phil
> 



More information about the PyQt mailing list