<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Why do I not get any MouseEvents in a QGraphicsItem derived class?]]></title><description><![CDATA[<p dir="auto">Hey,<br />
I have a QGraphicsView, which displays a QGraphicsScene in a tabWindget.<br />
In this scene I can add QGraphicsItems by clicking on it (class called Node).<br />
I want to receive mouseEvents in my QGraphicsItem derived class. I already found this solution: Just writing</p>
<pre><code>QGraphicsView::mousePressEvent(event);
</code></pre>
<p dir="auto">to the end of mousePressEvent-function in my QGraphicsView.<br />
Unfortunately it doesn't work. I receive no mousePressEvent in my Node class.<br />
My mousePressEvent in the QGraphicsView derived class:</p>
<pre><code>void GraphWidget::mousePressEvent(QMouseEvent *event){

    if (event-&gt;button() == Qt::RightButton) // doesn't matter I think
    {
        rightMousePressed = true;
        _panStartX = event-&gt;x();
        _panStartY = event-&gt;y();
        setCursor(Qt::ClosedHandCursor);
        event-&gt;accept();
        return;
    }
    QGraphicsView::mousePressEvent(event);
}
</code></pre>
<p dir="auto">I also tried to get the selected Node this way (in the QGraphicsView class also in the MousePressEvent-func):</p>
<pre><code>    Node *myNode = static_cast&lt;Node*&gt;(itemAt(event-&gt;pos()));
    qDebug() &lt;&lt; "selected node: " &lt;&lt; myNode;
    myNode-&gt;speak(); // speak() is a funtion in my Node class
    setSelectedNode(myNode);
</code></pre>
<p dir="auto">and this way (also in the QGraphicsView class also in the MousePressEvent-func) :</p>
<pre><code>    for(Node* i : all_nodes){
        if(i-&gt;isSelected()){
            setSelectedNode(i); // selectedNode is a Node* attribute of my QGraphicsView class
            i-&gt;speak();
            break;
        }
    }
</code></pre>
<p dir="auto">Both ways don't work. The program just crashes when I click to the scene.<br />
This is the constructor of my QGraphicsItem derived class:</p>
<pre><code>Node::Node(GraphWidget *graphWidgetInput)
{
    setFlag(ItemIsMovable);
    setFlag( ItemIsSelectable, true );
    setFlag(ItemSendsGeometryChanges);
    setCacheMode(DeviceCoordinateCache);
    setZValue(-1);
    graph = graphWidgetInput;
    width = 100;
    height = 100;
}
</code></pre>
<p dir="auto">Any ideas what I missed?<br />
Thanks for answers</p>
]]></description><link>https://forum.qt.io/topic/82046/why-do-i-not-get-any-mouseevents-in-a-qgraphicsitem-derived-class</link><generator>RSS for Node</generator><lastBuildDate>Sun, 14 Jun 2026 20:23:14 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/82046.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 04 Aug 2017 15:51:54 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Why do I not get any MouseEvents in a QGraphicsItem derived class? on Sun, 06 Aug 2017 15:29:40 GMT]]></title><description><![CDATA[<p dir="auto">The final answer for all these problems is there:<br />
<a href="https://forum.qt.io/topic/82085/qgraphicsview-mousereleaseevent-ensures-that-i-can-t-select-qgraphicsitems-anymore/2">https://forum.qt.io/topic/82085/qgraphicsview-mousereleaseevent-ensures-that-i-can-t-select-qgraphicsitems-anymore/2</a></p>
]]></description><link>https://forum.qt.io/post/408776</link><guid isPermaLink="true">https://forum.qt.io/post/408776</guid><dc:creator><![CDATA[Niagarer]]></dc:creator><pubDate>Sun, 06 Aug 2017 15:29:40 GMT</pubDate></item><item><title><![CDATA[Reply to Why do I not get any MouseEvents in a QGraphicsItem derived class? on Sat, 05 Aug 2017 10:55:56 GMT]]></title><description><![CDATA[<p dir="auto">@Wieland<br />
Ok, I reinstalled it and added the things I needed (I hope)  and made a new project with your code (and some additional lines) and it worked. But my big project still does not work and so far, I cant find the mistake. However I tried to do the procedure nearly the same as in your example, it does not work yet</p>
]]></description><link>https://forum.qt.io/post/408620</link><guid isPermaLink="true">https://forum.qt.io/post/408620</guid><dc:creator><![CDATA[Niagarer]]></dc:creator><pubDate>Sat, 05 Aug 2017 10:55:56 GMT</pubDate></item><item><title><![CDATA[Reply to Why do I not get any MouseEvents in a QGraphicsItem derived class? on Fri, 04 Aug 2017 21:13:23 GMT]]></title><description><![CDATA[<p dir="auto">I'm sorry to hear that :-/ But I'm sure it works with 5.8, too. I only mentioned 5.9 because I thought you might be using 4.7 or older.</p>
]]></description><link>https://forum.qt.io/post/408594</link><guid isPermaLink="true">https://forum.qt.io/post/408594</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 04 Aug 2017 21:13:23 GMT</pubDate></item><item><title><![CDATA[Reply to Why do I not get any MouseEvents in a QGraphicsItem derived class? on Fri, 04 Aug 2017 21:09:29 GMT]]></title><description><![CDATA[<p dir="auto">@Wieland<br />
Ok, thank you!<br />
I tried it in my project, it didn't work in version 5.8 so I downloaded 5.9.1 and now I'm unfortunately having problems with the Qt version. It says, this is Qt version 5.9.1 for Android (?) and it can't build and run my project. I downloaded and installed the normal version, not for Android... Phew I don't know, I will look and update when I have it (I hate it when I can't continue a project because of such unnessecary problems -.- )</p>
]]></description><link>https://forum.qt.io/post/408592</link><guid isPermaLink="true">https://forum.qt.io/post/408592</guid><dc:creator><![CDATA[Niagarer]]></dc:creator><pubDate>Fri, 04 Aug 2017 21:09:29 GMT</pubDate></item><item><title><![CDATA[Reply to Why do I not get any MouseEvents in a QGraphicsItem derived class? on Fri, 04 Aug 2017 21:14:03 GMT]]></title><description><![CDATA[<p dir="auto">Hi! The following works with Qt 5.9. It prints "Item" if you click on a custom item, and it prints "Nothing" if you click on empty space in the custom view.</p>
<p dir="auto"><strong>myview.h</strong></p>
<pre><code>#ifndef MYVIEW_H
#define MYVIEW_H

