Subclassing QColor



  • I would like to extend the QColor class by adding a few class member functions. Can I just subclass QColor? I read the Creating Custom Qt Types. But, that appears to be more for object composition. I want to inherit all the lovely functions already in QColor.

    Thanks,
    Ryan



  • You can do multiple sub classing from QColor and QObject.



  • [quote author="rheniser" date="1329264613"]I would like to extend the QColor class by adding a few class member functions. Can I just subclass QColor? I read the Creating Custom Qt Types. But, that appears to be more for object composition. I want to inherit all the lovely functions already in QColor.

    Thanks,
    Ryan[/quote]
    Yes, you can subclass QColor. However, be aware that this class was not designed to be subclassed, as it does not have a virtual destructor. If you start adding members, those might not be properly destructed if you use your subclass as a normal QColor.

    I don't understand the suggestion for multiple inheritance above. It does not seem to apply, and is generally a practice to avoid for anything but interfaces, IMHO.



  • Yes, the missing virtual destructor in the header is what prompted me to ask. Thanks for the insight. Out of curiosity, will my derived class work with Qt containers and QVariant::value() or the qvariant_cast() template function?



  • Qt containers will work as long as your class is copyable and default constructable. If you register your class, then QVariant, Q_PROPERTY and queued signals-slot connections will also work. See the [[doc:QMetaType]] documentation for details on that. If you also need streaming to work for those QVariants, don't forget to implement operator>>() and operator<<() for the type.



  • Alas, cspec is private and has no mutator function. I think an adapter class is the best way forward here.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.