<?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[QWidget::mousePressEvent() not called]]></title><description><![CDATA[<p dir="auto">I'm working on a TreeViewNode class derived from QObject and QGraphicsItem, responsible for creating the individual nodes of the tree view (which displays a family tree) and adding them to the scene. I made sure to include the <code>setFlag(QGraphicsItem::ItemIsSelectable);</code> method in the class constructor and I overloaded mousePressEvent like so:</p>
<pre><code>void TreeViewNode::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    if (event-&gt;button() == Qt::LeftButton) {
        qDebug() &lt;&lt; "Mouse pressed on node: " &lt;&lt; m_node-&gt;getPatient()-&gt;get_Name().c_str();
        emit clicked(this);
    }
    QGraphicsItem::mousePressEvent(event);
}
</code></pre>
<p dir="auto">The clicked signal is defined in the header file as <code>void clicked(TreeViewNode* node);</code><br />
Then I have a <code>updateSelectedPatient</code> slot in MainWindow</p>
<pre><code>void MainWindow::updateSelectedPatient(TreeViewNode* node) {
    selected-&gt;setSelectedPatient(node-&gt;getNode()-&gt;getPatient());
}
</code></pre>
<p dir="auto">and the corresponding connect statement in the MainWindow constructor</p>
<pre><code>connect(treeView, &amp;TreeViewNode::clicked, this, &amp;MainWindow::updateSelectedPatient);
</code></pre>
<p dir="auto">When I execute I see the nodes that have been added to the scene in the view but when I click on them nothing happens (I know through debugging and also because the setSelectedPatient() method updates a widget that shows the patient information of the currently selected patient); it seems like mousePressEvent() is not being called at all.. any help would be really appreciated, thank you for your time!</p>
]]></description><link>https://forum.qt.io/topic/143078/qwidget-mousepressevent-not-called</link><generator>RSS for Node</generator><lastBuildDate>Mon, 08 Jun 2026 11:52:34 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/143078.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 18 Feb 2023 20:03:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QWidget::mousePressEvent() not called on Mon, 20 Feb 2023 20:24:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a> Adding this</p>
<pre><code>QRectF TreeViewNode::boundingRect() const {
    return QRectF(-25, -25, 50, 50);
}
</code></pre>
<p dir="auto">fixed it instantly.. such a silly mistake, it's almost as if I started learning about Qt last week.. (it's true).<br />
What can I say, thank you so much to everyone for your time and your suggestions, I'll make sure to remove m_scene and use scene() as well and keep learning more in detail about the classes I'm using. Thank you!</p>
]]></description><link>https://forum.qt.io/post/748515</link><guid isPermaLink="true">https://forum.qt.io/post/748515</guid><dc:creator><![CDATA[nance97]]></dc:creator><pubDate>Mon, 20 Feb 2023 20:24:08 GMT</pubDate></item><item><title><![CDATA[Reply to QWidget::mousePressEvent() not called on Mon, 20 Feb 2023 18:52:52 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>Pl45m4</bdi></a> said in <a href="/post/748475">QWidget::mousePressEvent() not called</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nance97">@<bdi>nance97</bdi></a> said in <a href="/post/748429">QWidget::mousePressEvent() not called</a>:</p>
<blockquote>
<p dir="auto">QRectF TreeViewNode::boundingRect() const {<br />
return QRectF();<br />
}</p>
</blockquote>
</blockquote>
<blockquote>
<p dir="auto">This looks suspicious. Verify that your position and your <code>boundingRect</code> are where you expect them to be.</p>
</blockquote>
<p dir="auto">I would call it more than suspicious.</p>
<p dir="auto"><a href="https://doc.qt.io/qt-6/qgraphicsitem.html#boundingRect" target="_blank" rel="noopener noreferrer nofollow ugc">QGraphicsItem::boundingRect()</a></p>
<blockquote>
<p dir="auto"><em>This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be restricted to inside an item's bounding rect. QGraphicsView uses this to determine whether the item requires redrawing.</em></p>
</blockquote>
<p dir="auto">boundingRect() is also used for the default<br />
<a href="https://doc.qt.io/qt-6/qgraphicsitem.html#shape" target="_blank" rel="noopener noreferrer nofollow ugc">QGraphicsItem::shape()</a></p>
<blockquote>
<p dir="auto"><em>The shape is used for many things, including collision detection, hit tests, and for the QGraphicsScene::items() functions.<br />
The default implementation calls boundingRect() to return a simple rectangular shape, but subclasses can reimplement this function to return a more accurate shape for non-rectangular items. For example, a round item may choose to return an elliptic shape for better collision detection.</em></p>
</blockquote>
]]></description><link>https://forum.qt.io/post/748493</link><guid isPermaLink="true">https://forum.qt.io/post/748493</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Mon, 20 Feb 2023 18:52:52 GMT</pubDate></item><item><title><![CDATA[Reply to QWidget::mousePressEvent() not called on Mon, 20 Feb 2023 17:05:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nance97">@<bdi>nance97</bdi></a> said in <a href="/post/748429">QWidget::mousePressEvent() not called</a>:</p>
<blockquote>
<p dir="auto">QRectF TreeViewNode::boundingRect() const {<br />
return QRectF();<br />
}</p>
<p dir="auto">void TreeViewNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {<br />
Q_UNUSED(option)<br />
Q_UNUSED(widget)<br />
painter-&gt;drawEllipse(-25, -25, 50, 50);<br />
painter-&gt;drawText(QRectF(-25, -25, 50, 50), Qt::AlignCenter, QString::fromStdString(m_node-&gt;getPatient()-&gt;get_Name()));<br />
}</p>
</blockquote>
<p dir="auto">This looks suspicious. Verify that your position and your <code>boundingRect</code> are where you expect them to be.<br />
You could start by painting the <code>boundingRect</code> and compare it with your drawn item. If you paint outside the item's boundings, you wont receive events, when clicking inside this area.</p>
<p dir="auto">There is a lot stuff going on, that is not needed or could/should be done "cleaner".<br />
For example, a <code>QGraphicsItem</code> / <code>-Object</code> doesn't need to hold the scene as member, since you can access it any time with <code>scene()</code><br />
(<a href="https://doc.qt.io/qt-6/qgraphicsitem.html#scene" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-6/qgraphicsitem.html#scene</a>)</p>
]]></description><link>https://forum.qt.io/post/748475</link><guid isPermaLink="true">https://forum.qt.io/post/748475</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Mon, 20 Feb 2023 17:05:04 GMT</pubDate></item><item><title><![CDATA[Reply to QWidget::mousePressEvent() not called on Mon, 20 Feb 2023 13:48:32 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nance97">@<bdi>nance97</bdi></a><br />
That is good, it was not so in your earlier code.</p>
<p dir="auto">At this point you might start a brand new standalone <em>minimum compilable example</em> to verify it works there?  Then build up towards your code if so.</p>
]]></description><link>https://forum.qt.io/post/748459</link><guid isPermaLink="true">https://forum.qt.io/post/748459</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 20 Feb 2023 13:48:32 GMT</pubDate></item><item><title><![CDATA[Reply to QWidget::mousePressEvent() not called on Mon, 20 Feb 2023 10:11:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> it is, like so</p>
<pre><code>protected:
void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
</code></pre>
]]></description><link>https://forum.qt.io/post/748439</link><guid isPermaLink="true">https://forum.qt.io/post/748439</guid><dc:creator><![CDATA[nance97]]></dc:creator><pubDate>Mon, 20 Feb 2023 10:11:01 GMT</pubDate></item><item><title><![CDATA[Reply to QWidget::mousePressEvent() not called on Mon, 20 Feb 2023 09:49:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nance97">@<bdi>nance97</bdi></a><br />
<em>Always</em> start by marking an overridden method as <code>override</code> in the header file, in case it does not exist or you have the signature wrong....</p>
]]></description><link>https://forum.qt.io/post/748433</link><guid isPermaLink="true">https://forum.qt.io/post/748433</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 20 Feb 2023 09:49:40 GMT</pubDate></item><item><title><![CDATA[Reply to QWidget::mousePressEvent() not called on Mon, 20 Feb 2023 09:34:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>Pl45m4</bdi></a> said in <a href="/post/748368">QWidget::mousePressEvent() not called</a>:</p>
<blockquote>
<p dir="auto">Maybe you can post the whole class</p>
</blockquote>
<p dir="auto">I'll do that since it's not too long and I really have no idea what is going on, thank you both for taking a look in the first place! Not shown is the mousePressEvent method declared protected in the header file and implemented as shown above, am I doing something blatantly wrong?</p>
<pre><code>//TreeViewNode.h
class TreeViewNode : public QGraphicsObject {
    Q_OBJECT

private:
    node* m_node;
    Family_tree* m_family;
    QGraphicsScene* m_scene;
    static std::set&lt;node*&gt; addedNodes;

public:
    TreeViewNode(node* node, Family_tree* family, QGraphicsScene* scene);
    TreeViewNode* getTreeViewNode(node* n);
    node* getNode() const;
    static void clearAddedNodes();
    void updateNode(Patient&amp; patient);
    QRectF boundingRect() const override;
    void drawBranches(QPainter* painter);
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
};

