Results 1 to 2 of 2

Thread: Problem regarding item selection/GraphicsItems/GraphicsScene and event propagation

  1. #1
    Join Date
    Jun 2007
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem regarding item selection/GraphicsItems/GraphicsScene and event propagation

    I have a scene with some items, when I click in the view/scene I want to perform some operations, like draw with the mouse etc., but when I click on an item I want to do stuff with that item. Problem is when I click near the edges of an item. The event is handled by both the item and my custom event handler.

    In my subclassed scene code i have(pseudo)
    Qt Code:
    1. mousePressed(event)
    2. {
    3. // send events to items
    4. QGraphicsScene::mousePressed(event);
    5. if(event->isAccepted())
    6. {
    7. return;
    8. }
    9. // send event to active state
    10. emit(myMousePressed(event));
    11. }
    To copy to clipboard, switch view to plain text mode 

    I'm fairly new to qt so maybe I've designed the custom event handler in a bad way.

    The way it's done is by a somewhat modified state pattern. I have some state classes that connects to the custom event signals. The active state object is determined by a setState in the graphicsscene subclass. When state A is active, A's code for e.g left mouse press is run. By doing it this way a massive switch/case is avoided.

    But again, it's the events which reach both items and my event handler that causes the trouble

    Any ideas or comments?

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem regarding item selection/GraphicsItems/GraphicsScene and event propagatio

    The simplest way that comes to mind is:
    Qt Code:
    1. mousePressEvent(event)
    2. {
    3. if( itemAt( event->pos()) )
    4. {
    5. //you clicked on an item
    6. }
    7. else
    8. {
    9. //no item there
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Using accepted() is a bit ambiguous. The item can reject the event, for example.

Similar Threads

  1. Graphics View Event Propagation
    By pherthyl in forum Qt Programming
    Replies: 10
    Last Post: 3rd July 2008, 10:39
  2. How to prevent Event Propagation to Parent?
    By viking in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2007, 06:29
  3. event problem
    By windkracht8 in forum Qt Programming
    Replies: 5
    Last Post: 17th August 2006, 11:52

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.