<?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[Call a slot whenever a QObject is created]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I'm not so great at C++, so maybe I'm taking the wrong approach here, but I'm trying to make a garbage collector in QT. I've got most of it worked out, but I want to connect <em>all</em> instances of the onCreate() signal to a slot on said garbage collector, including those that haven't yet been made.</p>
<p dir="auto">Is this possible? Is there a better approach?</p>
<p dir="auto">Below is my header, for context. The idea is that it's a singleton which will be called whenever a page is pop()'d off my main StackView (at which point it will clear all items associated with said view).</p>
<pre><code>#ifndef GARBAGECOLLECTOR_H
#define GARBAGECOLLECTOR_H

#include &lt;QObject&gt;
#include &lt;QList&gt;

class garbageCollector : public QObject
{
    Q_OBJECT
private:
    explicit garbageCollector(QObject *parent = nullptr);
    garbageCollector* instance;
    QList&lt;QObject*&gt;* objectList;

public:
    garbageCollector* getInstance();

signals:

public slots:
    void cleanup();
    void addItem();
};

#endif // GARBAGECOLLECTOR_H
</code></pre>
]]></description><link>https://forum.qt.io/topic/104896/call-a-slot-whenever-a-qobject-is-created</link><generator>RSS for Node</generator><lastBuildDate>Sun, 03 May 2026 11:56:46 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/104896.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 12 Jul 2019 08:15:17 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Call a slot whenever a QObject is created on Fri, 12 Jul 2019 09:37:58 GMT]]></title><description><![CDATA[<p dir="auto">I do indeed come from Java :p<br />
Or rather, python -&gt; java -&gt; now trying C++ and Rust.</p>
<p dir="auto">So just to clarify, I'm in a situation where I might have several long-lived objects created by views in my stackview (C++ objects, that is). Despite that, by popping a view off said stackview, all the objects created by that view will be cleaned up, even if they're C++ objects (still derived from QObject)?</p>
]]></description><link>https://forum.qt.io/post/540474</link><guid isPermaLink="true">https://forum.qt.io/post/540474</guid><dc:creator><![CDATA[TTIO]]></dc:creator><pubDate>Fri, 12 Jul 2019 09:37:58 GMT</pubDate></item><item><title><![CDATA[Reply to Call a slot whenever a QObject is created on Fri, 12 Jul 2019 08:57:40 GMT]]></title><description><![CDATA[<p dir="auto">Hi<br />
There is already a system in place. as  <a class="plugin-mentions-user plugin-mentions-a" href="/user/sierdzio">@<bdi>sierdzio</bdi></a> says<br />
<a href="https://doc.qt.io/qt-5/objecttrees.html" target="_blank" rel="noopener noreferrer nofollow ugc">https://doc.qt.io/qt-5/objecttrees.html</a></p>
<p dir="auto">So in most cases, ownership and clean up is handled by Qt.</p>
]]></description><link>https://forum.qt.io/post/540469</link><guid isPermaLink="true">https://forum.qt.io/post/540469</guid><dc:creator><![CDATA[mrjj]]></dc:creator><pubDate>Fri, 12 Jul 2019 08:57:40 GMT</pubDate></item><item><title><![CDATA[Reply to Call a slot whenever a QObject is created on Fri, 12 Jul 2019 08:48:46 GMT]]></title><description><![CDATA[<p dir="auto">Oh! Someone comes from Java development!</p>
]]></description><link>https://forum.qt.io/post/540465</link><guid isPermaLink="true">https://forum.qt.io/post/540465</guid><dc:creator><![CDATA[closx]]></dc:creator><pubDate>Fri, 12 Jul 2019 08:48:46 GMT</pubDate></item><item><title><![CDATA[Reply to Call a slot whenever a QObject is created on Fri, 12 Jul 2019 08:44:44 GMT]]></title><description><![CDATA[<p dir="auto">Why do you need it? QML already contains a garbage collector. And all Q_OBJECT instances are additionally managed by parent-child hierarchy.</p>
<p dir="auto">If you need some additional collecting, I'd rather recommend using QSharedPointer - it will automatically remove the object when needed, without any garbage collection class.</p>
]]></description><link>https://forum.qt.io/post/540462</link><guid isPermaLink="true">https://forum.qt.io/post/540462</guid><dc:creator><![CDATA[sierdzio]]></dc:creator><pubDate>Fri, 12 Jul 2019 08:44:44 GMT</pubDate></item></channel></rss>