[Qt-interest] QObject copy construction

Bachrach, Robert L rob.bachrach at tycoelectronics.com
Tue Feb 17 12:53:57 CET 2009


Andrea Franceschini wrote: 
> I know that QObjects cannot be copy constructed and I 
> understand the reason but my point is: how can I have my 
> class extend QObject and fit into a container such as QHash? 
> I mean, Whenever I try to browse the container, be it via 
> foreach or via iterators, the copy constructor always gets called.

According to the Qt documentation on Generic Containers:

The values stored in the various containers can be of any assignable data type. To qualify, a type must provide a default constructor, a copy constructor, and an assignment operator. This covers most data types you are likely to want to store in a container, including basic types such as int and double, pointer types, and Qt data types such as QString, QDate, and QTime, but it doesn't cover QObject or any QObject subclass (QWidget, QDialog, QTimer, etc.). If you attempt to instantiate a QList<QWidget>, the compiler will complain that QWidget's copy constructor and assignment operators are disabled. If you want to store these kinds of objects in a container, store them as pointers, for example as QList<QWidget *>.



More information about the Qt-interest-old mailing list