Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Subclassing QColor
Qt 6.11 is out! See what's new in the release blog

Subclassing QColor

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Anticross
      wrote on last edited by
      #2

      You can do multiple sub classing from QColor and QObject.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        [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.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved