70371

QGraphicsPixmapItem mouseMoveEvent when not grabbing

Question:

I am working on a C++ application with Qt 4.8 as the GUI framework: A subclassed QGraphicsView rendering a subclassed QGraphicsScene containing one or more subclassed QGraphicsPixMapItem(s) in which I need to know the mouse cursor's relative pixel position.

Following <a href="https://stackoverflow.com/questions/9163271/how-to-show-pixel-position-and-color-from-a-qgraphicspixmapitem" rel="nofollow">How to show pixel position and color from a QGraphicsPixmapItem</a> I subclass <a href="http://qt-project.org/doc/qt-4.8/qgraphicspixmapitem.html" rel="nofollow">QGraphicsPixmapItem</a>:

class MyGraphicsPixmapItem : public QObject, public QGraphicsPixmapItem { Q_OBJECT public: explicit MyGraphicsPixmapItem(QGraphicsItem *parent = 0); signals: public slots: void mouseMoveEvent(QGraphicsSceneMouseEvent * event); void mousePressEvent(QGraphicsSceneMouseEvent *event); };

and implement constructor and handler:

MyGraphicsPixmapItem::MyGraphicsPixmapItem(QGraphicsItem *parent) : QGraphicsPixmapItem(parent) { qDebug() << "constructor mygraphicspixmapitem"; setCacheMode(NoCache); setAcceptHoverEvents(true); setFlag(QGraphicsItem::ItemIsSelectable,true); } void MyGraphicsPixmapItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) { QPointF mousePosition = event->pos(); qDebug() << "mouseMoveEvent"; qDebug() << mousePosition; QGraphicsPixmapItem::mouseMoveEvent(event); }

I have also subclassed QGraphicsView and QGraphicsScene with respective mouseMoveEvent handlers that pass the event further down successfully:

void MyGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { qDebug() << "QGraphicsScene: mouseMoveEvent"; QGraphicsScene::mouseMoveEvent(event); }

However, although MyGraphicsView is configured with ...

setMouseTracking(true); setInteractive(true);

... MyGraphicsPixmapItem only executes the mouseMoveEvent handler when being clicked or after setting it as grabber:

void MyGraphicsPixmapItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { qDebug() << QString("item clicked at: %1, %2").arg( event->pos().x()).arg( event->pos().y() ); event->accept(); grabMouse(); }

I would like to know whether I am making a mistake or the solution given in the above-mentioned answer is flawed or incomplete. Further, I would like to know whether the mouseMoveEvent handler can be triggered without the QGraphicsPixmapItem being configured as 'grabber'. It is also acceptable if the configuration as grabber occurs automatically when the mouse first moves onto the item.

Here is a <a href="http://www.qtcentre.org/threads/38014-Problem-getting-mouse-events-from-overrider-QGraphicsPixmapItem" rel="nofollow">thread</a> where a very similar problem seems to be related to the item's reference, but since I add the item to the scene like this:

thepixMapItem = new MyGraphicsPixmapItem(); scene->addItem(thepixMapItem);

I suppose this is not related.

Answer1:

The solution is to reimplement or filter the item's <a href="http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#hoverMoveEvent" rel="nofollow">hoverMoveEvent</a>, as <a href="http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#mouseMoveEvent" rel="nofollow">mouseMoveEvent</a> only triggers when receiving a mouse press:

<blockquote>

If you do receive this event, you can be certain that <strong>this item also received a mouse press event</strong>, and that this item is the current mouse grabber.

</blockquote>

Recommend

  • How to handle mouse events in Qt?
  • PySide/PyQt Overlay widget
  • How can i use Django smart select to filter ManyToManyField?
  • How to implement arriving behavior with time delta?
  • Any command in mysql equivalent to Oracle's autotrace for performance turning
  • Find unique tuples in a relation represented by a BDD
  • How can I create a plugin mechanism that calls functions only when the plugin is available?
  • Delete std::shared_ptr without destroying the managed object?
  • How to get the index of element in the List in c#
  • What exactly is the height of modalPresentationStyle - FormSheet on iPad?
  • Retrieve Facebook Account Information in ios 6
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • MonadTransControl instance for ProxyFast/ProxyCorrect
  • Leaflet z-index
  • Client side validation mvc dropdown
  • Java making confirming exit
  • Why people use prototype in javascript when it is easy to inherit using apply () and call () methods
  • Implement JwtBearer Authentication in NSwag SwaggerUi
  • unable to get jsonEncode in magento2
  • Angular2 - Template reference inside NgSwitch
  • abstracting over a collection
  • How to explicitly/implicitly implemented interface members in C++/CLI?
  • How to programatically 'login' a user based on 'remember me' cookie when using j
  • android google indoor map
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • Simulate click Geckofx vb,net
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • How can I set a binding to a Combox in a UserControl?
  • Insert new calendar with SyncAdapter- Calendar API Android
  • Django simple Captcha “No module named fields” error
  • Create DicomImage from scratch using Dcmtk
  • AJAX Html Editor Extender upload image appearing blank
  • d3 v4 drag and drop with TypeScript
  • JQuery Internet Explorer and ajaxstop
  • C# - Serializing and deserializing static member
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Bug in WPF DataGrid
  • Incrementing object id automatically JS constructor (static method and variable)
  • Angular 2 constructor injection vs direct access
  • Linking SubReports Without LinkChild/LinkMaster