Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Call a slot whenever a QObject is created
Forum Updated to NodeBB v4.3 + New Features

Call a slot whenever a QObject is created

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 607 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TTIO
    wrote on last edited by
    #1

    Hi,

    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 all instances of the onCreate() signal to a slot on said garbage collector, including those that haven't yet been made.

    Is this possible? Is there a better approach?

    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).

    #ifndef GARBAGECOLLECTOR_H
    #define GARBAGECOLLECTOR_H
    
    #include <QObject>
    #include <QList>
    
    class garbageCollector : public QObject
    {
        Q_OBJECT
    private:
        explicit garbageCollector(QObject *parent = nullptr);
        garbageCollector* instance;
        QList<QObject*>* objectList;
    
    public:
        garbageCollector* getInstance();
    
    signals:
    
    public slots:
        void cleanup();
        void addItem();
    };
    
    #endif // GARBAGECOLLECTOR_H
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Why do you need it? QML already contains a garbage collector. And all Q_OBJECT instances are additionally managed by parent-child hierarchy.

      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.

      (Z(:^

      1 Reply Last reply
      3
      • C Offline
        C Offline
        closx
        wrote on last edited by
        #3

        Oh! Someone comes from Java development!

        bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
        tag me (like @closx) if you are answering to me, so I can notice :D

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          There is already a system in place. as @sierdzio says
          https://doc.qt.io/qt-5/objecttrees.html

          So in most cases, ownership and clean up is handled by Qt.

          1 Reply Last reply
          4
          • T Offline
            T Offline
            TTIO
            wrote on last edited by
            #5

            I do indeed come from Java :p
            Or rather, python -> java -> now trying C++ and Rust.

            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)?

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved