error: "'QObject::QObject(const QObject&)' is private" and "use of deleted function 'QObject::QObject(const QObject&)'"
-
Hi all,
I have aSong
class like this:#include <QObject> class Song : public QObject { Q_OBJECT public: Song(const QString &title, const QString &singer, const QString &source, const QString &albumArt); explicit Song(QObject *parent = nullptr); Q_INVOKABLE QString title() const; QString singer() const; QString source() const; QString album_art() const; private: QString m_title; QString m_singer; QString m_source; QString m_albumArt; };
This returned some errors below:
'QObject::QObject(const QObject&)' is private within this context use of deleted function 'QObject::QObject(const QObject&)'
Why I got these errors and how can I deal with it? Thanks for your help
Regard
-
hi @lucas_1603,
can we see your .cpp also?
Have you added your definition of explicit Song(QObject *parent = nullptr);
definition in .cpp? -
@sharath
Yes, here it is:#include <song.h> Song::Song(QObject *parent) : QObject (parent) { this->title() = "Hello World"; this->singer() = "Dac Luc"; this->source() = "lll"; this->album_art() = "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; }
-
@lucas_1603 said in error: "'QObject::QObject(const QObject&)' is private" and "use of deleted function 'QObject::QObject(const QObject&)'":
this->title() = "Hello World"; this->singer() = "Dac Luc"; this->source() = "lll"; this->album_art() = "qrc:/Image/Hongkong1.png";
Nope! Those are getter functions, hence the error message! You need to write setters too, as Qt does for all properties like these, to be called like:
this->setTitle("Hello World");
-
@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
-
@lucas_1603 Where exactly do you get the error?
The problem: QObject instances are NOT copyable. -
@lucas_1603
I'm not an expert C++-er, but:-
Did you re-run
qmake
after adding theQ_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?)
-
-
@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. -
Hi,
Are you maybe using the copy constructor somewhere ?
-
It means that somewhere in your code you are using the copy constructor which is disabled since QObject objects cannot be copied.
-
@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
-
@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.
-
@lucas_1603
You can only accessprivate
variables in the class which defines them. So expose a getter/setter if you really need to. But sinceQAbstractListModel
already hasdata()
&setData()
methods you would normally go via those. -
@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.
-
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
.