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. [solved] responding to events without deriving from Qt's widgets
Forum Updated to NodeBB v4.3 + New Features

[solved] responding to events without deriving from Qt's widgets

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.3k Views 1 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.
  • T3STYT Offline
    T3STYT Offline
    T3STY
    wrote on last edited by
    #1

    It's pretty annoying having to create every time custom widgets where we redefine the event() or mousePressEvent() methods, just for the sake of responding to some events. In my case I only need to respond when the left mouse button is pressed, but I have about 10 QPushButtons that should do different things when pressed... in this case I would end up having 10 different custom widgets derived from QPushButton... looks to me like a waste of time.

    So, is it possible to respond to events without having to derive Qt's widgets to custom ones ?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Yes, it's possible via event filters.
      You create a single QObject that implements "eventFilter()":http://qt-project.org/doc/qt-5.1/qtcore/qobject.html#eventFilter and then install it on whatever number of other objects with "installEventFilter()":http://qt-project.org/doc/qt-5.1/qtcore/qobject.html#installEventFilter
      Inside this eventFilter you can check what type of event it was, from which object it came from and act accordingly.

      But if you want to respond to events like mouse clicks then instead of overwriting mousePressEvent or creating filters you should just connect to clicked(), pressed() or released() signals.

      1 Reply Last reply
      0
      • T3STYT Offline
        T3STYT Offline
        T3STY
        wrote on last edited by
        #3

        Thank you very much! I solved by using the signal-slot connection.

        Unfortunately though I haven't found signals for double-click and right-click events. Did I miss them?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          There's no right click event or double click event simply because these are very unusual actions for buttons.
          If you need those you can subclass QPushButton (once!) and emit such custom signals. Then you can connect to different instances of that class like you would with click().

          1 Reply Last reply
          0
          • T3STYT Offline
            T3STYT Offline
            T3STY
            wrote on last edited by
            #5

            That was my idea as well, I just wanted to make sure I didn't miss them.
            Thank you very much for help ;)

            1 Reply Last reply
            0

            • Login

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