Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions | ![]() |
The QCoreVariant class acts like a union for the most common Qt data types. More...
#include <QCoreVariant>
Inherited by QVariant.
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
This enum type defines the types of variable that a QCoreVariant can contain.
QCoreVariant::Invalid | no type |
QCoreVariant::BitArray | a QBitArray |
QCoreVariant::ByteArray | a QByteArray |
QCoreVariant::Bitmap | a QBitmap |
QCoreVariant::Bool | a bool |
QCoreVariant::Brush | a QBrush |
QCoreVariant::Color | a QColor |
QCoreVariant::Cursor | a QCursor |
QCoreVariant::Date | a QDate |
QCoreVariant::DateTime | a QDateTime |
QCoreVariant::Double | a double |
QCoreVariant::Font | a QFont |
QCoreVariant::IconSet | a QIconSet |
QCoreVariant::Image | a QImage |
QCoreVariant::Int | an int |
QCoreVariant::KeySequence | a QKeySequence |
QCoreVariant::List | a QCoreVariantList |
QCoreVariant::LongLong | a long long |
QCoreVariant::ULongLong | an unsigned long long |
QCoreVariant::Map | a QMap<QString,QCoreVariant> |
QCoreVariant::Palette | a QPalette |
QCoreVariant::Pen | a QPen |
QCoreVariant::Pixmap | a QPixmap |
QCoreVariant::Point | a QPoint |
QCoreVariant::PointArray | a QPointArray |
QCoreVariant::Rect | a QRect |
QCoreVariant::Region | a QRegion |
QCoreVariant::Size | a QSize |
QCoreVariant::SizePolicy | a QSizePolicy |
QCoreVariant::String | a QString |
QCoreVariant::StringList | a QStringList |
QCoreVariant::Time | a QTime |
QCoreVariant::UInt | an unsigned int |
QCoreVariant::UserType |
Note that Qt's definition of bool depends on the compiler. qglobal.h has the system-dependent definition of bool.
Constructs an invalid variant.
Constructs a null variant of type type.
Constructs a non-null variant of type type, and initializes with v if v is not 0.
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.
Reads the variant from the data stream, s.
Constructs a new variant with an integer value, val.
Constructs a new variant with an unsigned integer value, val.
Constructs a new variant with a long long integer value, val.
Constructs a new variant with an unsigned long long integer value, val.
Constructs a new variant with a boolean value, val. The integer argument is a dummy, necessary for compatibility with some compilers.
Constructs a new variant with a floating point value, 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.
Constructs a new variant with a bytearray value, val.
Constructs a new variant with a bitarray value, val.
Constructs a new variant with a string value, val.
Constructs a new variant with a string list value, val.
Constructs a new variant with a date value, val.
Constructs a new variant with a time value, val.
Constructs a new variant with a date/time value, val.
Constructs a new variant with a list value, val.
Constructs a new variant with a map of QCoreVariants, val.
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().
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:
Type | Automatically Cast To |
---|---|
Bool | Double, Int, UInt, LongLong, ULongLong |
Color | String |
Date | String, DateTime |
DateTime | String, Date, Time |
Double | String, Int, Bool, UInt |
Font | String |
Int | String, Double, Bool, UInt |
List | StringList (if the list contains strings or something that can be cast to a string) |
String | CString, Int, Uint, Bool, Double, Date, Time, DateTime, KeySequence, Font, Color |
CString | String |
StringList | List |
Time | String |
UInt | String, Double, Bool, Int |
KeySequence | String, Int |
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().
Convert this variant to type Invalid and free up any resources used.
Returns true if this is a NULL variant, false otherwise.
Returns true if the storage type of this variant is not QCoreVariant::Invalid; otherwise returns false.
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.
Returns the variant as a QBitArray if the variant has type() BitArray; otherwise returns an empty bitarray.
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.
Returns the variant as a QByteArray if the variant has type() ByteArray; otherwise returns an empty bytearray.
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.
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.
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.
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().
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; }
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().
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; }
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.
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; }
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.
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.
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().
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.
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.
Converts the enum representation of the storage type, typ, to its string representation.
Compares this QCoreVariant with v and returns true if they are not equal; otherwise returns false.
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.
Compares this QCoreVariant with v and returns true if they are equal; otherwise returns false.
Copyright © 2004 Trolltech. | Trademarks | Qt 4.0.0-tp1 |