Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

QCoreVariant Class Reference

The QCoreVariant class acts like a union for the most common Qt data types. More...

#include <QCoreVariant>

Inherited by QVariant.

Public Types

Public Functions

Static Public Members


Detailed Description

The QCoreVariant class acts like a union for the most common Qt data types.

Because C++ forbids unions from including types that have non-default constructors or destructors, most interesting Qt classes cannot be used in unions. Without QCoreVariant, this would be a problem for QObject::property() and for database work, etc.

A QCoreVariant object holds a single value of a single type() at a time. (Some type()s are multi-valued, for example a string list.) You can find out what type, T, the variant holds, convert it to a different type using one of the asT() functions, e.g. asSize(), get its value using one of the toT() functions, e.g. toSize(), and check whether the type can be converted to a particular type using canCast().

The methods named toT() (for any supported T, see the Type documentation for a list) are const. If you ask for the stored type, they return a copy of the stored object. If you ask for a type that can be generated from the stored type, toT() copies and converts and leaves the object itself unchanged. If you ask for a type that cannot be generated from the stored type, the result depends on the type (see the function documentation for details).

Note that two data types supported by QCoreVariant are explicitly shared, namely QImage and QPointArray, and in these cases the toT() methods return a shallow copy. In almost all cases you must make a deep copy of the returned values before modifying them.

The asT() functions are not const. They do conversion like the toT() methods, set the variant to hold the converted value, and return a reference to the new contents of the variant.

Here is some example code to demonstrate the use of QCoreVariant:

    QDataStream out(...);
    QCoreVariant v(123);            // The variant now contains an int
    int x = v.toInt();              // x = 123
    out << v;                       // Writes a type tag and an int to out
    v = QCoreVariant("hello");      // The variant now contains a QByteArray
    v = QCoreVariant(tr("hello"));  // The variant now contains a QString
    int y = v.toInt();              // y = 0 since v cannot be converted to an int
    QString s = v.toString();       // s = tr("hello")  (see QObject::tr())
    out << v;                       // Writes a type tag and a QString to out
    ...
    QDataStream in(...);            // (opening the previously written stream)
    in >> v;                        // Reads an Int variant
    int z = v.toInt();              // z = 123
    qDebug("Type is %s",            // prints "Type is int"
            v.typeName());
    v.asInt() += 100;               // The variant now hold the value 223.
    v = QCoreVariant(QStringList());
    v.asStringList().append("Hello");

You can even store QCoreVariantLists and QMap<QString,QCoreVariant>s in a variant, so you can easily construct arbitrarily complex data structures of arbitrary types. This is very powerful and versatile, but may prove less memory and speed efficient than storing specific types in standard data structures.

QCoreVariant also supports the notion of NULL values, where you have a defined type with no value set.

    QCoreVariant x, y(QString()), z(QString(""));
    x.asInt();
    // x.isNull() == true,
    // y.isNull() == true, z.isNull() == false
    // y.isEmpty() == true, z.Empty() == true

Member Type Documentation

enum QCoreVariant::Type

This enum type defines the types of variable that a QCoreVariant can contain.

QCoreVariant::Invalidno type
QCoreVariant::BitArraya QBitArray
QCoreVariant::ByteArraya QByteArray
QCoreVariant::Bitmapa QBitmap
QCoreVariant::Boola bool
QCoreVariant::Brusha QBrush
QCoreVariant::Colora QColor
QCoreVariant::Cursora QCursor
QCoreVariant::Datea QDate
QCoreVariant::DateTimea QDateTime
QCoreVariant::Doublea double
QCoreVariant::Fonta QFont
QCoreVariant::Icona QIcon
QCoreVariant::Imagea QImage
QCoreVariant::Intan int
QCoreVariant::KeySequencea QKeySequence
QCoreVariant::Lista QCoreVariantList
QCoreVariant::LongLonga long long
QCoreVariant::ULongLongan unsigned long long
QCoreVariant::Mapa QMap<QString,QCoreVariant>
QCoreVariant::Palettea QPalette
QCoreVariant::Pena QPen
QCoreVariant::Pixmapa QPixmap
QCoreVariant::Pointa QPoint
QCoreVariant::PointArraya QPointArray
QCoreVariant::Recta QRect
QCoreVariant::Regiona QRegion
QCoreVariant::Sizea QSize
QCoreVariant::SizePolicya QSizePolicy
QCoreVariant::Stringa QString
QCoreVariant::StringLista QStringList
QCoreVariant::Timea QTime
QCoreVariant::UIntan unsigned int
QCoreVariant::UserType 

