Results 1 to 14 of 14

Thread: Slider keyPressEvent handling

  1. #1
    Join Date
    Oct 2015
    Posts
    45
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Slider keyPressEvent handling

    I'm using a QSlider and I've sub classed it and implemented the keyPressEvent.

    I'm not entirely sure of the sequence of things, but, I can see with print statements that the keyReleaseEvent fire, then keyPressEvent, and subsequently the valueChanged signal gets fired.

    In this case, the up/down arrow key is of interest and is handled in the keyPressed event.

    Depending on other states in the program I need to decide if, in the valueChanged code, whether to update the associated data and send udp output or just update the data.

    Since the value changed code is in the main window and the keyRelease code in in the slider class how do I communicate between them to let the valueChange code make the decisions.

    Also, is it good practice to do a lot of work in the keyRelease that calls code in the main window code? For instance calling methods in children of the main window and/or setting values in the mainWindow.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Slider keyPressEvent handling

    It's easiest to just emit a signal (if QAbstractSlider::valueChanged() is not enough for you)
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2015
    Posts
    45
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Slider keyPressEvent handling

    valueChanged() is fine if all you want to do is know the value is changed.

    In this case I need to take different actions depending on whether the user has dragged the slider with the mouse, used the up/down key or has used other widgets that cause the value to change.

    I don't know how to tell the difference in valueChanged(), at least I have not found the way to tell.

    (There may well be a way, or I need to implement in a different way...I don't claim much experience in Qt. The docs and the examples always leave me wondering: "...ok, so that method does exactly what?, and when did it do it. )

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Slider keyPressEvent handling

    You can emit a custom signal (assuming you can subclass and replace the slider) or install an event filter on the given instance to spy on its events.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2015
    Posts
    45
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Slider keyPressEvent handling

    I already have subclass-ed the slider for other purposes.

    I've played with custom signals and event filters. But, I guess I don't grok enough that's it intuitive to me how to make it do what I need.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Slider keyPressEvent handling

    Show us what you had tried.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2015
    Posts
    45
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Slider keyPressEvent handling

    Leads back to my original question, since I think what I'm doing is not the best...

    At the moment I think my implementation is kludgey since I set a flag and check the flag in MySlider.

    The following is called after the ui is instantiated:
    Qt Code:
    1. def addChanStrip(self):
    2. self.max_slider_count = 32
    3. for idx in range(The_Show.mixers.__len__()):
    4. .
    5. .
    6. .
    7. for chn in range(The_Show.mixers[idx].mxrconsole.__len__()):
    8. .
    9. .
    10. .
    11. # Add slider for this channel
    12. sldr = MySlider() # slider with decibel ticks
    13. sldr.setContextMenuPolicy(Qt.CustomContextMenu)
    14. sldr.customContextMenuRequested.connect(self.sldr_action_click)
    15. sldr.sliderMoved.connect(self.slidermove)
    16. sldr.valueChanged.connect(self.slidervalchanged)
    17. sldr.sliderReleased.connect(self.sldrmovedone)
    18. sldr.sliderPressed.connect(self.slder_pressed)
    19. .
    20. .
    21. .
    22.  
    23. def slidervalchanged(self, val):
    24. sending_slider = self.sender()
    25. logging.info('In slidervalchanged, sending_slider name: {0}'.format(sending_slider.objectName()))
    26. logging.info('SliderDown is : {}'.format(sending_slider.isSliderDown()))
    27. if sending_slider.isSliderDown() or self.slider_keypress:
    28. self.cuehaschanged = True
    29. CE_msg = osc_message_builder.OscMessageBuilder(address='/cue/editstarted')
    30. CE_msg.add_arg(True)
    31. CE_msg = CE_msg.build()
    32. self.CEResponsethread.queue_msg(CE_msg, self.CueAppDev)
    33. .
    34. .
    35. .
    36. if sending_slider.isSliderDown() or self.slider_keypress:
    37. print('In slidervalchanged, sending_slider name: {0}, updating level state'.format(sending_slider.objectName()))
    38. self.updatecuelevelstate()
    39. if self.slider_keypress: self.slider_keypress = False # todo not sure setting flag in MySlider is good...
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class MySlider(QtWidgets.QSlider):
    2. def __init__(self, parent=None):
    3. super(MySlider, self).__init__(parent)
    4. .
    5. .
    6. .
    7. def keyPressEvent(self, k_ev):
    8. print('In keyReleaseEvent for slider {}'.format(self.objectName()))
    9. if k_ev.key() in [Qt.Key_Up, Qt.Key_Down, Qt.Key_PageDown, Qt.Key_PageUp]:
    10. self.window().slider_keypress = True
    11. QtWidgets.QSlider.keyPressEvent(self, k_ev)
    12. .
    13. .
    14. .
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Slider keyPressEvent handling

    And what is wrong with this implementation?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Oct 2015
    Posts
    45
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Slider keyPressEvent handling

    Well, I guess that is really my question.

    Is this a bodgy way of doing it, is there a better/preferred way, etc.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Slider keyPressEvent handling

    It's quite low-level but it is okay.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Oct 2015
    Posts
    45
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Slider keyPressEvent handling

    What would a higher level implementation be?

    Can you point me to example/s or docs that detail the approach (i.e. not just "this function does this and has these arguments", but something that educates on the what, how, who calls what when, etc.) (to me the examples in the Qt web pages are always too superficial, but maybe I just don't see how to interpret and extrapolate to more complex situations... )
    I'm asking because I've found no good sources...

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Slider keyPressEvent handling

    You would emit custom signals related to the logic of your application and then connect to those signals and put your logic there. It's a minor detail, like I said before.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Oct 2015
    Posts
    45
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Slider keyPressEvent handling

    So, emit a signal when?

    When there is a up/down arrow pressed? When the slider is moved by the the mouse, when the value is changed for viewing but, not for an actual change is state to the device being controlled?

    I guess I need to experiment with emitting a signal and responding to it before I can envision how to use it for my purposes.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Slider keyPressEvent handling

    Quote Originally Posted by drmacro View Post
    So, emit a signal when?
    When some interesting (in terms of your application logic) state is reached or event happens.

    When there is a up/down arrow pressed? When the slider is moved by the the mouse, when the value is changed for viewing but, not for an actual change is state to the device being controlled?
    I can't answer that question, I don't know what your application aims to do.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  15. The following user says thank you to wysota for this useful post:

    drmacro (31st August 2017)

Similar Threads

  1. Replies: 1
    Last Post: 17th June 2017, 22:59
  2. Dynamic label to right of slider resizes slider
    By zenzero-2001 in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2013, 10:11
  3. keypressevent
    By djwk in forum Newbie
    Replies: 9
    Last Post: 5th July 2010, 02:12
  4. Replies: 2
    Last Post: 21st March 2010, 09:01
  5. Handling of dead keys in keyPressEvent()
    By ghorwin in forum Qt Programming
    Replies: 4
    Last Post: 2nd December 2006, 12:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.