Results 1 to 4 of 4

Thread: Mouse press event in a QGraphicsScene

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Mouse press event in a QGraphicsScene

    Hi,
    an old problem is bothering me and I can't get it fixed. After double clicking a custom QGraphicsTextItem, I want to move the cursor of the QTextDocument to the position under the cursor. This is the behavior you can achieve if you press three times the mouse button. So I thought just send a mouse event, but I fail. Here my files:

    Qt Code:
    1. #include <QtGui>
    2. #include "myitem.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. MyItem *it = new MyItem();
    9. it->setPlainText("String to edit");
    10.  
    11. scene.addItem(it);
    12.  
    13. QGraphicsView view(&scene);
    14. view.show();
    15.  
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef MYITEM_H
    2. #define MYITEM_H
    3.  
    4. #include <QtGui>
    5.  
    6. class MyItem : public QGraphicsTextItem
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MyItem(QGraphicsItem *parent = 0);
    12.  
    13. protected:
    14. void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
    15. void focusOutEvent(QFocusEvent *event);
    16. };
    17.  
    18. #endif /* MYITEM_H */
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "myitem.h"
    2.  
    3. MyItem::MyItem(QGraphicsItem *parent)
    4. {
    5. setTextInteractionFlags(Qt::LinksAccessibleByMouse);
    6. }
    7.  
    8. void MyItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
    9. {
    10. if (textInteractionFlags() & Qt::TextEditorInteraction)
    11. {
    12. QGraphicsTextItem::mouseDoubleClickEvent(event);
    13. }
    14. else
    15. {
    16. setTextInteractionFlags(Qt::TextEditorInteraction);
    17. // HERE SOME MAGIC CODE TO MOVE THE CURSOR
    18. // which is by default at 0 after calling setTextInteractionFlags(Qt::TextEditorInteraction).
    19. }
    20. }
    21.  
    22. void MyItem::focusOutEvent(QFocusEvent *event)
    23. {
    24. setTextInteractionFlags(Qt::LinksAccessibleByMouse);
    25. QGraphicsTextItem::focusOutEvent(event);
    26. }
    To copy to clipboard, switch view to plain text mode 

    I need to set the editor functionability only after a double click. All attempts to redirect the event leads in a total selection of the text. And creating different QMouseEvents and send them via QCoreApplication::sendEvent ended somewhere in Nirvana...

    Any hints are welcome. Even code, as you know I am to lazy to do the work by my own, and it's your job to do my work...

    Thanks,

    Lykurg

  2. #2
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse press event in a QGraphicsScene

    Hi,
    my idea was to directly call QGraphicsItem::mousePressEvent. For this, you need to create a QGraphicsSceneMouseEvent. According to the Qt API Docs, there seems to be no way, but after a peek into the source code of QGraphicsScene, I came up with the following:

    Qt Code:
    1. void MyItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
    2. {
    3. if (textInteractionFlags() & Qt::TextEditorInteraction)
    4. {
    5. QGraphicsTextItem::mouseDoubleClickEvent(event);
    6. }
    7. else
    8. {
    9. setTextInteractionFlags(Qt::TextEditorInteraction);
    10. // HERE SOME MAGIC CODE TO MOVE THE CURSOR
    11. QGraphicsSceneMouseEvent newEvent(QEvent::GraphicsSceneMousePress);
    12. newEvent.setScenePos(event->scenePos());
    13. newEvent.setPos(event->pos());
    14. newEvent.setButtons(event->buttons());
    15. newEvent.setButton(event->button());
    16. newEvent.setModifiers(event->modifiers());
    17. QGraphicsTextItem::mousePressEvent(&newEvent);
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    It's a bit nasty, since it uses non-API public members of QGraphicsSceneMouseEvent (marked as 'internal'), but it works...

  3. The following user says thank you to shentian for this useful post:

    Lykurg (19th June 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Mouse press event in a QGraphicsScene

    Quote Originally Posted by shentian View Post
    It's a bit nasty, since it uses non-API public members of QGraphicsSceneMouseEvent (marked as 'internal'),
    I intended to avoid use internal stuff...
    but it works...
    ...yeah, what the heck, what works works, I will use that. By the way I also guess that they wont change so much on that.
    Thanks very much, that you have spend your time looking through the sources, to get a solution for my problem, I appreciate that!

    But I am still confused why something link that don't work:
    Qt Code:
    1. QMouseEvent ne(QEvent::MouseButtonPress, event->pos().toPoint(), event->button(), event->buttons(), event->modifiers());
    2. QApplication::sendEvent(this, &ne);
    To copy to clipboard, switch view to plain text mode 

    The event is delivered to MyItem, says my eventFilter, but the reimp MyItem::mousePressEvent() isn't invoked. Also used QCursor:: pos(), Qt::LeftButton...


    Lykurg

  5. #4
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse press event in a QGraphicsScene

    MyItem::mousePressEvent() isn't invoked because you are sending a QMouseEvent, not a QGraphicsSceneMouseEvent. And sadly, API doesn't allow to create a QGraphicsSceneMouseEvent

    Maybe you can try to send a QMouseEvent to the QGraphicsView, which will translate it to a QGraphicsSceneMouseEvent.

    I tried this, but it doesn't seem to work either:

    Qt Code:
    1. QGraphicsView* view = scene()->views().first();
    2. QMouseEvent ne(QEvent::MouseButtonPress, view->mapFromGlobal(event->screenPos()), event->button(), event->buttons(), event->modifiers());
    3. QApplication::sendEvent(view, ne);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Mouse press event detection
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2009, 13:08
  2. Checking for key press on mouse event
    By Cruz in forum Newbie
    Replies: 1
    Last Post: 24th January 2009, 18:18
  3. Replies: 2
    Last Post: 2nd April 2008, 14:19
  4. Replies: 1
    Last Post: 24th October 2007, 18:34
  5. Draw QtCanvasElipse on mouse press event position
    By YuriyRusinov in forum Newbie
    Replies: 1
    Last Post: 31st May 2006, 11:57

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.