Note that Qt's definition of bool depends on the compiler. qglobal.h has the system-dependent definition of bool.


Member Function Documentation

QCoreVariant::QCoreVariant ()

Constructs an invalid variant.

QCoreVariant::QCoreVariant ( Type type )

Constructs a null variant of type type.

QCoreVariant::QCoreVariant ( int typeOrUserType, const void * copy )

Constructs variant of type typeOrUserType, and initializes with copy if copy is not 0.

QCoreVariant::QCoreVariant ( const QCoreVariant & p )

Constructs a copy of the variant, p, passed as the argument to this constructor. Usually this is a deep copy, but a shallow copy is made if the stored data type is explicitly shared, as e.g. QImage is.

QCoreVariant::QCoreVariant ( QDataStream & s )

Reads the variant from the data stream, s.

QCoreVariant::QCoreVariant ( int val )

Constructs a new variant with an integer value, val.

QCoreVariant::QCoreVariant ( uint val )

Constructs a new variant with an unsigned integer value, val.

QCoreVariant::QCoreVariant ( Q_LONGLONG val )

Constructs a new variant with a long long integer value, val.

QCoreVariant::QCoreVariant ( Q_ULONGLONG val )

Constructs a new variant with an unsigned long long integer value, val.

QCoreVariant::QCoreVariant ( bool val )

Constructs a new variant with a boolean value, val. The integer argument is a dummy, necessary for compatibility with some compilers.

QCoreVariant::QCoreVariant ( double val )

Constructs a new variant with a floating point value, val.

QCoreVariant::QCoreVariant ( const char * val )

Constructs a new variant with a C-string value of val if val is non-null. The variant creates a deep copy of val.

If val is null, the resulting variant has type Invalid.

QCoreVariant::QCoreVariant ( const QByteArray & val )

Constructs a new variant with a bytearray value, val.

QCoreVariant::QCoreVariant ( const QBitArray & val )

Constructs a new variant with a bitarray value, val.

QCoreVariant::QCoreVariant ( const QString & val )

Constructs a new variant with a string value, val.

QCoreVariant::QCoreVariant ( const QLatin1String & val )

Constructs a new variant with a string value, val.

QCoreVariant::QCoreVariant ( const QStringList & val )

Constructs a new variant with a string list value, val.

QCoreVariant::QCoreVariant ( const QChar & qchar )

QCoreVariant::QCoreVariant ( const QDate & val )

Constructs a new variant with a date value, val.

QCoreVariant::QCoreVariant ( const QTime & val )

Constructs a new variant with a time value, val.

QCoreVariant::QCoreVariant ( const QDateTime & val )

Constructs a new variant with a date/time value, val.

QCoreVariant::QCoreVariant ( const QList<QCoreVariant> & val )

Constructs a new variant with a list value, val.

QCoreVariant::QCoreVariant ( const QMap<QString, QCoreVariant> & val )

Constructs a new variant with a map of QCoreVariants, val.

QCoreVariant::QCoreVariant ( const QSize & val )

Constructs a new variant with a size value of val.

QCoreVariant::QCoreVariant ( const QRect & val )

Constructs a new variant with a rect value of val.

QCoreVariant::QCoreVariant ( const QPoint & val )

Constructs a new variant with a point value of val.

QCoreVariant::QCoreVariant ( const QUrl & val )

Constructs a new variant with a url value of val.

QCoreVariant::~QCoreVariant ()

Destroys the QCoreVariant and the contained object.

