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&)'"

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.0k Views
  • 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.
  • Lucas_1603L Lucas_1603

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

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on 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
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on 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

      Lucas_1603L 1 Reply Last reply
      3
      • JKSHJ JKSH

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

        Lucas_1603L Offline
        Lucas_1603L Offline
        Lucas_1603
        wrote on 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?

        JKSHJ 1 Reply Last reply
        0
        • IntruderExcluderI Offline
          IntruderExcluderI Offline
          IntruderExcluder
          wrote on 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.

          Lucas_1603L 1 Reply Last reply
          0
          • IntruderExcluderI IntruderExcluder

            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.

            Lucas_1603L Offline
            Lucas_1603L Offline
            Lucas_1603
            wrote on 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
            • IntruderExcluderI Offline
              IntruderExcluderI Offline
              IntruderExcluder
              wrote on 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
              • Lucas_1603L Lucas_1603

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

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on 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

                Lucas_1603L 1 Reply Last reply
                0
                • JKSHJ JKSH

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

                  Lucas_1603L Offline
                  Lucas_1603L Offline
                  Lucas_1603
                  wrote on 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.

                  JKSHJ 1 Reply Last reply
                  0
                  • Lucas_1603L Lucas_1603

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

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on 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

                    Lucas_1603L 1 Reply Last reply
                    2
                    • JKSHJ JKSH

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

                      Lucas_1603L Offline
                      Lucas_1603L Offline
                      Lucas_1603
                      wrote on last edited by
                      #26

                      @jksh Thank you so much
                      All you guys helped me a lot and I really appreciate it.
                      I'll spend more time to learn more about model-view framework to see how really they work.

                      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