Results 1 to 5 of 5

Thread: Mouse event on a QGraphicsItem that is not in it's own class

  1. #1
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Mouse event on a QGraphicsItem that is not in it's own class

    Let's say I create a QGraphicsItem (rectangle) in a QGraphicsScene (scene), without creating a class for the item:

    Constructur of Mainwindow (.cpp):
    Qt Code:
    1. ui->setupUi(this);
    2. scene = new QGraphicsScene(this);
    3. ui->graphicsView->setScene(scene);
    4. rectangle = scene->addRect(0,0,100,100);
    To copy to clipboard, switch view to plain text mode 

    Is there a way to overwrite a mouse click on the rectangle and make something happen, even if it is not in another class?
    What about showing a tooltip when hovering over it?

    I hope the question is clear and not completely redundant.
    Last edited by xzyerasu; 26th December 2012 at 18:52.

  2. #2
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse event on a QGraphicsItem that is not in it's own class

    I can think of one way of doing so without subclassing. You may create class that will be filtering mouse events and install it on the QGraphicsView via QObject::installEventFilter,
    (you can't install it onto item itself, because it is not derived from QObject). But if you go this way you will be forced to map coordinates from view to item.

    IMHO subclassing QGraphicsRectItem is much more clearer and easy way. It's not that hard! You'll need to override just a few protected methods.

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

    xzyerasu (28th December 2012)

  4. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    658
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mouse event on a QGraphicsItem that is not in it's own class

    Quote Originally Posted by xzyerasu View Post
    Let's say I create a QGraphicsItem (rectangle) in a QGraphicsScene (scene), without creating a class for the item:

    Constructur of Mainwindow (.cpp):
    Qt Code:
    1. ui->setupUi(this);
    2. scene = new QGraphicsScene(this);
    3. ui->graphicsView->setScene(scene);
    4. rectangle = scene->addRect(0,0,100,100);
    To copy to clipboard, switch view to plain text mode 

    Is there a way to overwrite a mouse click on the rectangle and make something happen, even if it is not in another class?
    What about showing a tooltip when hovering over it?

    I hope the question is clear and not completely redundant.
    u need to set void QGraphicsItem::setToolTip ( const QString & toolTip ) of the graphicsItem ... if u want to use the hover event hoverEnterEvent ( QGraphicsSceneHoverEvent * ) then u override the graphicsItem instead of using eventFilter ..
    "Behind every great fortune lies a crime" - Balzac

  5. The following user says thank you to wagmare for this useful post:

    xzyerasu (28th December 2012)

  6. #4
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Mouse event on a QGraphicsItem that is not in it's own class

    Thank you, it worked great wit setToolTip. Now however, do you also know if there is a way to use the mouse clicks on this item? (to open a dialog box for example)

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Mouse event on a QGraphicsItem that is not in it's own class

    If you want, you can handle all events in the scene or in the view.
    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.


Similar Threads

  1. Ogre+qt mouse event (add object with mouse problem)
    By rimie23 in forum Qt Programming
    Replies: 7
    Last Post: 24th April 2012, 10:49
  2. Replies: 3
    Last Post: 7th January 2012, 08:38
  3. QGraphicsItem can't receive mouse leave/enter event
    By wshn13 in forum Qt Programming
    Replies: 1
    Last Post: 22nd June 2011, 08:32
  4. Do the layout class can receive the mouse event?
    By tszzp in forum Qt Programming
    Replies: 3
    Last Post: 23rd December 2009, 08:07
  5. can not get mouse double click event for QGraphicsItem
    By learning_qt in forum Qt Programming
    Replies: 2
    Last Post: 14th September 2009, 20:36

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.