Note that subclasses that reimplement clear() should reimplement the destructor to call clear(). This destructor calls clear(), but because it is the destructor, QCoreVariant::clear() is called rather than a subclass's clear().

bool QCoreVariant::canCast ( Type t ) const

Returns true if the variant's type can be cast to the requested type, t. Such casting is done automatically when calling the toInt(), toBool(), ... or asInt(), asBool(), ... methods.

The following casts are done automatically:

TypeAutomatically Cast To
BoolDouble, Int, UInt, LongLong, ULongLong
ColorString
DateString, DateTime
DateTimeString, Date, Time
DoubleString, Int, Bool, UInt
FontString
IntString, Double, Bool, UInt
ListStringList (if the list contains strings or something that can be cast to a string)
StringCString, Int, Uint, Bool, Double, Date, Time, DateTime, KeySequence, Font, Color
CStringString
StringListList
TimeString
UIntString, Double, Bool, Int
KeySequenceString, Int

bool QCoreVariant::cast ( Type t )

Casts the variant to the requested type. If the cast cannot be done, the variant is set to the default value of the requested type (e.g. an empty string if the requested type t is QCoreVariant::String, an empty point array if the requested type t is QCoreVariant::PointArray, etc). Returns true if the current type of the variant was successfully cast; otherwise returns false.

See also canCast().

void QCoreVariant::clear ()

Convert this variant to type Invalid and free up any resources used.

bool QCoreVariant::isNull () const

Returns true if this is a NULL variant, false otherwise.

bool QCoreVariant::isValid () const

Returns true if the storage type of this variant is not QCoreVariant::Invalid; otherwise returns false.

Type QCoreVariant::nameToType ( const char * name )   [static]

Converts the string representation of the storage type given in name, to its enum representation.

If the string representation cannot be converted to any enum representation, the variant is set to Invalid.

QBitArray QCoreVariant::toBitArray () const

Returns the variant as a QBitArray if the variant has type() BitArray; otherwise returns an empty bitarray.

bool QCoreVariant::toBool () const

Returns the variant as a bool if the variant has type() Bool.

Returns true if the variant has type Int, UInt or Double and its value is non-zero, or if the variant has type String and its lower-case content is not empty, "0" or "false"; otherwise returns false.

QByteArray QCoreVariant::toByteArray () const

Returns the variant as a QByteArray if the variant has type() ByteArray; otherwise returns an empty bytearray.

QChar QCoreVariant::toChar () const

QDate QCoreVariant::toDate () const

Returns the variant as a QDate if the variant has type() Date, DateTime or String; otherwise returns an invalid date.

Note that if the type() is String an invalid date will be returned if the string cannot be parsed as a Qt::ISODate format date.

QDateTime QCoreVariant::toDateTime () const

Returns the variant as a QDateTime if the variant has type() DateTime, Date or String; otherwise returns an invalid date/time.

Note that if the type() is String an invalid date/time will be returned if the string cannot be parsed as a Qt::ISODate format date/time.

double QCoreVariant::toDouble ( bool * ok = 0 ) const

Returns the variant as a double if the variant has type() String, ByteArray, Double, Int, UInt, LongLong, ULongLong or Bool; otherwise returns 0.0.

If ok is non-null: *ok is set to true if the value could be converted to a double; otherwise *ok is set to false.

int QCoreVariant::toInt ( bool * ok = 0 ) const

Returns the variant as an int if the variant has type() String, Int, UInt, Double, Bool or KeySequence; otherwise returns 0.

If ok is non-null: *ok is set to true if the value could be converted to an int; otherwise *ok is set to false.

See also canCast().

QList<QCoreVariant> QCoreVariant::toList () const

Returns the variant as a QCoreVariantList if the variant has type() List or StringList; otherwise returns an empty list.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.

    QCoreVariantList list = myVariant.toList();
    QCoreVariantList::Iterator it = list.begin();
    while(it != list.end()) {
        myProcessing(*it);
        ++it;
    }

Q_LONGLONG QCoreVariant::toLongLong ( bool * ok = 0 ) const

