CopperSpice API  1.7.2
Creating Custom Types

When creating user interfaces, particularly those with specialized controls and features, developers sometimes need to create new data types that can be used alongside or in place of the existing set of value types. Standard types such as QSize, QColor and QString can all be stored in QVariant objects, used as the types of properties, and emitted in signal-slot communication.

This document will take a custom type and describe how to integrate it into the CopperSpice object model so it can be stored the same way as standard types. Then we will show how to register the custom type so it can be used in signals and slots connections.

Creating a Custom Type

When designing a new class or custom type it must meet the following three requirements imposed by QVariant.

  • public default constructor
  • public copy constructor
  • public destructor

This is an example of a new class named Message which was added in your application or library. The class also provides a constructor for normal use and two public member functions that are used to obtain the private data.

class Message {
Message(const Message &other);
Message(const QString &body, const QStringList &headers);
QString body() const;
QStringList headers() const;
QString m_body;
QStringList m_headers;

Declaring the Type

In order to use the Message class with the property system or in a QVariant, it must be added as a MetaType. This will provide the ability to query the QVariant system for the name, which would be "Message".

To add this class to the QVariant system invoke the CS_DECLARE_METATYPE() macro in the header file where it is defined, after the class declaration.