////TreeViewNode.cpp
std::set&lt;node*&gt; TreeViewNode::addedNodes;

TreeViewNode::TreeViewNode(node* node, Family_tree* family, QGraphicsScene* scene) : m_node(node), m_family(family), m_scene(scene) {
    setFlag(ItemIsFocusable);
    m_scene-&gt;addItem(this);
    addedNodes.insert(m_node);
    qDebug() &lt;&lt; "Scene item count: " &lt;&lt; m_scene-&gt;items().count();

    if (m_node-&gt;getFather()) {
        auto father = m_node-&gt;getFather();
        if (addedNodes.count(father) &gt; 0) {return;}
        TreeViewNode* fatherNode = new TreeViewNode(m_node-&gt;getFather(), family, m_scene);
        fatherNode-&gt;setPos(-50, -100);
    }

    if (m_node-&gt;getMother()) {
        auto mother = m_node-&gt;getMother();
        if (addedNodes.count(mother) &gt; 0) {return;}
        TreeViewNode* motherNode = new TreeViewNode(m_node-&gt;getMother(), family, m_scene);
        motherNode-&gt;setPos(50, -100);
    }

    if (m_node-&gt;getSpouse()) {
        auto spouse = m_node-&gt;getSpouse();
        if (addedNodes.count(spouse) &gt; 0) {return;}
        TreeViewNode* spouseNode = new TreeViewNode(m_node-&gt;getSpouse(), family, m_scene);
        spouseNode-&gt;setPos(100, 0);
    }

    int childCount = 0;
    for (auto child : m_node-&gt;getChildren()) {
        if (addedNodes.count(child) &gt; 0) {return;}
        TreeViewNode* childNode = new TreeViewNode(child, family, m_scene);
        childCount++;
        double xPos, yPos;
        xPos = pos().x() + 50 + 100 * (childCount - (m_node-&gt;getChildren().size() + 1) / 2.0);
        yPos = pos().y() + 100;
        childNode-&gt;setPos(xPos, yPos);
    }
}

TreeViewNode* TreeViewNode::getTreeViewNode(node* n) {
    for (auto item : m_scene-&gt;items()) {
        TreeViewNode* treeViewNode = dynamic_cast&lt;TreeViewNode*&gt;(item);
        if (treeViewNode &amp;&amp; treeViewNode-&gt;getNode() == n) {
            return treeViewNode;
        }
    }
    return nullptr;
}

node* TreeViewNode::getNode() const {
    return m_node;
}

void TreeViewNode::clearAddedNodes() {
    addedNodes.clear();
}

void TreeViewNode::updateNode(Patient&amp; patient) {
    m_node-&gt;setPatient(patient);
}

QRectF TreeViewNode::boundingRect() const {
    return QRectF();
}

void TreeViewNode::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
    Q_UNUSED(option)
    Q_UNUSED(widget)
    painter-&gt;drawEllipse(-25, -25, 50, 50);
    painter-&gt;drawText(QRectF(-25, -25, 50, 50), Qt::AlignCenter, QString::fromStdString(m_node-&gt;getPatient()-&gt;get_Name()));
}
</code></pre>
]]></description><link>https://forum.qt.io/post/748429</link><guid isPermaLink="true">https://forum.qt.io/post/748429</guid><dc:creator><![CDATA[nance97]]></dc:creator><pubDate>Mon, 20 Feb 2023 09:34:54 GMT</pubDate></item><item><title><![CDATA[Reply to QWidget::mousePressEvent() not called on Mon, 20 Feb 2023 07:52:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pl45m4">@<bdi>Pl45m4</bdi></a> said in <a href="/post/748368">QWidget::mousePressEvent() not called</a>:</p>
<blockquote>
<p dir="auto">Maybe you can post the whole class,</p>
</blockquote>
<p dir="auto">Or even better, a minimal example that demonstrates the problem.</p>
]]></description><link>https://forum.qt.io/post/748403</link><guid isPermaLink="true">https://forum.qt.io/post/748403</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Mon, 20 Feb 2023 07:52:31 GMT</pubDate></item><item><title><![CDATA[Reply to QWidget::mousePressEvent() not called on Sun, 19 Feb 2023 23:56:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nance97">@<bdi>nance97</bdi></a> said in <a href="/post/748367">QWidget::mousePressEvent() not called</a>:</p>
<blockquote>
<p dir="auto">I might have to consider a different solution</p>
</blockquote>
<p dir="auto">You dont have to, since it's the right way. Both ways (<code>QGraphicsObject</code> subclass or custom class derived from <code>QGraphicsItem</code> and <code>QObject</code>, which actually is the same as <code>QGraphicsObject</code>) should work.</p>
<p dir="auto">Maybe you can post the whole class, since there must be something going on, so that you dont receive click events<br />
Eventually a <code>boundingRect</code> issue, as mentioned by <a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a> or something else...</p>
]]></description><link>https://forum.qt.io/post/748368</link><guid isPermaLink="true">https://forum.qt.io/post/748368</guid><dc:creator><![CDATA[Pl45m4]]></dc:creator><pubDate>Sun, 19 Feb 2023 23:56:27 GMT</pubDate></item><item><title><![CDATA[Reply to QWidget::mousePressEvent() not called on Sun, 19 Feb 2023 23:21:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a> thank you, I ended up deriving TreeViewNode from QGraphicsObject but its overriden version of mousePressEvent still doesn't get triggered when clicking the nodes in the view; I might have to consider a different solution</p>
]]></description><link>https://forum.qt.io/post/748367</link><guid isPermaLink="true">https://forum.qt.io/post/748367</guid><dc:creator><![CDATA[nance97]]></dc:creator><pubDate>Sun, 19 Feb 2023 23:21:13 GMT</pubDate></item><item><title><![CDATA[Reply to QWidget::mousePressEvent() not called on Sun, 19 Feb 2023 02:22:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nance97">@<bdi>nance97</bdi></a> said in <a href="/post/748288">QWidget::mousePressEvent() not called</a>:</p>
<blockquote>
<p dir="auto">I'm working on a TreeViewNode class derived from QObject and QGraphicsItem,</p>
</blockquote>
<p dir="auto">Another option is to inherit from <a href="https://doc.qt.io/qt-6/qgraphicsobject.html" target="_blank" rel="noopener noreferrer nofollow ugc">QGraphicsObject</a>.</p>
<blockquote>
<p dir="auto">I made sure to include the <code>setFlag(QGraphicsItem::ItemIsSelectable);</code> method in the class constructor and I overloaded mousePressEvent like so:</p>
<pre><code>void TreeViewNode::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    if (event-&gt;button() == Qt::LeftButton) {
        qDebug() &lt;&lt; "Mouse pressed on node: " &lt;&lt; m_node-&gt;getPatient()-&gt;get_Name().c_str();
</code></pre>
</blockquote>
<p dir="auto">Making the item selectable isn't necessary to receive mouse events. In the following example, the left half of the item receives mouse presses because that falls within the area reported by <a href="https://doc.qt.io/qt-6/qgraphicsitem.html#shape" target="_blank" rel="noopener noreferrer nofollow ugc">QGraphicsItem::shape()</a>. The right half of the text is displayed, but does not respond to clicks.</p>
<pre><code>class Item : public QGraphicsTextItem {
    Q_OBJECT
protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) {
        qDebug() &lt;&lt; "mousePressEvent" &lt;&lt; event-&gt;pos();
    }
public:
    Item(QGraphicsItem *parent = nullptr): QGraphicsTextItem(parent) {
        setPlainText("A|B");
        setFlag(QGraphicsItem::GraphicsItemFlag::ItemIsSelectable, false);
    }
    QPainterPath shape() const {
        QPainterPath path;
        QRectF rect = boundingRect();
        rect.setWidth(rect.width() / 2);
        path.addRect(rect);
        return path;
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene scene;
    QGraphicsView view(&amp;scene);
    Item item;
    scene.addItem(&amp;item);
    view.show();
    return a.exec();
}
</code></pre>
]]></description><link>https://forum.qt.io/post/748301</link><guid isPermaLink="true">https://forum.qt.io/post/748301</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Sun, 19 Feb 2023 02:22:09 GMT</pubDate></item></channel></rss>