#include &lt;QGraphicsView&gt;

class MyView : public QGraphicsView
{
public:
    MyView(QWidget *parent = Q_NULLPTR);
protected:
    virtual void mousePressEvent(QMouseEvent *event) override;
};

#endif // MYVIEW_H
</code></pre>
<p dir="auto"><strong>myview.cpp</strong></p>
<pre><code>#include "myview.h"
#include &lt;QMouseEvent&gt;
#include &lt;QGraphicsItem&gt;
#include &lt;QDebug&gt;

MyView::MyView(QWidget *parent)
    : QGraphicsView(parent)
{
}

void MyView::mousePressEvent(QMouseEvent *event)
{
    if (!itemAt(event-&gt;pos())) {
        qDebug("Nothing");
    }
    QGraphicsView::mousePressEvent(event);
}
</code></pre>
<p dir="auto"><strong>myitem.h</strong></p>
<pre><code>#ifndef MYITEM_H
#define MYITEM_H

#include &lt;QGraphicsItem&gt;

class MyItem : public QGraphicsItem
{
public:
    MyItem(QGraphicsItem *parent = Q_NULLPTR);
    virtual QRectF boundingRect() const override;
    virtual void paint(QPainter *painter, QStyleOptionGraphicsItem const *, QWidget *) override;
protected:
    virtual void mousePressEvent(QGraphicsSceneMouseEvent *) override;
};

#endif // MYITEM_H
</code></pre>
<p dir="auto"><strong>myitem.cpp</strong></p>
<pre><code>#include "myitem.h"
#include &lt;QPainter&gt;
#include &lt;QDebug&gt;

MyItem::MyItem(QGraphicsItem *parent)
    : QGraphicsItem(parent)
{
}

QRectF MyItem::boundingRect() const
{
    auto const penWidth = 1.0;
    return QRectF(-10 - penWidth / 2, -10 - penWidth / 2, 20 + penWidth, 20 + penWidth);
}

void MyItem::paint(QPainter *painter, QStyleOptionGraphicsItem const *, QWidget *)
{
    painter-&gt;setBrush(QBrush(QColor("orange")));
    painter-&gt;setPen(QColor("lime"));
    painter-&gt;drawRect(-10, -10, 20, 20);
}

void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *)
{
    qDebug() &lt;&lt; "Item";
}
</code></pre>
]]></description><link>https://forum.qt.io/post/408572</link><guid isPermaLink="true">https://forum.qt.io/post/408572</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 04 Aug 2017 21:14:03 GMT</pubDate></item></channel></rss>