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. Trouble understanding custom QEvents
Qt 6.11 is out! See what's new in the release blog

Trouble understanding custom QEvents

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 3.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.
  • J Offline
    J Offline
    jdowner
    wrote on last edited by
    #1

    Hi,

    For the last couple of days I have been working on a problem where I want to post a custom event to be handled by custom QGraphicsTextItems. The reason I decided to create a custom event is that I am integrating a custom input device into our product and this seemed consistent with how other input 'events' are handled.

    Now, I have managed to get this to work, but not really the way I would like. So I am wondering if I am thinking about this problem the right way or if I just don't understand the way QEvents are used. I am calling,

    QApplication::sendEvent(&listener, &event)

    where the listener is the QObject that I want receiving the event. This doesn't seem right to me. I expected that I would pass the event to the application or parent and the event would filter down until it is handled. I don't really want to have to register each of my custom QGraphicsTextItems with the source of these events. I want to register the app or scene once, and have it dispatch the events to the appropriate children.

    Am I just expecting the Qt event system to do something it was not designed to do? Or have I just failed to find the article that would show me how this is done in Qt?

    Like I said, I have a solution to my problem, but I would like to understand the framework better.

    -Josh

    1 Reply Last reply
    0
    • L Offline
      L Offline
      loladiro
      wrote on last edited by
      #2

      [quote author="jdowner" date="1307980782"]
      Am I just expecting the Qt event system to do something it was not designed to do?
      [/quote]
      I think you are. AFAIK, there is no way to do what you want (I might be wrong here, I never had to use custom events to that extent). You can find a useful discussion of events "here":http://doc.qt.nokia.com/qq/qq11-events.html

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        First of all: there's a difference between SENDING an event and POSTING an event. See the aforementioned link for more details.

        Note also that input event dispatching in Qt is bottom-up, not top-down. They're sent directly to the "right" widget. If the event is not accepted, event() will return false and QApplication::notify will try sending the event to the parent widget. If you need to do the same for your custom events, subclass QApplication, override notify() and perform the sending going up in the hierarchy.

        QGraphicsView introduces another layer of indirection. When an event is posted for a view, it gets translated in a "scene" event, and sent to the scene. The scene, in turn, will dispatch it to the right item by calling its sceneEvent method, which will call the specialized handlers.

        Now, if you want to post your events to a QGraphicsItem, just use postEvent(view, event). Subclass QGraphicsView and handle that event in your event() implementation, by converting it in a scene event, and sending it to the scene. Subclass also QGraphicsScene, and in your event() pick the scene event and send it to the right item. Last, process the event in your item.

        I hardly suggest you to read the source code of the relevant classes methods, in order to see how it's done for "ordinary" Qt events (mouse, keyboard).

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          By default, custom events are only propagated to the specified receiver.
          So event propagation like for "normal" events will only work by subclassing QApplication.

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jdowner
            wrote on last edited by
            #5

            Thank you all for helping me to understand a little better!

            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