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. Multi-level inheritance with QObjects
Forum Updated to NodeBB v4.3 + New Features

Multi-level inheritance with QObjects

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.9k Views 3 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.
  • M Offline
    M Offline
    michelson
    wrote on last edited by
    #1

    Hello,
    I have problem inheriting custom abstract class which inherits QObject. Long story short heres the code (simplified):

    class AbstractRejestration : public QObject
    {
        Q_OBJECT
    public:
        explicit AbstractRejestration(QByteArray raw_data = 0, QObject *parent = 0) 
           : QObject(parent) 
        { setData(raw_data); }
    
        virtual ~AbstractRejestration() { delete parameters; }
    
        virtual void setData(QByteArray raw_data);
    
        void setParameters(AbstractParameters *params);
    
    protected:
        virtual void parseData() = 0;
        AbstractParameters *parameters;
        QByteArray raw_data;
    };
    
    class OfflineRejestration : public AbstractRejestration
    {
    public:
        explicit OfflineRejestration(QByteArray raw_data = 0,
                                     QObject *parent = 0) : AbstractRejestration(raw_data, parent) {};
        virtual ~OfflineRejestration();
    
        void setData(QByteArray raw_data);
    
        void print();
        void clear();
    
    private:
        void parseData();
    };
    

    This is sufficent enough to generate such errors:

    use of deleted function 'OfflineRejestration::OfflineRejestration(const OfflineRejestration&)'
    use of deleted function 'AbstractRejestration::AbstractRejestration(const AbstractRejestration&)'
    qobject.h:461: error: 'QObject::QObject(const QObject&)' is private 'Q_DISABLE_COPY(QObject)'
    error: within this context 'class AbstractRejestration : public QObject'
    error: use of deleted function 'QObject::QObject(const QObject&)'
    

    I would be greatfull if someone explain me what is wrong here...

    kshegunovK 1 Reply Last reply
    0
    • Joel BodenmannJ Offline
      Joel BodenmannJ Offline
      Joel Bodenmann
      wrote on last edited by Joel Bodenmann
      #2

      From the looks of it you try to create a copy of a OfflineRejestration object somewhere in your code (by calling the copy constructor or the assignment operator). You cannot copy a QObject because the copy constructor and the assignment operators are declared as private. The documentation gives more information about why QObjects cannot be copied: https://doc.qt.io/qt-5/qobject.html#no-copy-constructor-or-assignment-operator

      It's recommended that you always use the Q_DISABLE_COPY() macro in your own classes. See the documentation linked above.

      If you really need a "copy" of a QObject based object the common way of doing this is by adding a clone() function to your class. Just make sure that you don't screw things up in there (eg. usually you don't want to assign/copy the parent attribute).

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      1 Reply Last reply
      2
      • M michelson

        Hello,
        I have problem inheriting custom abstract class which inherits QObject. Long story short heres the code (simplified):

        class AbstractRejestration : public QObject
        {
            Q_OBJECT
        public:
            explicit AbstractRejestration(QByteArray raw_data = 0, QObject *parent = 0) 
               : QObject(parent) 
            { setData(raw_data); }
        
            virtual ~AbstractRejestration() { delete parameters; }
        
            virtual void setData(QByteArray raw_data);
        
            void setParameters(AbstractParameters *params);
        
        protected:
            virtual void parseData() = 0;
            AbstractParameters *parameters;
            QByteArray raw_data;
        };
        
        class OfflineRejestration : public AbstractRejestration
        {
        public:
            explicit OfflineRejestration(QByteArray raw_data = 0,
                                         QObject *parent = 0) : AbstractRejestration(raw_data, parent) {};
            virtual ~OfflineRejestration();
        
            void setData(QByteArray raw_data);
        
            void print();
            void clear();
        
        private:
            void parseData();
        };
        

        This is sufficent enough to generate such errors:

        use of deleted function 'OfflineRejestration::OfflineRejestration(const OfflineRejestration&)'
        use of deleted function 'AbstractRejestration::AbstractRejestration(const AbstractRejestration&)'
        qobject.h:461: error: 'QObject::QObject(const QObject&)' is private 'Q_DISABLE_COPY(QObject)'
        error: within this context 'class AbstractRejestration : public QObject'
        error: use of deleted function 'QObject::QObject(const QObject&)'
        

        I would be greatfull if someone explain me what is wrong here...

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @michelson
        As an addition to Joel's good points, this:

        QByteArray raw_data = 0
        

        doesn't make much sense to me. What's the problem with giving an empty object here, like the following:

        explicit AbstractRejestration(QByteArray raw_data = QByteArray(), QObject * parent = 0)
               : QObject(parent) 
        {
            setData(raw_data);
        }
        

        You're missing the Q_OBJECT macro in your derived class. And finally inlining virtual methods is not possible, so I advise just putting the definitions in the cpp file.
        Meanwhile, what compiler are you using? I'd guess that the overload matching is somehow confused by your declarations, judging from the errors you get.

        PS.
        It's also a good idea to use override (or Q_DECL_OVERRIDE) to make sure you're indeed overriding a virtual method and nullptr instead of the terrible type-safety unaware 0.

        @Joel-Bodenmann

        If you really need a "copy" of a QObject based object the common way of doing this is by adding a clone() function to your class. Just make sure that you don't screw things up in there (eg. usually you don't want to assign/copy the parent attribute).

        Not that easy (that's why copying is removed in the first place) - you'd need to decide how and if, and should you duplicate signal-slot connections (beside the obvious problem with ownership). As a whole it's not a good idea to work around the disabled copy constructor in the first place.

        Kind regards.

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2

        • Login

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