1

I have a relatively simple class that contains a layout with a few widgets (labels, lineEdits, pushButtons) that gets displayed as a card. It all works fine until I try to add signals and slots. The header file, below, compiles fine with the sections commented-out as shown.

#include <QString>
#include <QObject>
#include <QWidget>

class KMLFile //: public QObject
{
    //Q_OBJECT
public:
    KMLFile();
    ~KMLFile();

    QString m_originalFilename;
    QString m_originalPath;
    QString m_proposedFilename;
    QString m_propsoedPath;
    QString m_coords;
    QWidget* trackWidget;

    void populate(QString originalFilename, QString originalPath, QString proposedFilename, QString coords);
    QString getCoords();
    int getLength();

//public slots:
    //void changeFilename();

};

When those bits of code are left in, I get the following error:

"C:...\qlist.h:425: error: C2280: 'KMLFile::KMLFile(const KMLFile &)': attempting to reference a deleted function

I am using Qt5.3 and Qt Creator. I have tried cleaning, running qmake and deleting the build folder to no avail. I'm stumped!

Grateful for any insight into how to fix this so that I can progress.

SeaMouse
  • 21
  • 1
  • 6
  • You can't use Qt's signals/slots mechanism without inheriting from QObject and without using the Q_OBJECT macro and running your code through the "moc" meta-object-compiler. – Jesper Juhl May 23 '16 at 17:27

2 Answers2

5

The copy constructor of QObject is private - or deleted.

Somewhere else in your code you use the copy constructor of KMLFile, which then calls the copy constructor of QObject.

You could reimplement the copy constructor of KMLFile, but the best solution is to not use it at all.

In your QList you should store pointers to the KMLFile objects instead of the objects themselves provided that they will live longer than the list itself. Or you can start using QSharedPointer.

coyotte508
  • 9,175
  • 6
  • 44
  • 63
  • Thanks very much, this gave me just enough to work out how to solve the problem. I changed the declaration of my QList to `QList tracks;` and instantiate objects of it with `KMLFile *track = new KMLFile;`, and it now compiles fine. Thank you very much! – SeaMouse May 23 '16 at 19:24
  • @SeaMouse be careful. You need to delete them at one point or another. Giving a parent to each `KMLFile` (presumably in the member function of the `QObject` inheritor which holds the list) takes care of most of the problem, but doing a `qDeleteAll()` on the list before it's destroyed can also solve it. – coyotte508 May 23 '16 at 19:37
  • thanks for the reminder - I had forgotten to do that! – SeaMouse May 23 '16 at 19:58
  • I wanted my object to have a signal, but it was in a QVector of another class. Once I made it a QVector of pointers, it started working. – CodeLurker Apr 21 '23 at 21:44
1

QObject has a deleted copy constructor. Your class is creating a compiler generated copy constructor. When you make QObject a base of your class the compiler generated copy constructor for KLMFile attempts to call the deleted copy constructor of the base class and that is when you get an error.

The copy constructor for KLMFile will be automatically generated if required (ie if you try copy an instance of the class) unless you explicitly specify a copy constructor of your own.

If the line referenced in the error is near a copy of the KLMFile class that would be a strong indicator that this is the case.

sji
  • 1,877
  • 1
  • 13
  • 28