Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. error: "'QObject::QObject(const QObject&)' is private" and "use of deleted function 'QObject::QObject(const QObject&)'"
Forum Updated to NodeBB v4.3 + New Features

error: "'QObject::QObject(const QObject&)' is private" and "use of deleted function 'QObject::QObject(const QObject&)'"

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
26 Posts 7 Posters 9.1k 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.
  • L Lucas_1603
    16 Sept 2019, 10:27

    @jonb
    After added those setter functions and now my .cpp became like:

    #include <song.h>
    Song::Song(QObject *parent) : QObject (parent)
    {
        this->setTitle("Hello world");
        this->setSinger("Dac Luc");
        this->setSource("lll");
        this->setAlbumArt("qrc:/Image/Hongkong1.png");
    }
    Song::Song(const QString &title, const QString &singer, const QString &source,  const QString &albumArt)
    {
        m_title = title;
        m_singer = singer;
        m_source = source;
        m_albumArt = albumArt;
    }
    
    Q_INVOKABLE QString Song::title() const
    {
        return m_title;
    }
    
    QString Song::singer() const
    {
        return m_singer;
    }
    
    QString Song::source() const
    {
        return m_source;
    }
    
    QString Song::album_art() const
    {
        return m_albumArt;
    }
    

    But the errors are still the same

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 16 Sept 2019, 10:30 last edited by
    #6

    @lucas_1603 Where exactly do you get the error?
    The problem: QObject instances are NOT copyable.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    L 1 Reply Last reply 16 Sept 2019, 10:44
    1
    • J jsulm
      16 Sept 2019, 10:30

      @lucas_1603 Where exactly do you get the error?
      The problem: QObject instances are NOT copyable.

      L Offline
      L Offline
      Lucas_1603
      wrote on 16 Sept 2019, 10:44 last edited by Lucas_1603
      #7

      @jsulm I think I'm getting trouble with Song class but actually I'm not sure about that because my project has other classes too. But I just see the errors in Song class now

      0_1568630374154_5f945232-7a62-4a16-8251-6caa978974c4-image.png

      J J 2 Replies Last reply 16 Sept 2019, 11:05
      0
      • L Lucas_1603
        16 Sept 2019, 10:44

        @jsulm I think I'm getting trouble with Song class but actually I'm not sure about that because my project has other classes too. But I just see the errors in Song class now

        0_1568630374154_5f945232-7a62-4a16-8251-6caa978974c4-image.png

        J Online
        J Online
        JonB
        wrote on 16 Sept 2019, 11:05 last edited by JonB
        #8

        @lucas_1603
        I'm not an expert C++-er, but:

        • Did you re-run qmake after adding the Q_OBJECT macro to the file?

        • Song::Song(const QString &title, const QString &singer, const QString &source, const QString &albumArt)
          Does this constructor not need to invoke : QObject (parent) or your : Song(parent)? (I'm not sure about this one, but I'm thinking you need something like this?)

        L 1 Reply Last reply 16 Sept 2019, 12:01
        2
        • J JonB
          16 Sept 2019, 11:05

          @lucas_1603
          I'm not an expert C++-er, but:

          • Did you re-run qmake after adding the Q_OBJECT macro to the file?

          • Song::Song(const QString &title, const QString &singer, const QString &source, const QString &albumArt)
            Does this constructor not need to invoke : QObject (parent) or your : Song(parent)? (I'm not sure about this one, but I'm thinking you need something like this?)

          L Offline
          L Offline
          Lucas_1603
          wrote on 16 Sept 2019, 12:01 last edited by
          #9

          @jonb Thanks a lot, I'll try to find it out

          1 Reply Last reply
          0
          • L Lucas_1603
            16 Sept 2019, 10:44

            @jsulm I think I'm getting trouble with Song class but actually I'm not sure about that because my project has other classes too. But I just see the errors in Song class now

            0_1568630374154_5f945232-7a62-4a16-8251-6caa978974c4-image.png

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 16 Sept 2019, 12:01 last edited by
            #10

            @lucas_1603 Your Song class is QObject based and thus not copyable. You're apparently trying to make a copy of it.
            Double click at the first error line and see what is there.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            L 1 Reply Last reply 16 Sept 2019, 16:21
            2
            • J jsulm
              16 Sept 2019, 12:01

              @lucas_1603 Your Song class is QObject based and thus not copyable. You're apparently trying to make a copy of it.
              Double click at the first error line and see what is there.

              L Offline
              L Offline
              Lucas_1603
              wrote on 16 Sept 2019, 16:21 last edited by
              #11

              @jsulm I don't know what you mean by said that I'm trying to make a copy of it. I actually didn't assign any ones to others!

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 16 Sept 2019, 17:18 last edited by
                #12

                Hi,

                Are you maybe using the copy constructor somewhere ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                L 1 Reply Last reply 16 Sept 2019, 18:15
                0
                • S SGaist
                  16 Sept 2019, 17:18

                  Hi,

                  Are you maybe using the copy constructor somewhere ?

                  L Offline
                  L Offline
                  Lucas_1603
                  wrote on 16 Sept 2019, 18:15 last edited by Lucas_1603
                  #13

                  @sgaist Nope, I don't use it in my project and I'm quite sure about that ^^.
                  I really want to know what does the error use of deleted function 'Song::Song(const Song&)' mean.

                  J 1 Reply Last reply 16 Sept 2019, 23:44
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 16 Sept 2019, 19:32 last edited by
                    #14

                    It means that somewhere in your code you are using the copy constructor which is disabled since QObject objects cannot be copied.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • L Lucas_1603
                      16 Sept 2019, 18:15

                      @sgaist Nope, I don't use it in my project and I'm quite sure about that ^^.
                      I really want to know what does the error use of deleted function 'Song::Song(const Song&)' mean.

                      J Offline
                      J Offline
                      JKSH
                      Moderators
                      wrote on 16 Sept 2019, 23:44 last edited by JKSH
                      #15

                      @lucas_1603 said in error: "'QObject::QObject(const QObject&)' is private" and "use of deleted function 'QObject::QObject(const QObject&)'":

                      Nope, I don't use it in my project and I'm quite sure about that ^^.

                      The copy constructor is invoked when you try to assign one Song object to another, for example:

                      Song s1("Happy Birthday to You", "Me", "/path/to/song", "/path/to/art");
                      
                      Song s2(s1);  // copy-constructor invoked here
                      Song s3 = s1; // operator=() and copy-constructor invoked here
                      

                      I really want to know what does the error use of deleted function 'Song::Song(const Song&)' mean.

                      It means your code contains something like s2 or s3 above.

                      • Song::Song(const Song&) is the copy-constructor
                      • A "deleted function" is a function which the creator has removed/deleted from the class -- QObjects are not allowed to have copy-constructors. See https://thispointer.com/c11-c14-delete-keyword-and-deleted-functions-with-use-cases-examples/ for more info

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      L 1 Reply Last reply 17 Sept 2019, 10:57
                      4
                      • J JKSH
                        16 Sept 2019, 23:44

                        @lucas_1603 said in error: "'QObject::QObject(const QObject&)' is private" and "use of deleted function 'QObject::QObject(const QObject&)'":

                        Nope, I don't use it in my project and I'm quite sure about that ^^.

                        The copy constructor is invoked when you try to assign one Song object to another, for example:

                        Song s1("Happy Birthday to You", "Me", "/path/to/song", "/path/to/art");
                        
                        Song s2(s1);  // copy-constructor invoked here
                        Song s3 = s1; // operator=() and copy-constructor invoked here
                        

                        I really want to know what does the error use of deleted function 'Song::Song(const Song&)' mean.

                        It means your code contains something like s2 or s3 above.

                        • Song::Song(const Song&) is the copy-constructor
                        • A "deleted function" is a function which the creator has removed/deleted from the class -- QObjects are not allowed to have copy-constructors. See https://thispointer.com/c11-c14-delete-keyword-and-deleted-functions-with-use-cases-examples/ for more info
                        L Offline
                        L Offline
                        Lucas_1603
                        wrote on 17 Sept 2019, 10:57 last edited by
                        #16

                        @jksh Yep, I got it. I realized what all you guys said.

                        So I change the approach to my project now, assume that I have a PlaylistModel class:

                        #include <QAbstractListModel>
                        class PlaylistModel : public QAbstractListModel
                        {
                            Q_OBJECT
                        
                        public:
                            enum Roles {
                                TitleRole = Qt::UserRole + 1,
                                SingerRole,
                                SourceRole,
                                AlbumArtRole
                            };
                        
                            explicit PlaylistModel(QObject *parent = nullptr);
                        
                            int rowCount(const QModelIndex &parent = QModelIndex()) const override;
                        
                            QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
                            void addSong(Song &song);
                        
                        protected:
                            QHash<int, QByteArray> roleNames() const override;
                        
                        private:
                            QList<Song> m_data;
                        };
                        

                        Now I want to access m_data (is the private attribute) from QML file, how can I do that?

                        Thanks a lot for your help.

                        J 1 Reply Last reply 17 Sept 2019, 12:10
                        0
                        • L Lucas_1603
                          17 Sept 2019, 10:57

                          @jksh Yep, I got it. I realized what all you guys said.

                          So I change the approach to my project now, assume that I have a PlaylistModel class:

                          #include <QAbstractListModel>
                          class PlaylistModel : public QAbstractListModel
                          {
                              Q_OBJECT
                          
                          public:
                              enum Roles {
                                  TitleRole = Qt::UserRole + 1,
                                  SingerRole,
                                  SourceRole,
                                  AlbumArtRole
                              };
                          
                              explicit PlaylistModel(QObject *parent = nullptr);
                          
                              int rowCount(const QModelIndex &parent = QModelIndex()) const override;
                          
                              QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
                              void addSong(Song &song);
                          
                          protected:
                              QHash<int, QByteArray> roleNames() const override;
                          
                          private:
                              QList<Song> m_data;
                          };
                          

                          Now I want to access m_data (is the private attribute) from QML file, how can I do that?

                          Thanks a lot for your help.

                          J Online
                          J Online
                          JonB
                          wrote on 17 Sept 2019, 12:10 last edited by JonB
                          #17

                          @lucas_1603
                          You can only access private variables in the class which defines them. So expose a getter/setter if you really need to. But since QAbstractListModel already has data() & setData() methods you would normally go via those.

                          1 Reply Last reply
                          2
                          • J Offline
                            J Offline
                            JKSH
                            Moderators
                            wrote on 17 Sept 2019, 12:12 last edited by
                            #18

                            @lucas_1603 said in error: "'QObject::QObject(const QObject&)' is private" and "use of deleted function 'QObject::QObject(const QObject&)'":

                            QList<Song> m_data

                            Does that compile?

                            Store pointers-to-QObjects inside containers, rather than QObjects themselves.

                            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                            L 1 Reply Last reply 17 Sept 2019, 13:44
                            3
                            • J JKSH
                              17 Sept 2019, 12:12

                              @lucas_1603 said in error: "'QObject::QObject(const QObject&)' is private" and "use of deleted function 'QObject::QObject(const QObject&)'":

                              QList<Song> m_data

                              Does that compile?

                              Store pointers-to-QObjects inside containers, rather than QObjects themselves.

                              L Offline
                              L Offline
                              Lucas_1603
                              wrote on 17 Sept 2019, 13:44 last edited by
                              #19

                              @jksh Yes, it compiled.
                              @JonB As you said, I can access m_data via data() and setData() method. In data(), I have to assign QModelIndex &index and int role to it .
                              How I can use data() method with enum TitleRole?

                              J 1 Reply Last reply 17 Sept 2019, 21:56
                              0
                              • I Offline
                                I Offline
                                IntruderExcluder
                                wrote on 17 Sept 2019, 13:59 last edited by
                                #20

                                You should override roleNames method in your model:

                                QHash<int, QByteArray> PlaylistModel::roleNames() const
                                {
                                    static QHash<int, QByteArray>* ret = nullptr;
                                    if (ret)
                                        return *ret;
                                
                                    ret = new QHash<int, QByteArray>();
                                    (*ret)[TitleRole] = "title";
                                    ...
                                
                                    return *ret;
                                }
                                

                                Then in QML you can use them as model property: model.title.

                                L 1 Reply Last reply 17 Sept 2019, 14:31
                                0
                                • I IntruderExcluder
                                  17 Sept 2019, 13:59

                                  You should override roleNames method in your model:

                                  QHash<int, QByteArray> PlaylistModel::roleNames() const
                                  {
                                      static QHash<int, QByteArray>* ret = nullptr;
                                      if (ret)
                                          return *ret;
                                  
                                      ret = new QHash<int, QByteArray>();
                                      (*ret)[TitleRole] = "title";
                                      ...
                                  
                                      return *ret;
                                  }
                                  

                                  Then in QML you can use them as model property: model.title.

                                  L Offline
                                  L Offline
                                  Lucas_1603
                                  wrote on 17 Sept 2019, 14:31 last edited by Lucas_1603
                                  #21

                                  @intruderexcluder yes, I overrided it. When I use PlaylistModel as a model in QML, I actually just want to access m_data variable, not all of PlaylistModel attributes.
                                  Any suggestions to do that? Thanks a lot

                                  1 Reply Last reply
                                  0
                                  • I Offline
                                    I Offline
                                    IntruderExcluder
                                    wrote on 17 Sept 2019, 14:36 last edited by
                                    #22

                                    You are probably doing something wrong, but you can at any time simply add slot or invokable method to access any data.

                                    1 Reply Last reply
                                    0
                                    • L Lucas_1603
                                      17 Sept 2019, 13:44

                                      @jksh Yes, it compiled.
                                      @JonB As you said, I can access m_data via data() and setData() method. In data(), I have to assign QModelIndex &index and int role to it .
                                      How I can use data() method with enum TitleRole?

                                      J Offline
                                      J Offline
                                      JKSH
                                      Moderators
                                      wrote on 17 Sept 2019, 21:56 last edited by
                                      #23

                                      @lucas_1603 said in error: "'QObject::QObject(const QObject&)' is private" and "use of deleted function 'QObject::QObject(const QObject&)'":

                                      Yes, it compiled
                                      ...
                                      I actually just want to access m_data variable, not all of PlaylistModel attributes.

                                      Is Song still a subclass of QObject?

                                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                      L 1 Reply Last reply 18 Sept 2019, 01:51
                                      0
                                      • J JKSH
                                        17 Sept 2019, 21:56

                                        @lucas_1603 said in error: "'QObject::QObject(const QObject&)' is private" and "use of deleted function 'QObject::QObject(const QObject&)'":

                                        Yes, it compiled
                                        ...
                                        I actually just want to access m_data variable, not all of PlaylistModel attributes.

                                        Is Song still a subclass of QObject?

                                        L Offline
                                        L Offline
                                        Lucas_1603
                                        wrote on 18 Sept 2019, 01:51 last edited by
                                        #24

                                        @jksh No, it's not now. I think I'm doing right way in this time ^^
                                        The challenge now is accessing to m_data to get title, singer, source, album_art.

                                        J 1 Reply Last reply 18 Sept 2019, 03:23
                                        0
                                        • L Lucas_1603
                                          18 Sept 2019, 01:51

                                          @jksh No, it's not now. I think I'm doing right way in this time ^^
                                          The challenge now is accessing to m_data to get title, singer, source, album_art.

                                          J Offline
                                          J Offline
                                          JKSH
                                          Moderators
                                          wrote on 18 Sept 2019, 03:23 last edited by
                                          #25

                                          @lucas_1603 said in error: "'QObject::QObject(const QObject&)' is private" and "use of deleted function 'QObject::QObject(const QObject&)'":

                                          I think I'm doing right way in this time ^^

                                          There's often more than 1 way to achieve something; some are just easier or more efficient than others :)

                                          The challenge now is accessing to m_data to get title, singer, source, album_art.

                                          I see that you've implemented a Model. The idea behind a model is that you shouldn't need to access the internal data structure directly. Instead, you use the model's standard interface (rows, columns, and maybe roles) to access individual fields.

                                          In your example, you can treat each Row as an individual song. You can also treat each Column as a song field (e.g. Col 1 = Title, Col 2 = Singer, etc.). Older examples use Roles instead of Columns

                                          In QML, you normally don't call data() and setData() directly; you let the View and Delegate call them for you.

                                          Spend some time getting to know the model-view framework first:

                                          • https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html
                                          • https://doc.qt.io/qt-5/qtquick-models-objectlistmodel-example.html
                                            • https://code.qt.io/cgit/qt/qtdeclarative.git/tree/examples/quick/models/objectlistmodel?h=5.13

                                          This is quite a lengthy topic, so it's not something that we can teach you over a few forum posts. You need to spend time going through examples and modifying them to see how they behave. trying things out.

                                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                          L 1 Reply Last reply 18 Sept 2019, 03:44
                                          2

                                          15/26

                                          16 Sept 2019, 23:44

                                          • Login

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