Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. make buttons process events
Qt 6.11 is out! See what's new in the release blog

make buttons process events

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Kent-DorfmanK Offline
    Kent-DorfmanK Offline
    Kent-Dorfman
    wrote on last edited by
    #1

    Been busy for a while so I haven't been on the site in forever, but I knew I'd receive some quality input for an issue I'm having so here goes.

    I have a scrollable panel of QToolButton subclassed widgets that each contain an image and my complication is that I want to do more with the buttons than a simple left-click. I want to accept modifiers with the click, like meta key or right-click instead. However, the QToolButton only processes the "clicked" slot, and without any params.

    I tried to wedge the desired behaviour in by reimplementing the keyReleaseEvent or mouseReleaseEvents but no joy. Id expect that if I reimplement either of those then I'd have to also call the correct parent version of the function as well, but when I do the interface locks up. I'm doing in pyqt5 as

    def mouseReleaseEvent(self, _e):
        # do some stuff if event involves right click
        self.parentWidget().mouseReleaseEvent(_e)
    

    What would be the correct way to handle a right click in a QToolButton subclassed widget?
    using qt5 under X11 in Debian 11

    The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

    1 Reply Last reply
    0
    • Kent-DorfmanK Kent-Dorfman

      @Pl45m4

      Thanks for the followup...

      Two collateral questions are important:

      1. must the virtual override also call the parent after doing in-class processing? I tried super.mouseReleaseEvent() and self.getParentWidget().mouseReleaseEvent() and both simply locked up the interface.

      2. what if I simply want to process right-click in a QAbstractButton subclass instead of keyboard event?

      yes, pyqt is pretty much a one for one in functionality to the C++ API.

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #4

      @Kent-Dorfman said in make buttons process events:

      @Pl45m4

      Thanks for the followup...

      Two collateral questions are important:

      1. must the virtual override also call the parent after doing in-class processing? I tried super.mouseReleaseEvent() and self.getParentWidget().mouseReleaseEvent() and both simply locked up the interface.

      What is getParentWidget()? If that's something like QWidget::parentWidget(), that would be a very unusual thing to do.

      Hopefully you mean super().mouseReleaseEvent(event), with super called as a function rather than used as an object.

      Call the base class version of the event handler if you want the functionality implemented by the base class. Don't call it if you want to prevent that functionality. Sometimes the documentation explains when this is necessary for particular widgets.

      1. what if I simply want to process right-click in a QAbstractButton subclass instead of keyboard event?

      There's no need to override keyboard events to get modifier keys. Either use the static QGuiApplication::keyboardModifiers() mentioned by @Pl45m4, or QInputEvent::modifiers() from the event passed into your mouse event handler.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      Kent-DorfmanK 1 Reply Last reply
      1
      • Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #2

        @Kent-Dorfman

        I want to accept modifiers with the click, like meta key or right-click instead

        Just off the top of my head, I would do it like you did and capture the key modifiers and/or pressed keys:

        • https://doc.qt.io/qt-5/qguiapplication.html#keyboardModifiers

        In mouseReleaseEvent

        Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
        // if key x, y, z pressed while click happens,
        // do something
        

        (I know it's C++ code but should also work in PyQt)


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        Kent-DorfmanK 1 Reply Last reply
        0
        • Pl45m4P Pl45m4

          @Kent-Dorfman

          I want to accept modifiers with the click, like meta key or right-click instead

          Just off the top of my head, I would do it like you did and capture the key modifiers and/or pressed keys:

          • https://doc.qt.io/qt-5/qguiapplication.html#keyboardModifiers

          In mouseReleaseEvent

          Qt::KeyboardModifiers modifiers = QGuiApplication::keyboardModifiers();
          // if key x, y, z pressed while click happens,
          // do something
          

          (I know it's C++ code but should also work in PyQt)

          Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #3

          @Pl45m4

          Thanks for the followup...

          Two collateral questions are important:

          1. must the virtual override also call the parent after doing in-class processing? I tried super.mouseReleaseEvent() and self.getParentWidget().mouseReleaseEvent() and both simply locked up the interface.

          2. what if I simply want to process right-click in a QAbstractButton subclass instead of keyboard event?

          yes, pyqt is pretty much a one for one in functionality to the C++ API.

          The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

          jeremy_kJ 1 Reply Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            @Pl45m4

            Thanks for the followup...

            Two collateral questions are important:

            1. must the virtual override also call the parent after doing in-class processing? I tried super.mouseReleaseEvent() and self.getParentWidget().mouseReleaseEvent() and both simply locked up the interface.

            2. what if I simply want to process right-click in a QAbstractButton subclass instead of keyboard event?

            yes, pyqt is pretty much a one for one in functionality to the C++ API.

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #4

            @Kent-Dorfman said in make buttons process events:

            @Pl45m4

            Thanks for the followup...

            Two collateral questions are important:

            1. must the virtual override also call the parent after doing in-class processing? I tried super.mouseReleaseEvent() and self.getParentWidget().mouseReleaseEvent() and both simply locked up the interface.

            What is getParentWidget()? If that's something like QWidget::parentWidget(), that would be a very unusual thing to do.

            Hopefully you mean super().mouseReleaseEvent(event), with super called as a function rather than used as an object.

            Call the base class version of the event handler if you want the functionality implemented by the base class. Don't call it if you want to prevent that functionality. Sometimes the documentation explains when this is necessary for particular widgets.

            1. what if I simply want to process right-click in a QAbstractButton subclass instead of keyboard event?

            There's no need to override keyboard events to get modifier keys. Either use the static QGuiApplication::keyboardModifiers() mentioned by @Pl45m4, or QInputEvent::modifiers() from the event passed into your mouse event handler.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            Kent-DorfmanK 1 Reply Last reply
            1
            • jeremy_kJ jeremy_k

              @Kent-Dorfman said in make buttons process events:

              @Pl45m4

              Thanks for the followup...

              Two collateral questions are important:

              1. must the virtual override also call the parent after doing in-class processing? I tried super.mouseReleaseEvent() and self.getParentWidget().mouseReleaseEvent() and both simply locked up the interface.

              What is getParentWidget()? If that's something like QWidget::parentWidget(), that would be a very unusual thing to do.

              Hopefully you mean super().mouseReleaseEvent(event), with super called as a function rather than used as an object.

              Call the base class version of the event handler if you want the functionality implemented by the base class. Don't call it if you want to prevent that functionality. Sometimes the documentation explains when this is necessary for particular widgets.

              1. what if I simply want to process right-click in a QAbstractButton subclass instead of keyboard event?

              There's no need to override keyboard events to get modifier keys. Either use the static QGuiApplication::keyboardModifiers() mentioned by @Pl45m4, or QInputEvent::modifiers() from the event passed into your mouse event handler.

              Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote on last edited by
              #5

              @jeremy_k

              Thanks for super()...It's been a while since I've had to program and had not considered that the super needed to be called as a function. That solved my hanging problem.

              keyboard events are not an issue now since I can properly trap the right-click now with mouseReleaseEvent()

              The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

              1 Reply Last reply
              0
              • Kent-DorfmanK Kent-Dorfman has marked this topic as solved on

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved