Results 1 to 11 of 11

Thread: Graphics View Event Propagation

  1. #1
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Graphics View Event Propagation

    This may be a basic question, but I'm really having trouble with mouse event propagation with Graphics View.

    My problem is that I have a box with some text in it. So a QGraphicsItem with a QGraphicsTextItem as the child. Well the box tracks mouse hover events, but for some reason the QGraphicsTextItem swallows those events. So when the mouse is over the text item, my box doesn't get hover events. While I don't know why QGraphicsTextItem doesn't pass those events along (with NoTextInteraction set), it at least makes sense that the topmost item gets the events first.
    Now when I set TextEditorInteraction on the QGraphicsTextItem, I can type and click the cursor around, but I can't click and drag to highlight text. Those events are passed on to the box, so I end up moving the box when I really want to select text instead. Also, the QGraphicsView mousePressed method seems to get called first, even though the mouse press occurred on a QGraphicsItem. Shouldn't the view only get the mouse press if it is ignored by all the QGraphicsItems that are under the mouse?

    All in all I'm very confused about how events work in Graphics View. Any help would be greatly appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Graphics View Event Propagation

    Quote Originally Posted by pherthyl View Post
    While I don't know why QGraphicsTextItem doesn't pass those events along (with NoTextInteraction set), it at least makes sense that the topmost item gets the events first.
    It handles them by itself, so they don't get propagated. It's just it doesn't provide any interaction as you asked it not to.

    Now when I set TextEditorInteraction on the QGraphicsTextItem, I can type and click the cursor around, but I can't click and drag to highlight text. Those events are passed on to the box, so I end up moving the box when I really want to select text instead.
    Hard to explain that without knowing how your scene is set up. A small compilable example would help pinpoint the problem.

    Also, the QGraphicsView mousePressed method seems to get called first, even though the mouse press occurred on a QGraphicsItem.
    The item probably doesn't handle mouse press events, so they get propagated to the scene and then to the view.

    Shouldn't the view only get the mouse press if it is ignored by all the QGraphicsItems that are under the mouse?
    That's probably exactly what happens.

    All in all I'm very confused about how events work in Graphics View. Any help would be greatly appreciated.
    Events is one thing, interactions are another - your items probably have a Movable flag set, so all mouse move events cause items to be moved (that's why the box moves instead of the text being dragged).

  3. #3
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graphics View Event Propagation

    Thanks for the reply.

    Quote Originally Posted by wysota View Post
    It handles them by itself, so they don't get propagated. It's just it doesn't provide any interaction as you asked it not to.
    So if I don't want the QGraphicsTextItem to swallow the hover events (it doesn't do anything with them anyway so I don't see why it doesn't ignore() them), do I have to install an event filter on it?

    Quote Originally Posted by wysota View Post
    Events is one thing, interactions are another - your items probably have a Movable flag set, so all mouse move events cause items to be moved (that's why the box moves instead of the text being dragged).
    Yeah, I understand that part, I just don't know why I'm getting the events in the first place. If TextEditorInteraction is set on the QGraphicsTextItem, then the text in it should be selectable by the mouse. In other words, shouldn't it be grabbing all the mouse events and not passing them on to its parent?
    Basically what confuses me is that QGraphicsTextItem swallows the hover events (where I don't want it to) and then passes on the mouse press events (which I also don't want). If someone is clicking around in the QGraphicsTextItem, I don't want all those mouse events to be passed on to the parent of the text item.. If that makes any sense

  4. #4
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graphics View Event Propagation

    If I add a QGraphicsTextItem to a scene, like so:

    QGraphicsTextItem* ti = new QGraphicsTextItem("test");
    ti->setTextInteractionFlags(Qt::TextEditorInteraction );
    addItem(ti);

    I can edit text fine with the keyboard, and clicking the mouse will move the cursor there. But click drag to highlight still doesn't work. The text item doesn't accept the event, so it just gets passed on to the scene and view, which then draws a rubberband. But no selection in the text item.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Graphics View Event Propagation

    Quote Originally Posted by pherthyl View Post
    So if I don't want the QGraphicsTextItem to swallow the hover events (it doesn't do anything with them anyway so I don't see why it doesn't ignore() them), do I have to install an event filter on it?
    No, subclass, reimplement the event handler in it and ignore() the event there. It'll get propagated to the parent then (but you won't be able to move the item using mouse).

    Yeah, I understand that part, I just don't know why I'm getting the events in the first place. If TextEditorInteraction is set on the QGraphicsTextItem, then the text in it should be selectable by the mouse. In other words, shouldn't it be grabbing all the mouse events and not passing them on to its parent?
    Maybe the movable flag is checked first? You'd have to look at the source code if the base implementation is called in the beginning or in the end of the handler...

    Basically what confuses me is that QGraphicsTextItem swallows the hover events (where I don't want it to) and then passes on the mouse press events (which I also don't want). If someone is clicking around in the QGraphicsTextItem, I don't want all those mouse events to be passed on to the parent of the text item.. If that makes any sense
    I don't know what your "box" item does, but maybe it'd be simpler to subclas QGraphicsTextItem and modify its behaviour so that it draws the frame?

  6. #6
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graphics View Event Propagation

    Quote Originally Posted by wysota View Post
    No, subclass, reimplement the event handler in it and ignore() the event there. It'll get propagated to the parent then (but you won't be able to move the item using mouse).
    Hmm, I tried this, but the event still does not get propagated to the parent. Very strange. I get the event in the QGraphicsTextItem subclass, and properly ignore() it, but it still never reaches the QGraphicsItem behind it.

    I don't know what your "box" item does, but maybe it'd be simpler to subclas QGraphicsTextItem and modify its behaviour so that it draws the frame?
    Well that wouldn't be easily possible, but I found a another way to work around the problems. Now I just set setHandlesChildEvents(true) on the parent item when I don't want the textitem to be editable, and when I do, I clear that flag. Selecting text with the mouse still doesn't work though. Maybe I'll start another thread for that problem to avoid confusion.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: Graphics View Event Propagation

    Quote Originally Posted by pherthyl View Post
    Hmm, I tried this, but the event still does not get propagated to the parent.
    It's almost not possible. You might have done something wrong. The only possibility is that there is an event filter applied on the item and it handles the event instead of the regular handler.

    Very strange. I get the event in the QGraphicsTextItem subclass, and properly ignore() it, but it still never reaches the QGraphicsItem behind it.
    In that case there is no filter Is the graphics item behind it the parent of the text item?

  8. #8
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Graphics View Event Propagation

    Quote Originally Posted by wysota View Post
    It's almost not possible. You might have done something wrong.
    Quite likely yes

    In that case there is no filter Is the graphics item behind it the parent of the text item?
    Yes. Does being the parent make events not propagate?

  9. #9
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Graphics View Event Propagation

    hmm... looks quite confusing.. I had a look at the QGraphicsItem::sceneEvent() and i found this
    Qt Code:
    1. bool QGraphicsItem::sceneEvent(QEvent *event)
    2. {
    3. if (d_ptr->ancestorFlags & QGraphicsItemPrivate::AncestorHandlesChildEvents) {
    4. if (event->type() == QEvent::HoverEnter || event->type() == QEvent::HoverLeave
    5. || event->type() == QEvent::DragEnter || event->type() == QEvent::DragLeave) {
    6. // Hover enter and hover leave events for children are ignored;
    7. // hover move events are forwarded.
    8. return true;
    9. }
    To copy to clipboard, switch view to plain text mode 
    May be this is causing some side effect since you are igonoring the hover event. Not sure though. IMHO you should post a simple example. It will make us understand in a better way.

    BTW did you try calling
    Qt Code:
    1. QGraphicsTextItem::setAcceptHoverEvents(false);
    To copy to clipboard, switch view to plain text mode 
    EDIT: If you are doing the above there is no need to reimplement hoverEvent methods as far as i have understood.
    Note that QGraphicsTextItem enables hover events while constructing.
    Also call setAcceptHoverEvents(true) on your parent item otherwise the handlers are not called.
    Its worth to have a look at the documentation of QGraphicsItem::setAcceptsHoverEvents ( )
    Last edited by Gopala Krishna; 12th August 2007 at 07:14.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #10
    Join Date
    Jan 2006
    Location
    Norway
    Posts
    124
    Thanked 38 Times in 30 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Graphics View Event Propagation

    If someone can show a sample subclass that ignores the event, it'll be easier to see what's going on. The code in sceneEvent() is for filtering away enter and leave events for items that handle child events - it's a special thing for hover events to let you track the mouse motion over all children, avoiding having to check whether enter and leave events were for the parent or the child, or whatever :-). Hover events and mouse events are handled differently, though...
    Bitto / Andreas Aardal Hanssen - andreas dot aardal dot hanssen at nokia
    Nokia Software Manager, Qt Development

  11. #11

    Default Re: Graphics View Event Propagation

    Hmm I was confused by this for a while, but it looks like the parent object needs to have the flags:

    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);

    in order for the text interaction to work right.

Similar Threads

  1. Qt Coordinate System and the Graphics View Framework
    By djurodrljaca in forum Qt Programming
    Replies: 14
    Last Post: 17th February 2012, 11:19
  2. Graphics view display problem.
    By kiranraj in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 07:08
  3. graphics view FitInView problem
    By aamer4yu in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2007, 10:24
  4. which is better QCanvas or graphics view?
    By neomax in forum General Discussion
    Replies: 1
    Last Post: 23rd November 2006, 15:19
  5. Adding Rectangular overlay on graphics view
    By forrestfsu in forum Qt Programming
    Replies: 10
    Last Post: 21st November 2006, 19:42

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.