Returns the variant as a long long int if the variant has type() LongLong, ULongLong, any type allowing a toInt() conversion; otherwise returns 0.

If ok is non-null: *ok is set to true if the value could be converted to an int; otherwise *ok is set to false.

See also canCast().

QMap<QString, QCoreVariant> QCoreVariant::toMap () const

Returns the variant as a QMap<QString,QCoreVariant> if the variant has type() Map; otherwise returns an empty map.

Note that if you want to iterate over the map, you should iterate over a copy, e.g.

    QCoreVariantMap map = myVariant.toMap();
    QCoreVariantMap::Iterator it = map.begin();
    while(it != map.end()) {
        myProcessing(*it);
        ++it;
    }

QPoint QCoreVariant::toPoint () const

Returns the variant as a QPoint if the variant has type() Point; otherwise returns a null QPoint.

QRect QCoreVariant::toRect () const

Returns the variant as a QRect if the variant has type() Rect; otherwise returns an invalid QRect.

QSize QCoreVariant::toSize () const

Returns the variant as a QSize if the variant has type() Size; otherwise returns an invalid QSize.

QString QCoreVariant::toString () const

Returns the variant as a QString if the variant has type() String, ByteArray, Int, Uint, Bool, Double, Date, Time, DateTime, KeySequence, Font or Color; otherwise returns an empty string.

QStringList QCoreVariant::toStringList () const

Returns the variant as a QStringList if the variant has type() StringList or List of a type that can be converted to QString; otherwise returns an empty list.

Note that if you want to iterate over the list, you should iterate over a copy, e.g.

    QStringList list = myVariant.toStringList();
    QStringList::Iterator it = list.begin();
    while(it != list.end()) {
        myProcessing(*it);
        ++it;
    }

QTime QCoreVariant::toTime () const

Returns the variant as a QTime if the variant has type() Time, DateTime or String; otherwise returns an invalid time.

Note that if the type() is String an invalid time will be returned if the string cannot be parsed as a Qt::ISODate format time.

uint QCoreVariant::toUInt ( bool * ok = 0 ) const

Returns the variant as an unsigned int if the variant has type() String, ByteArray, UInt, Int, Double, or Bool; otherwise returns 0.

If ok is non-null: *ok is set to true if the value could be converted to an unsigned int; otherwise *ok is set to false.

Q_ULONGLONG QCoreVariant::toULongLong ( bool * ok = 0 ) const

Returns the variant as as an unsigned long long int if the variant has type() LongLong, ULongLong, any type allowing a toUInt() conversion; otherwise returns 0.

If ok is non-null: *ok is set to true if the value could be converted to an int; otherwise *ok is set to false.

See also canCast().

QUrl QCoreVariant::toUrl () const

Returns the variant as a QUrl if the variant has type() Url; otherwise returns an invalid QUrl.

Type QCoreVariant::type () const

Returns the storage type of the value stored in the variant. Usually it's best to test with canCast() whether the variant can deliver the data type you are interested in.

const char * QCoreVariant::typeName () const

Returns the name of the type stored in the variant. The returned strings describe the C++ datatype used to store the data: for example, "QFont", "QString", or "QCoreVariantList". An Invalid variant returns 0.

const char * QCoreVariant::typeToName ( Type typ )   [static]

Converts the enum representation of the storage type, typ, to its string representation.

int QCoreVariant::userType () const

Returns the storage type of the value stored in the variant. For non-user types, this is the same as type().

See also type().

bool QCoreVariant::operator!= ( const QCoreVariant & v ) const

Compares this QCoreVariant with v and returns true if they are not equal; otherwise returns false.

QCoreVariant & QCoreVariant::operator= ( const QCoreVariant & variant )

Assigns the value of the variant variant to this variant.

This is a deep copy of the variant, but note that if the variant holds an explicitly shared type such as QImage, a shallow copy is performed.

bool QCoreVariant::operator== ( const QCoreVariant & v ) const

Compares this QCoreVariant with v and returns true if they are equal; otherwise returns false.


Copyright © 2004 Trolltech Trademarks
Qt 4.0.0-b1