3

I'm getting the error:

C:\Qt\5.3\mingw482_32\include\QtCore\qobject.h:465: error : 'QObject::QObject(const QObject&)' is private
     Q_DISABLE_COPY(QObject)
                    ^
Qt\MyMediaLibraries\MyMediaLibraries\cpp.films\Movie.h:10: error : within this context
 class Movie: public QObject
   ^

I've read a lot about it but really I don't know what's wrong in my case. I know that QObject constructor is not copyable, but I'm not doing it here. Or actually I don't know I'm doing it ^^. Here is my code: Movie.h:

class Movie: public QObject
{

    Q_OBJECT

public:
    Movie();
    Movie(const int &movie_id, const QString &movie_title, const QString &movie_md5, const QDate &movie_releaseDate, const QString &movie_genre,
          const int &movie_note, const bool &movie_alreadySeen, const bool &movie_favourite, const bool &movie_toBeSeen,
          const QString &movie_synopsis, const int &movie_duration, const QString &movie_backdropPath, const QString &movie_path,
          const QString &movie_backdropMD5, const QString &movie_posterMD5, const QString &movie_posterPath, const QString &movie_collection);
    void getInfos();

    //TheMovieDB *tmdb;

    int id;
    QString title;
    QDate releaseDate;
    QString genre;
    int note;
    bool alreadySeen;
    bool favourite;
    bool toBeSeen;
    QString synopsis;
    int duration;
    QString path;
    QString md5;
    QString backdropPath;
    QString backdropMD5;
    QString posterPath;
    QString posterMD5;
    QString collection;

public slots:
    void updateDatas();
};

Movie.cpp:

Movie::Movie()
{
    title = "";
    md5 = "";
    id = 0;
    path = "";
    releaseDate = QDate();
    genre = "";
    note = 0;
    alreadySeen = false;
    favourite = false;
    toBeSeen = false;
    synopsis = "";
    duration = 0;
    backdropPath = "";
    backdropMD5 = "";
    posterPath = "";
    posterMD5 = "";
    collection = "";
}

Movie::Movie(const int &movie_id, const QString &movie_title, const QString &movie_md5, const QDate &movie_releaseDate,
             const QString &movie_genre, const int &movie_note, const bool &movie_alreadySeen, const bool &movie_favourite,
             const bool &movie_toBeSeen, const QString &movie_synopsis, const int &movie_duration,
             const QString &movie_backdropPath, const QString &movie_path, const QString &movie_backdropMD5,
             const QString &movie_posterMD5, const QString &movie_posterPath, const QString &movie_collection)
{
    id = movie_id;
    title = movie_title;
    md5 = movie_md5;
    path = movie_path;
    releaseDate = movie_releaseDate;
    genre = movie_genre;
    note = movie_note;
    alreadySeen = movie_alreadySeen;
    favourite = movie_favourite;
    toBeSeen = movie_toBeSeen;
    synopsis = movie_synopsis;
    duration = movie_duration;
    backdropPath = movie_backdropPath;
    backdropMD5 = movie_backdropMD5;
    posterPath = movie_posterPath;
    posterMD5 = movie_posterMD5;
    collection = movie_collection;
}

void Movie::getInfos()
{
    QObject::connect( tmdb , SIGNAL( dataRetrieved() ) , this , SLOT( updateDatas() ));
    tmdb->search(title);
}


void Movie::updateDatas()
{
    title = tmdb->t_infosList["title"].toString();
    md5 = "";
    id = tmdb->t_infosList["id"].toInt();
    path = "";
    releaseDate = tmdb->t_infosList["release_date"].toDate();
    note = tmdb->t_infosList["note"].toInt();
    synopsis = tmdb->t_infosList["overview"].toString();
    backdropPath = tmdb->t_infosList["backdrop"].toString();
    backdropMD5 = GeneralFunctions::hashMD5(tmdb->t_infosList["backdrop"].toString());
    posterPath = tmdb->t_infosList["poster"].toString();
    posterMD5 = GeneralFunctions::hashMD5(tmdb->t_infosList["poster"].toString());
    collection = tmdb->t_infosList["collection"].toString();

    QStringList genres = tmdb->t_infosList["genres"].toStringList();
    genre = genres[0];
    for(int i=0 ; i<genres.size() ; i++)
    {
        genre =genre + ", " + genres[i];
    }
}

