Results 1 to 17 of 17

Thread: Using custom events.

  1. #1
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Using custom events.

    Hello,

    i'm having this in my code:
    Qt Code:
    1. class AWBObject : public QGraphicsItem {
    2. public:
    3. // Event types.
    4. static const QEvent::Type HoverEnter = static_cast<QEvent::Type>(QEvent::User+1);
    5. static const QEvent::Type HoverLeave = static_cast<QEvent::Type>(QEvent::User+2);
    6. // Base event class.
    7. class Event : public QEvent {
    8. public:
    9. Event (QEvent::Type type) : QEvent(type) {};
    10. AWBObject *object;
    11. };
    12. // Hover enter event.
    13. class HoverEnterEvent : public AWBObject::Event {
    14. public:
    15. HoverEnterEvent () : Event(HoverEnter) {};
    16. };
    17. // Hover leave event.
    18. class HoverLeaveEvent : public AWBObject::Event {
    19. public:
    20. HoverLeaveEvent () : Event(HoverLeave) {};
    21. };
    22. [...]
    23. };
    To copy to clipboard, switch view to plain text mode 

    Then on a QGraphicItem "hoverEnterEvent" and "hoverLeaveEvent" i'm doing something like this:
    Qt Code:
    1. if (scene()) {
    2. HoverEnterEvent *toSend = new HoverEnterEvent();
    3. toSend->object = this;
    4. QCoreApplication::sendEvent((QObject*)scene(), toSend);
    5. }
    6. QGraphicsItem::hoverEnterEvent(evt);
    To copy to clipboard, switch view to plain text mode 

    Then in my scene subclass:
    Qt Code:
    1. void WBScene::customEvent (QEvent *evt) {
    2. switch (evt->type()) {
    3. case AWBObject::HoverEnter:
    4. _highlight = ((AWBObject::Event*)evt)->object;
    5. if (_highlight) _highlight->setHighlighted(true);
    6. return;
    7. case AWBObject::HoverLeave:
    8. _highlight = ((AWBObject::Event*)evt)->object;
    9. if (_highlight) _highlight->setHighlighted(false);
    10. return;
    11. default:
    12. return QGraphicsScene::customEvent(evt);
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    Saddly, the event isn't received by the scene. If i post a regular event (e.g. Just propagage the QGraphicsSceneHoverEvent), it's received by the scene in the event() method. i've tried both event() and customEvent() in the scene subclass, in either case the custom event is received.

    Note that i'm doing this after i instanciate my QApplication (And before execing the loop):
    Qt Code:
    1. QEvent::registerEventType(AWBObject::HoverEnter);
    2. QEvent::registerEventType(AWBObject::HoverLeave);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using custom events.

    Did you register the custom event?
    QEvent::registerEventType ( )
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using custom events.

    Hello High Flyer, and thank you for your time.

    Yes, i have registered my types (See the end of my post, last code block...).

    Pierre.

  4. #4
    Join Date
    Jun 2010
    Location
    India
    Posts
    50
    Thanks
    1
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using custom events.

    Hi ,

    Will this help ?

    Qt Code:
    1. HoverEnterEvent toSend;
    2. toSend.object = this;
    3. QCoreApplication::sendEvent((QObject*)scene(),&toSend);
    To copy to clipboard, switch view to plain text mode 

    If you call postEvent(), you must create the event object using new and Qt will automatically delete it after it is processed. If you call sendEvent(), you must create the event on the stack.

  5. #5
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using custom events.

    Really, it doesn't change anything. Thanks anyway. Anyone, UP?

  6. #6
    Join Date
    Jun 2010
    Location
    India
    Posts
    50
    Thanks
    1
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using custom events.

    Hi,
    It works perfectly for me. The Problem may be in your Scene's MouseMoveEvent. Please make sure QGraphicsScene::mouseMoveEvent(...) is called in your derrived Scene Class.

    Qt Code:
    1. void QLMyItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )
    2. {
    3. if (scene()) {
    4. MyEvent e(QLMyItem::HoverLeave);
    5. e.object = (void*)this;
    6. QCoreApplication::sendEvent((QObject*)scene(), &e);
    7. }
    8. QGraphicsItem::hoverLeaveEvent(event);
    9. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    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: Using custom events.

    Did you ask the view to deliver hover events to your item in the first place?
    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.


  8. #8
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using custom events.

    Hello, and thank you for your time.

    @Agathiyaa: My scene overrides these functions (mouseMoveEvent, Press, Release, DragEnter, DragLeave, Drop...). The problem clearly doesn't come from there, since i'm able to catch NON-CUSTOM events from my scene if my QGraphicsItem re-posts / re-sends it.

    @Wysota: My view delivers hoverring events. Again: my items receive the hover events. i can propagate them to the scene. Only, if i try to propagate my own events instead, it just never get catched by my scene.

    Thanks,
    Pierre.

  9. #9
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using custom events.

    Wow. i just tried something and it works. Actually i'm trying to post a HoverEnterEvent and a HoverLeaveEvent (Both derived from my Event class, and both calling the Event constructor with a different type). This didn't work. Now i have just tried to directly construct an Event and send it.

    Guess what... it works, and get catched as expected.

    May be a bug? Maybe Qt accepts subclasses of QEvent, but not sub-subclasses?

  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: Using custom events.

    Quote Originally Posted by hickscorp View Post
    May be a bug?
    Nope, an optimization.
    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
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using custom events.

    An Optimization? i'm not sure i understand... Now i've pointed it out, you know it's an optimization? Why not telling it in the doc then?... i really don't think it's an optimisation (When registering a new event type, it's based on it's type #ID, not it's class... If internally Qt does a type check based on the class, it makes no sense... i don't even think it's possible, since QEvent is not a subclass of QObject, and since my subclasses don't Q_OBJECT, it's not possible to get the superclass at runtime.

    Explain me, i really hate to get vague guesses

    Pierre.

  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: Using custom events.

    Quote Originally Posted by hickscorp View Post
    An Optimization? i'm not sure i understand...
    There is no point in tracking mouse position on the items (which requires coordinate transformations and collision detection) if you know the mouse is not in any of the views that hold the items. There is no point in tracking the mouse to detect hover events if you know the mouse is not over the item, etc. I'd assume it's exactly the same for widgets, although it might be a bit different as there is no scene equivalent for widgets so you'd post the event directly to the widget and this will probably just work (although Qt will not try to detect hover events by itself).

    Now i've pointed it out, you know it's an optimization?
    It's just logical to assume so. Qt is open source, you can easily verify my assumption by reading its source code.

    Why not telling it in the doc then?...
    If every detail was covered in the docs and every possible abuse of Qt was warned about in the docs, then the docs would have several gigabytes of volume and there would still be creative people who would invent new ways of abusing Qt.
    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
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using custom events.

    Quote Originally Posted by hickscorp View Post
    Now i have just tried to directly construct an Event and send it.

    Guess what... it works, and get catched as expected.
    Isnt it what hickscorp suggested to you in his post above? specifically the quaote from the doc?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #14
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using custom events.

    Quote Originally Posted by wysota View Post
    There is no point in tracking mouse position on the items (which requires coordinate transformations and collision detection) if you know the mouse is not in any of the views that hold the items. There is no point in tracking the mouse to detect hover events if you know the mouse is not over the item, etc. I'd assume it's exactly the same for widgets, although it might be a bit different as there is no scene equivalent for widgets so you'd post the event directly to the widget and this will probably just work (although Qt will not try to detect hover events by itself).
    It's just logical to assume so. Qt is open source, you can easily verify my assumption by reading its source code.
    If every detail was covered in the docs and every possible abuse of Qt was warned about in the docs, then the docs would have several gigabytes of volume and there would still be creative people who would invent new ways of abusing Qt.
    i'm not sure you see the problem.... The mouse is tracked, whatever if there is a point or not (QGraphicsItem has the hovering methods virtual, which allows subclassing + overriding). it's not a mouse tracking related problem. it's an custom event propagation problem. i could be trying to post ANY other kind of event (Even NOT hover-related), as long as they are custom, derived from a class itself derived from QEvent, it won't work.

    Try it yourself. Make a class A, derived from QEvent. Make a class B, derived from A. Register a custome event type assigned to either A or B. Post a B. (You'll never receive it in your target, whatever you're using post or send). Try this outside any QGraphics context, again: i'm really not talking about any hover / mouse / move thingy stuff.

    @high_flier: Sorry, i don't understand your answer. Are you misstaking me with me? You're saying i'm suggesting myself something? XD

  15. #15
    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: Using custom events.

    Only GUI-related events are propagated. Custom events are not GUI related.
    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.


  16. #16
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Using custom events.

    Quote Originally Posted by hickscorp View Post

    @high_flier: Sorry, i don't understand your answer. Are you misstaking me with me? You're saying i'm suggesting myself something? XD
    I meant agathiyaa.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  17. #17
    Join Date
    Jun 2010
    Location
    India
    Posts
    50
    Thanks
    1
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using custom events.

    Hi...

    I have found the exact problem in your code.

    Qt ( C++) allows / accepts subclasses of QEvent, Also sub-subclasses / throughout the inheritance hierarchy
    It seems "HoverEnter" & "HoverLeave" is defined or used somewhere else. If you try different name or
    Qt Code:
    1. class HoverEnterEvent : public AWBObject::Event {
    2. public:
    3. HoverEnterEvent () : Event(AWBObject::HoverEnter) {};
    4. };
    To copy to clipboard, switch view to plain text mode 
    it should work for you as is.(Note: AWBObject::HoverEnter)

    Please give a try and let us know. It worked for me.
    Last edited by agathiyaa; 17th June 2010 at 19:35.

Similar Threads

  1. Pass custom events to another process
    By TTGator in forum Qt Programming
    Replies: 9
    Last Post: 14th January 2009, 22:02
  2. Capture events in a custom delegate
    By vfernandez in forum Qt Programming
    Replies: 9
    Last Post: 19th March 2008, 05:33
  3. resizing events of a custom widget in a layout
    By Rooster in forum Qt Programming
    Replies: 7
    Last Post: 16th February 2008, 10:52
  4. custom events
    By momesana in forum Qt Programming
    Replies: 5
    Last Post: 30th January 2008, 23:20
  5. Posting custom events to a subclass of QThread
    By jpn in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2006, 15:49

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.