QVariant Class ReferenceThe QVariant class acts like a union for the most common Qt data types. More... #include <qvariant.h> Public Members
Static Public Members
Detailed DescriptionThe QVariant 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 QVariant, this would be a problem for QObject::property() and for database work, etc. A QVariant 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 three data types supported by QVariant are explicitly shared, namely QImage, QPointArray, and QCString, 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 QVariant:
QDataStream out(...); QVariant 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 = QVariant("hello"); // The variant now contains a QCString v = QVariant(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 = QVariant( QStringList() ); v.asStringList().append( "Hello" ); You can even store QValueList QVariant also supports the notion of NULL values, where you have a
defined type with no value set.
See the Collection Classes.
See also Miscellaneous Classes and Object Model.
This enum type defines the types of variable that a QVariant can
contain.
Note that Qt's definition of bool depends on the compiler.
qglobal.h has the system-dependent definition of bool.
If you want to modify the QCString after you've passed it to this
constructor, we recommend passing a deep copy (see
QCString::copy()).
If val is null, the resulting variant has type Invalid.
Because QImage is explicitly shared, you may need to pass a deep copy to the variant using QImage::copy(), e.g. if you intend
changing the image you've passed later on.
Because QPointArray is explicitly shared, you may need to pass a
deep copy to the variant using QPointArray::copy(), e.g. if you
intend changing the point array you've passed later on.
Note that subclasses that reimplement clear() should reimplement
the destructor to call clear(). This destructor calls clear(), but
because it is the destructor, QVariant::clear() is called rather
than a subclass's clear().
Tries to convert the variant to hold a QBitArray value. If that is
not possible then the variant is set to an empty bitarray.
Returns a reference to the stored bitarray.
See also toBitArray().
Tries to convert the variant to hold a bitmap value. If that is
not possible the variant is set to a null bitmap.
Returns a reference to the stored bitmap.
See also toBitmap().
Tries to convert the variant to hold a brush value. If that is not
possible the variant is set to a default black brush.
Returns a reference to the stored brush.
See also toBrush().
Tries to convert the variant to hold a QByteArray value. If that
is not possible then the variant is set to an empty bytearray.
Returns a reference to the stored bytearray.
See also toByteArray().
Tries to convert the variant to hold a string value. If that is
not possible the variant is set to an empty string.
Returns a reference to the stored string.
See also toCString().
Tries to convert the variant to hold a QColor value. If that is
not possible the variant is set to an invalid color.
Returns a reference to the stored color.
See also toColor() and QColor::isValid().
Tries to convert the variant to hold a QColorGroup value. If that
is not possible the variant is set to a color group of all black
colors.
Returns a reference to the stored color group.
See also toColorGroup().
Tries to convert the variant to hold a QCursor value. If that is
not possible the variant is set to a default arrow cursor.
Returns a reference to the stored cursor.
See also toCursor().
Tries to convert the variant to hold a QDate value. If that is not
possible then the variant is set to an invalid date.
Returns a reference to the stored date.
See also toDate().
Tries to convert the variant to hold a QDateTime value. If that is
not possible then the variant is set to an invalid date/time.
Returns a reference to the stored date/time.
See also toDateTime().
Tries to convert the variant to hold a QFont. If that is not
possible the variant is set to the application's default font.
Returns a reference to the stored font.
See also toFont().
Tries to convert the variant to hold a QIconSet value. If that is
not possible the variant is set to an empty iconset.
Returns a reference to the stored iconset.
See also toIconSet().
Tries to convert the variant to hold an image value. If that is
not possible the variant is set to a null image.
Returns a reference to the stored image.
See also toImage().
Tries to convert the variant to hold a QKeySequence value. If that
is not possible then the variant is set to an empty key sequence.
Returns a reference to the stored key sequence.
See also toKeySequence().
Note that if you want to iterate over the list, you should iterate
over a copy, e.g.
Note that if you want to iterate over the map, you should iterate
over a copy, e.g.
Tries to convert the variant to hold a QPalette value. If that is
not possible the variant is set to a palette of black colors.
Returns a reference to the stored palette.
See also toString().
Tries to convert the variant to hold a QPen value. If that
is not possible then the variant is set to an empty pen.
Returns a reference to the stored pen.
See also toPen().
Tries to convert the variant to hold a pixmap value. If that is
not possible the variant is set to a null pixmap.
Returns a reference to the stored pixmap.
See also toPixmap().
Tries to convert the variant to hold a point value. If that is not
possible the variant is set to a (0, 0) point.
Returns a reference to the stored point.
See also toPoint().
Tries to convert the variant to hold a QPointArray value. If that
is not possible the variant is set to an empty point array.
Returns a reference to the stored point array.
See also toPointArray().
Tries to convert the variant to hold a rectangle value. If that is
not possible the variant is set to an empty rectangle.
Returns a reference to the stored rectangle.
See also toRect().
Tries to convert the variant to hold a QRegion value. If that is
not possible the variant is set to a null region.
Returns a reference to the stored region.
See also toRegion().
Tries to convert the variant to hold a QSize value. If that is not
possible the variant is set to an invalid size.
Returns a reference to the stored size.
See also toSize() and QSize::isValid().
Tries to convert the variant to hold a QSizePolicy value. If that
fails, the variant is set to an arbitrary (valid) size policy.
Tries to convert the variant to hold a string value. If that is
not possible the variant is set to an empty string.
Returns a reference to the stored string.
See also toString().
Tries to convert the variant to hold a QStringList value. If that
is not possible the variant is set to an empty string list.
Returns a reference to the stored string list.
Note that if you want to iterate over the list, you should
iterate over a copy, e.g.
See also toStringList().
Tries to convert the variant to hold a QTime value. If that is not
possible then the variant is set to an invalid time.
Returns a reference to the stored time.
See also toTime().
The following casts are done automatically:
See also canCast().
Returns TRUE if the storage type of this variant is not
QVariant::Invalid; otherwise returns FALSE.
Returns an iterator to the first item in the list if the variant's
type is appropriate; otherwise returns a null iterator.
Returns the end iterator for the list if the variant's type is
appropriate; otherwise returns a null iterator.
Returns an iterator to the first item in the map, if the variant's
type is appropriate; otherwise returns a null iterator.
Returns the end iterator for the map, if the variant's type is
appropriate; otherwise returns a null iterator.
Returns an iterator to the item in the map with key as key, if
the variant's type is appropriate and key is a valid key;
otherwise returns a null iterator.
If the string representation cannot be converted to any enum
representation, the variant is set to Invalid.
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.
Returns an iterator to the first string in the list if the
variant's type is StringList; otherwise returns a null iterator.
Returns the end iterator for the list if the variant's type is
StringList; otherwise returns a null iterator.
See also asBitArray().
See also asBitmap().
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.
See also asBool().
See also asBrush().
See also asByteArray().
See also asCString().
See also asColor().
See also asColorGroup().
See also asCursor().
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.
See also asDate().
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.
See also asDateTime().
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.
See also asDouble().
See also asFont().
See also asIconSet().
See also asImage().
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 asInt() and canCast().
Note that not all Ints and Strings are valid key sequences and in
such cases an empty key sequence will be returned.
See also asKeySequence().
Note that if you want to iterate over the list, you should iterate
over a copy, e.g.
See also asList().
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 asLongLong() and canCast().
Note that if you want to iterate over the map, you should iterate
over a copy, e.g.
See also asMap().
See also asPalette().
See also asPen().
See also asPixmap().
See also asPoint().
See also asPointArray().
See also asRect().
See also asRegion().
See also asSize().
See also asString().
Note that if you want to iterate over the list, you should iterate
over a copy, e.g.
See also asStringList().
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.
See also asTime().
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.
See also asUInt().
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 asULongLong() and 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.
This file is part of the Qt toolkit.
Copyright © 1995-2003
Trolltech. All Rights Reserved. |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 3.2 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com