Thanks for your help

Avatar36
  • 79
  • 2
  • 6
  • 2
    Your fundamental problem is that you're trying to copy a Movie object somewhere but cannot do so because of the base class. Disable your Movie's copy constructor also, and then the compiler will show you exactly where the attempted copy is (if it's not already doing that but not shown in your error message). – metal Mar 17 '15 at 19:37
  • On a side note, that second constructor of yours is just plain out horrendous. Just create a `MovieInfo` struct, have one inside the `Movie` class to make it cleaner and a constructor which accepts one. – dtech Mar 17 '15 at 20:33
  • ok I'll consider it. But actually having a struct containing all those variables will be horrible too. And it means I'll have to know what variables are in my class before using it. Using a struct means I don't need a class ^^ (only for functions). – Avatar36 Mar 18 '15 at 10:05

1 Answers1

3

You need to simply read the documentation for Q_DISABLE_COPY

Disables the use of copy constructors and assignment operators for the given Class.

Instances of subclasses of QObject should not be thought of as values that can be copied or assigned, but as unique identities. This means that when you create your own subclass of QObject (director or indirect), you should not give it a copy constructor or an assignment operator. However, it may not enough to simply omit them from your class, because, if you mistakenly write some code that requires a copy constructor or an assignment operator (it's easy to do), your compiler will thoughtfully create it for you. You must do more.

The curious user will have seen that the Qt classes derived from QObject typically include this macro in a private section:

class MyClass : public QObject
{

  private:
    Q_DISABLE_COPY(MyClass)
};
Dmitriy
  • 5,357
  • 8
  • 45
  • 57
  • I don't understand. This means I shouldn't use an overloaded constructor? I'm going to read a bit about Q_DISABLE_COPY cause I don't get it. – Avatar36 Mar 17 '15 at 22:20
  • `Q_DISABLE_COPY` declared in `QObject`. And you use `QObject` as a base class. You should not override copy constructor nor `operator=` – Dmitry Sazonov Mar 18 '15 at 09:11
  • So it's my constructor Movie() which is wrong? I just want to be able to catch signals with this class. I have no other use of the class QObject. – Avatar36 Mar 18 '15 at 09:52
  • You just need to add two lines of code in the bottom of your class definithion `private: Q_DISABLE_COPY(Movie)` – Dmitriy Mar 18 '15 at 10:23
  • But it's still not working :(. Now I have an error on this macro – Avatar36 Mar 18 '15 at 10:27
  • If you no longer have an error on this macro you need to accept the answer and then ask a new question with the other error you get – Dmitriy Mar 18 '15 at 10:38
  • I mean the problem isn't solve. I still have my main problem. Adding this macro add another error. – Avatar36 Mar 18 '15 at 10:59
  • I told you already what you should do to solve your problem: ask a new question with the other error you get – Dmitriy Mar 18 '15 at 11:06
  • Adding those lines doesn't solve my problem. I stil have the same error messages. – Avatar36 Mar 18 '15 at 12:29
  • @Avatar36 You cannot copy `QObject`s. End of story. If you wish to ask how you could change your design so that the problem is worked around, ask a separate question. There are ways of copying `QObject`-derived classes without copying the underlying `QObject`. – Kuba hasn't forgotten Monica Mar 18 '15 at 21:30
  • 4
    I've understood I can't copy. But I don't understand where I'm doing it in my code (I didn't even notice it). I've deleted my second constructor and the error is still here. – Avatar36 Mar 19 '15 at 07:26