2
votes

I have my custom class that delivered from QObject:

class Client : public QObject
{
    Q_OBJECT
    friend class Server;
public:
    Client(QTcpSocket *socket, QObject *parent = 0);
private:
    QTcpSocket *mSocket;
};

And try to add it in the my list (QList)

auto socket = mServer->nextPendingConnection();
Client client(socket);
mClients.append(client);

It outputs next:

/usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h:521: required from 'void QList::append(const T&) [with T = Client]'
...
/usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h:372: error: use of deleted function 'Client::Client(const Client&)' if (QTypeInfo::isLarge || QTypeInfo::isStatic) n->v = new T(t);

How I can add my object in the list correctly?

3
Is it all code of Client class? It is strange, since there is no deleted copy constructor and no members that have no copy constructor.ForEveR
Yes, it's all code at this moment. Of course, it has realization in .cpp file for prototypes.Шах
Can I not use copy constructor and just move object in list?Шах
QObject’s shouldn’t be copied, make it a list of smart pointers (shared_ptrs/QSharedPointers for example), instead.Frank Osterfeld

3 Answers

6
votes

QObject has private copy constructor and assignment operator. You should store list of smart pointers, instead of objects.

You should not define copy constructor for class, that is derived from QObject.

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.

No Copy Constructor or Assignment Operator

QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private.

1
votes

Do not use copying of QObject based classes. Use pointers:

QList<Client*> mClients;
Client* cln = new Client( socket );
mClients << cln;
....

Or you can use smart pointers

0
votes

I guess you should define the copy constructor for your Client class, it is deleted because of a valid reason and it looks like the QList wants it.