0

I need to create an object that inherit from QTextEdit. The problem is that every time I try to inherit from it, I get the following error:

error C2248: 'QWidget::operator =' : cannot access private declared member from class 'QWidget'

I tried defining operator= and nothing works. Even if I also define a copy constructor. I always keep getting this error.

nobody
  • 19,814
  • 17
  • 56
  • 77
Shak
  • 166
  • 7

1 Answers1

4

QWidget class is a subclass of QObject which has copy and assignment operations disabled using the Q_DISABLE_COPY guard macro. When you inherit from QObject (or any derived class), the copy constructor or assignment operator you define tries to access base class's counter parts, but they are private in QObject and can not be accessed. This is by design.

As a side effect, object of classes that inherit from QObject can not be stored in STL or Qt containers (vector, list, etc). Only pointers or references can be stored. Because getting/setting object requires copying using the copy constructor, which is not possible.

If you need both to inherit from QWidget and overloading operator= and/or copy constructor, you can achieve by adding explicit methods for these tasks like isEqual or clone (though cloning QObject is not a good idea).

Community
  • 1
  • 1
Rakib
  • 7,435
  • 7
  • 29
  • 45
  • This answer could be improved by stating specifically what the (or a) fix is. Right now it's only explaining why the error is happening. – nobody May 08 '14 at 05:25
  • 1
    You state that *Each Qt class is a subclass of `QObject`*, which is not true. Not all Qt classes are a subclass of `QObject`. – thuga May 08 '14 at 05:37
  • @thuga, please read the first line of http://qt-project.org/doc/qt-5/QObject.html – Rakib May 08 '14 at 05:41
  • I think the answer is pretty clear, maybe it could be more explicit. i.e. don't implement assignment operator and copy constructor -> use a pointer. – Gerstmann May 08 '14 at 05:42
  • 1
    @RakibulHasan It doesn't say that every Qt class is a subclass of `QObject`. Are you insisting that for example `QPixmap` which is a Qt class, is a subclass of `QObject`? – thuga May 08 '14 at 05:47
  • @thuga, object comes from class, so "The QObject class is the base class of all Qt objects" implies "each class inherits QObject". I don't know about `QPixmap` class and you are right, it does not inherit `QObject`. Then the documentation of `QObject` is broken. – Rakib May 08 '14 at 05:57
  • The Qt documentation is slightly misleading. There are quite a few Qt classes that don't inherit from QObject - for example QByteArray http://qt-project.org/doc/qt-4.8/qbytearray.html – nobody May 08 '14 at 06:01
  • 1
    @RakibulHasan In that part of the documentation, `Qt object` does not mean the same as `object`, which is an instance of a class. The term they use is a little bit misleading though. – thuga May 08 '14 at 06:02
  • @thuga, thanks for correcting me. That line gave the impression of Java where everything inherits `Object`. – Rakib May 08 '14 at 06:03
  • @AndrewMedico there is no fix for this, it is by design. The question asker needs to have a very long rethink about the design and change it to conform to the `QObject` rules. – RobbieE May 08 '14 at 07:52
  • 1
    RakibulHasan, AndrewMedico: The Qt documentation is not broken, just not read properly. Qt Classes are divided into groups. Qt object classes and Qt data classes are the two largest groups. Qt object classes are all derived from QObject and employ the signal/slot mechnisms. Qt data classes are mostly just specialised data containers but almost all use the implicit sharing mechanism so you can pass by value without worrying too much about performance loss. – RobbieE May 08 '14 at 07:58