QVariant(3qt)
π QVariant π β the QVariant(3qt) manual, but make it cute
okay bestie, sit down, hydrate, because we're about to talk about the most useful little shape-shifter in all of Qt π¦
β¨ NAME β¨
QVariant</purpose> β it acts like a union for the most common Qt data types π
πΈ SYNOPSIS πΈ
#include <qvariant.h>
π Public Members
enum Type { Invalid, Map, List, String, StringList, Font, Pixmap, Brush,
Rect, Size, Color, Palette, ColorGroup, IconSet, Point, Image, Int,
UInt, Bool, Double, CString, PointArray, Region, Bitmap, Cursor,
SizePolicy, Date, Time, DateTime, ByteArray, BitArray, KeySequence,
Pen, LongLong, ULongLong }
QVariant ()
~QVariant ()
QVariant ( const QVariant & p )
QVariant ( QDataStream & s )
QVariant ( const QString & val )
QVariant ( const QCString & val )
QVariant ( const char * val )
QVariant ( const QStringList & val )
QVariant ( const QFont & val )
QVariant ( const QPixmap & val )
QVariant ( const QImage & val )
QVariant ( const QBrush & val )
QVariant ( const QPoint & val )
QVariant ( const QRect & val )
QVariant ( const QSize & val )
QVariant ( const QColor & val )
QVariant ( const QPalette & val )
QVariant ( const QColorGroup & val )
QVariant ( const QIconSet & val )
QVariant ( const QPointArray & val )
QVariant ( const QRegion & val )
QVariant ( const QBitmap & val )
QVariant ( const QCursor & val )
QVariant ( const QDate & val )
QVariant ( const QTime & val )
QVariant ( const QDateTime & val )
QVariant ( const QByteArray & val )
QVariant ( const QBitArray & val )
QVariant ( const QKeySequence & val )
QVariant ( const QPen & val )
QVariant ( const QValueList<QVariant> & val )
QVariant ( const QMap<QString, QVariant> & val )
QVariant ( int val )
QVariant ( uint val )
QVariant ( Q_LLONG val )
QVariant ( Q_ULLONG val )
QVariant ( bool val, int )
QVariant ( double val )
QVariant ( QSizePolicy val )
QVariant & operator= ( const QVariant & variant )
bool operator== ( const QVariant & v ) const
bool operator!= ( const QVariant & v ) const
Type type () const
const char * typeName () const
bool canCast ( Type t ) const
bool cast ( Type t )
bool isValid () const
bool isNull () const
void clear ()
const QString toString () const
const QCString toCString () const
const QStringList toStringList () const
const QFont toFont () const
const QPixmap toPixmap () const
const QImage toImage () const
const QBrush toBrush () const
const QPoint toPoint () const
const QRect toRect () const
const QSize toSize () const
const QColor toColor () const
const QPalette toPalette () const
const QColorGroup toColorGroup () const
const QIconSet toIconSet () const
const QPointArray toPointArray () const
const QBitmap toBitmap () const
const QRegion toRegion () const
const QCursor toCursor () const
const QDate toDate () const
const QTime toTime () const
const QDateTime toDateTime () const
const QByteArray toByteArray () const
const QBitArray toBitArray () const
const QKeySequence toKeySequence () const
const QPen toPen () const
int toInt ( bool * ok = 0 ) const
uint toUInt ( bool * ok = 0 ) const
Q_LLONG toLongLong ( bool * ok = 0 ) const
Q_ULLONG toULongLong ( bool * ok = 0 ) const
bool toBool () const
double toDouble ( bool * ok = 0 ) const
const QValueList<QVariant> toList () const
const QMap<QString, QVariant> toMap () const
QSizePolicy toSizePolicy () const
QValueListConstIterator<QString> stringListBegin () const (obsolete)
QValueListConstIterator<QString> stringListEnd () const (obsolete)
QValueListConstIterator<QVariant> listBegin () const (obsolete)
QValueListConstIterator<QVariant> listEnd () const (obsolete)
QMapConstIterator<QString, QVariant> mapBegin () const (obsolete)
QMapConstIterator<QString, QVariant> mapEnd () const (obsolete)
QMapConstIterator<QString, QVariant> mapFind ( const QString & key )
const (obsolete)
QString & asString ()
QCString & asCString ()
QStringList & asStringList ()
QFont & asFont ()
QPixmap & asPixmap ()
QImage & asImage ()
QBrush & asBrush ()
QPoint & asPoint ()
QRect & asRect ()
QSize & asSize ()
QColor & asColor ()
QPalette & asPalette ()
QColorGroup & asColorGroup ()
QIconSet & asIconSet ()
QPointArray & asPointArray ()
QBitmap & asBitmap ()
QRegion & asRegion ()
QCursor & asCursor ()
QDate & asDate ()
QTime & asTime ()
QDateTime & asDateTime ()
QByteArray & asByteArray ()
QBitArray & asBitArray ()
QKeySequence & asKeySequence ()
QPen & asPen ()
int & asInt ()
uint & asUInt ()
Q_LLONG & asLongLong ()
Q_ULLONG & asULongLong ()
bool & asBool ()
double & asDouble ()
QValueList<QVariant> & asList ()
QMap<QString, QVariant> & asMap ()
QSizePolicy & asSizePolicy ()
π Static Public Members
const char * typeToName ( Type typ )
Type nameToType ( const char * name )
π DESCRIPTION π
The QVariant class acts like a union for the most common Qt data types. periodt.
So here's the tea, bestie: C++ is so dramatic and forbids unions from including types that have non-default constructors or destructors, which means basically every interesting Qt class gets left out of the union party π. And without QVariant, that would be a whole problem for QObject::property() and for database work, etc. So QVariant said "not me being the solution" and showed up π
.
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. She contains multitudes, lowkey.) Here's how you work with her:
- You can find out what type, T, the variant holds π΅οΈββοΈ
- You convert it to a different type using one of the
asT()functions, e.g.asSize() - You get its value using one of the
toT()functions, e.g.toSize() - You 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. Real "look but don't touch" energy:
- 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 β iykyk).
β οΈ Note that three data types supported byQVariantare explicitly shared, namelyQImage,QPointArray, andQCString, and in these cases thetoT()methods return a shallow copy. In almost all cases you must make a deep copy of the returned values before modifying them. Don't get caught out fr fr π
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. She's a mutable queen π
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>s and QMap<QString,QVariant>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. Iconic but expensive, we love a high-maintenance queen πβ¨
QVariant also supports the notion of NULL values, where you have a defined type with no value set.
QVariant x, y( QString() ), z( QString("") );
x.asInt();
// x.isNull() == TRUE, y.isNull() == TRUE, z.isNull() == FALSE
See the Collection Classes.
See also Miscellaneous Classes and Object Model. π«Ά
π§ Member Type Documentation π§
QVariant::Type
This enum type defines the types of variable that a QVariant can contain. Here's the full roster, no cuts, no skips π:
QVariant::Invalidβ no type πQVariant::BitArrayβ aQBitArrayQVariant::ByteArrayβ aQByteArrayQVariant::Bitmapβ aQBitmapQVariant::Boolβ aboolQVariant::Brushβ aQBrushQVariant::Colorβ aQColorQVariant::ColorGroupβ aQColorGroupQVariant::Cursorβ aQCursorQVariant::Dateβ aQDateQVariant::DateTimeβ aQDateTimeQVariant::Doubleβ adoubleQVariant::Fontβ aQFontQVariant::IconSetβ aQIconSetQVariant::Imageβ aQImageQVariant::Intβ anintQVariant::KeySequenceβ aQKeySequenceQVariant::Listβ aQValueList<QVariant>QVariant::LongLongβ along longQVariant::ULongLongβ anunsigned long longQVariant::Mapβ aQMap<QString,QVariant>QVariant::Paletteβ aQPaletteQVariant::Penβ aQPenQVariant::Pixmapβ aQPixmapQVariant::Pointβ aQPointQVariant::PointArrayβ aQPointArrayQVariant::Rectβ aQRectQVariant::Regionβ aQRegionQVariant::Sizeβ aQSizeQVariant::SizePolicyβ aQSizePolicyQVariant::Stringβ aQStringQVariant::CStringβ aQCStringQVariant::StringListβ aQStringListQVariant::Timeβ aQTimeQVariant::UIntβ anunsigned int
π Note that Qt's definition ofbooldepends on the compiler.qglobal.hhas the system-dependent definition ofbool. (Not us being vague, it's literally the compiler's fault bestie π)
π MEMBER FUNCTION DOCUMENTATION π
QVariant::QVariant ()
Constructs an invalid variant. She's giving nothing, she's empty, she's a blank canvas π
QVariant::QVariant ( bool val, int )
Constructs a new variant with a boolean value, val. The integer argument is a dummy, necessary for compatibility with some compilers. Not her fault, the compilers were being weird π
QVariant::QVariant ( double val )
Constructs a new variant with a floating point value, val. π
QVariant::QVariant ( QSizePolicy val )
Constructs a new variant with a size policy value, val.
QVariant::QVariant ( const QVariant & 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. Yes, she's a copy with β¨layersβ¨
QVariant::QVariant ( QDataStream & s )
Reads the variant from the data stream, s. πΎ
QVariant::QVariant ( const QString & val )
Constructs a new variant with a string value, val.
QVariant::QVariant ( const QCString & val )
Constructs a new variant with a C-string value, val.
π If you want to modify theQCStringafter you've passed it to this constructor, we recommend passing a deep copy (seeQCString::copy()).
QVariant::QVariant ( const char * val )
okay so bestie, if val is not null, this makes a new variant holding a C-string value β and it does a deep copy of val π. But if val is null? The variant's type is Invalid, periodt.
QVariant::QVariant ( const QStringList & val )
New variant, who dis? It's holding a string list value, val β¨.
QVariant::QVariant ( const QFont & val )
Constructs a new variant with a font value, val π.
QVariant::QVariant ( const QPixmap & val )
Constructs a new variant with a pixmap value, val πΌοΈ.
QVariant::QVariant ( const QImage & val )
Constructs a new variant with an image value, val πΈ.
β οΈ BecauseQImageis explicitly shared, you may need to pass a deep copy to the variant usingQImage::copy(), e.g. if you intend changing the image you've passed later on. no cap.
QVariant::QVariant ( const QBrush & val )
Constructs a new variant with a brush value, val ποΈ.
QVariant::QVariant ( const QPoint & val )
Constructs a new variant with a point value, val π.
QVariant::QVariant ( const QRect & val )
Constructs a new variant with a rect value, val π².
QVariant::QVariant ( const QSize & val )
Constructs a new variant with a size value, val π.
QVariant::QVariant ( const QColor & val )
Constructs a new variant with a color value, val π.
QVariant::QVariant ( const QPalette & val )
Constructs a new variant with a color palette value, val π¨.
QVariant::QVariant ( const QColorGroup & val )
Constructs a new variant with a color group value, val π .
QVariant::QVariant ( const QIconSet & val )
Constructs a new variant with an icon set value, val β.
QVariant::QVariant ( const QPointArray & val )
Constructs a new variant with a point array value, val πΊ.
β οΈ BecauseQPointArrayis explicitly shared, you may need to pass a deep copy to the variant usingQPointArray::copy(), e.g. if you intend changing the point array you've passed later on. fr fr.
QVariant::QVariant ( const QRegion & val )
Constructs a new variant with a region value, val πΊοΈ.
QVariant::QVariant ( const QBitmap & val )
Constructs a new variant with a bitmap value, val π€.
QVariant::QVariant ( const QCursor & val )
Constructs a new variant with a cursor value, val π±οΈ.
QVariant::QVariant ( const QDate & val )
Constructs a new variant with a date value, val π .
QVariant::QVariant ( const QTime & val )
Constructs a new variant with a time value, val β°.
QVariant::QVariant ( const QDateTime & val )
Constructs a new variant with a date/time value, val π°οΈ.
QVariant::QVariant ( const QByteArray & val )
Constructs a new variant with a bytearray value, val π§±.
QVariant::QVariant ( const QBitArray & val )
Constructs a new variant with a bitarray value, val π’.
QVariant::QVariant ( const QKeySequence & val )
Constructs a new variant with a key sequence value, val β¨οΈ.
QVariant::QVariant ( const QPen & val )
Constructs a new variant with a pen value, val βοΈ.
QVariant::QVariant ( const QValueList<QVariant> & val )
Constructs a new variant with a list value, val π.
QVariant::QVariant ( const QMap<QString, QVariant> & val )
Constructs a new variant with a map of QVariants, val ποΈ.
QVariant::QVariant ( int val )
Constructs a new variant with an integer value, val π’.
QVariant::QVariant ( uint val )
Constructs a new variant with an unsigned integer value, val π’.
QVariant::QVariant ( Q_LLONG val )
Constructs a new variant with a long long integer value, val π’.
QVariant::QVariant ( Q_ULLONG val )
Constructs a new variant with an unsigned long long integer value, val π’.
QVariant::~QVariant ()
Destroys the QVariant and the contained object π.
π Note that subclasses that reimplementclear()should reimplement the destructor to callclear(). This destructor callsclear(), but because it is the destructor,QVariant::clear()is called rather than a subclass'sclear(). iykyk.
QBitArray & QVariant::asBitArray ()
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().
QBitmap & QVariant::asBitmap ()
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().
bool & QVariant::asBool ()
Returns the variant's value as bool reference π.
QBrush & QVariant::asBrush ()
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().
QByteArray & QVariant::asByteArray ()
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().
QCString & QVariant::asCString ()
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().
QColor & QVariant::asColor ()
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().
QColorGroup & QVariant::asColorGroup ()
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().
QCursor & QVariant::asCursor ()
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().
QDate & QVariant::asDate ()
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().
QDateTime & QVariant::asDateTime ()
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().
double & QVariant::asDouble ()
Returns the variant's value as double reference π.
QFont & QVariant::asFont ()
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().
QIconSet & QVariant::asIconSet ()
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().
QImage & QVariant::asImage ()
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().
int & QVariant::asInt ()
Returns the variant's value as int reference π’.
QKeySequence & QVariant::asKeySequence ()
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().
QValueList<QVariant> & QVariant::asList ()
Returns the variant's value as variant list reference π.
π Note that if you want to iterate over the list, you should iterate over a copy, e.g.
QValueList<QVariant> list = myVariant.asList();
QValueList<QVariant>::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
Q_LLONG & QVariant::asLongLong ()
Returns the variant's value as long long reference π’.
QMap<QString, QVariant> & QVariant::asMap ()
Returns the variant's value as variant map reference ποΈ.
π Note that if you want to iterate over the map, you should iterate over a copy, e.g.
QMap<QString, QVariant> map = myVariant.asMap();
QMap<QString, QVariant>::Iterator it = map.begin();
while( it != map.end() ) {
myProcessing( *it );
++it;
}
QPalette & QVariant::asPalette ()
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().
QPen & QVariant::asPen ()
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().
QPixmap & QVariant::asPixmap ()
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().
QPoint & QVariant::asPoint ()
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().
QPointArray & QVariant::asPointArray ()
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().
QRect & QVariant::asRect ()
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().
QRegion & QVariant::asRegion ()
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().
QSize & QVariant::asSize ()
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().
QSizePolicy & QVariant::asSizePolicy ()
Tries to convert the variant to hold a QSizePolicy value. If that fails, the variant is set to an arbitrary (valid) size policy π.
QString & QVariant::asString ()
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().
QStringList & QVariant::asStringList ()
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.
QStringList list = myVariant.asStringList();
QStringList::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
See also: toStringList().
QTime & QVariant::asTime ()
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().
uint & QVariant::asUInt ()
Hands you the variant's value as an unsigned int reference. Yep, a reference, so you can poke it. π
Q_ULLONG & QVariant::asULongLong ()
Same energy, different type: gives you the variant's value as an unsigned long long reference. π«Ά
bool QVariant::canCast ( Type t ) const
Returns TRUE if the variant's type can be cast to the type you asked for, t. Vibe check before the glow-up. β¨
This kind of casting is literally what happens automatically when you call toInt(), toBool(), ... or asInt(), asBool(), ... β the vibes convert themselves.
The following casts are done automatically:
<center>.nf
</center>
bool QVariant::cast ( Type t )
Casts the variant to the requested type. πͺ
If the cast cannot be done, the variant gets set to the default value of the requested type β so like, an empty string if the requested type t is QVariant::String, an empty point array if the requested type t is QVariant::PointArray, etc.
Returns TRUE if the current type of the variant was successfully cast; otherwise returns FALSE.
See also canCast().
void QVariant::clear ()
Converts this variant to type Invalid and frees up any resources it was using. It's giving "we're done here, release the memory" π
bool QVariant::isNull () const
Returns TRUE if this is a NULL variant, FALSE otherwise.
bool QVariant::isValid () const
Returns TRUE if the storage type of this variant is not QVariant::Invalid; otherwise returns FALSE. π
QValueListConstIterator<QVariant> QVariant::listBegin () const
This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.
Returns an iterator to the first item in the list if the variant's type is appropriate; otherwise returns a null iterator.
QValueListConstIterator<QVariant> QVariant::listEnd () const
This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.
Returns the end iterator for the list if the variant's type is appropriate; otherwise returns a null iterator.
QMapConstIterator<QString, QVariant> QVariant::mapBegin () const
This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.
Returns an iterator to the first item in the map, if the variant's type is appropriate; otherwise returns a null iterator.
QMapConstIterator<QString, QVariant> QVariant::mapEnd () const
This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.
Returns the end iterator for the map, if the variant's type is appropriate; otherwise returns a null iterator.
QMapConstIterator<QString, QVariant> QVariant::mapFind ( const QString & key ) const
This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.
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.
Type QVariant::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.
bool QVariant::operator!= ( const QVariant & v ) const
Compares this QVariant with v and returns TRUE if they are not equal; otherwise returns FALSE. Not me checking if they're different π
QVariant & QVariant::operator= ( const QVariant & variant )
Assigns the value of the variant variant to this variant.
This is a deep copy of the variant, but bestie, note this: if the variant holds an explicitly shared type such as QImage, a shallow copy is performed instead. π«
bool QVariant::operator== ( const QVariant & v ) const
Compares this QVariant with v and returns TRUE if they are equal; otherwise returns FALSE.
QValueListConstIterator<QString> QVariant::stringListBegin () const
This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.
Returns an iterator to the first string in the list if the variant's type is StringList; otherwise returns a null iterator.
QValueListConstIterator<QString> QVariant::stringListEnd () const
This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.
Returns the end iterator for the list if the variant's type is StringList; otherwise returns a null iterator.
const QBitArray QVariant::toBitArray () const
Returns the variant as a QBitArray if the variant has type() BitArray; otherwise returns an empty bitarray.
See also asBitArray().
const QBitmap QVariant::toBitmap () const
Returns the variant as a QBitmap if the variant has type() Bitmap; otherwise returns a null QBitmap.
See also asBitmap().
bool QVariant::toBool () const
Returns the variant as a bool if the variant can be cast to Bool; otherWise returns FALSE. (yes, the capital W in "otherWise" is literally in the docs, we keep it fr fr)
Returns TRUE if the variant has a numeric type and its value is non-zero, or if the variant has type String, ByteArray or CString and its lower-case content is not empty, "0" or "false"; otherwise returns FALSE.
See alsoasBool()andcanCast().
const QBrush QVariant::toBrush () const
Returns the variant as a QBrush if the variant has type() Brush; otherwise returns a default brush (with all black colors).
See also asBrush().
const QByteArray QVariant::toByteArray () const
Returns the variant as a QByteArray if the variant can be cast to a ByteArray; otherwise returns an empty bytearray.
See alsoasByteArray()andcanCast().
const QCString QVariant::toCString () const
Returns the variant as a QCString if the variant can be cast to a CString; otherwise returns 0. π«₯
See alsoasCString()andcanCast().
const QColor QVariant::toColor () const
Returns the variant as a QColor if the variant can be cast to Color; otherwise returns an invalid color.
See alsoasColor()andcanCast().
const QColorGroup QVariant::toColorGroup () const
Returns the variant as a QColorGroup if the variant has type() ColorGroup; otherwise returns a completely black color group. π€
See also asColorGroup().
const QCursor QVariant::toCursor () const
Returns the variant as a QCursor if the variant has type() Cursor; otherwise returns the default arrow cursor.
See also asCursor().
const QDate QVariant::toDate () const
Returns the variant as a QDate if the variant can be cast to Date; otherwise returns an invalid date.
Note that if the type() is String, CString or ByteArray an invalid date will be returned if the string cannot be parsed as a Qt::ISODate format date. No vibes, no date. π
See alsoasDate()andcanCast().
const QDateTime QVariant::toDateTime () const
Returns the variant as a QDateTime if the variant can be cast to DateTime; otherwise returns an invalid QDateTime.
Note that if the type() is String, CString or ByteArray an invalid QDateTime will be returned if the string cannot be parsed as a Qt::ISODate format date/time.
See also asDateTime().
double QVariant::toDouble ( bool * ok = 0 ) const
Returns the variant as a double if the variant can be cast to Double; 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. Thanks for the receipt bestie π§Ύ
See alsoasDouble()andcanCast().
const QFont QVariant::toFont () const
Returns the variant as a QFont if the variant can be cast to Font; otherwise returns the application's default font.
See alsoasFont()andcanCast().
const QIconSet QVariant::toIconSet () const
Returns the variant as a QIconSet if the variant has type() IconSet; otherwise returns an icon set of null pixmaps.
See also asIconSet().
const QImage QVariant::toImage () const
Returns the variant as a QImage if the variant has type() Image; otherwise returns a null image.
See also asImage().
int QVariant::toInt ( bool * ok = 0 ) const
Returns the variant as an int if the variant can be cast to Int; 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 alsoasInt()andcanCast().
const QKeySequence QVariant::toKeySequence () const
Returns the variant as a QKeySequence if the variant can be cast to a KeySequence; otherwise returns an empty key sequence.
See alsoasKeySequence()andcanCast().
const QValueList<QVariant> QVariant::toList () const
Returns the variant as a QValueList<QVariant> 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.
QValueList<QVariant> list = myVariant.toList();
QValueList<QVariant>::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
See also asList().
Q_LLONG QVariant::toLongLong ( bool * ok = 0 ) const
Returns the variant as a long long int if the variant can be cast to LongLong; 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 alsoasLongLong()andcanCast().
const QMap<QString, QVariant> QVariant::toMap () const
Returns the variant as a QMap<QString,QVariant> 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.
QMap<QString, QVariant> map = myVariant.toMap();
QMap<QString, QVariant>::Iterator it = map.begin();
while( it != map.end() ) {
myProcessing( *it );
++it;
}
See also asMap().
const QPalette QVariant::toPalette () const
Returns the variant as a QPalette if the variant has type() Palette; otherwise returns a completely black palette. π€
See also asPalette().
const QPen QVariant::toPen () const
Returns the variant as a QPen if the variant has type() Pen; otherwise returns an empty QPen.
See also asPen().
const QPixmap QVariant::toPixmap () const
Returns the variant as a QPixmap if the variant has type() Pixmap; otherwise returns a null pixmap.
See also asPixmap().
const QPoint QVariant::toPoint () const
Returns the variant as a QPoint if the variant has type() Point; otherwise returns a point (0, 0). Origin core β¨
See also asPoint().
const QPointArray QVariant::toPointArray () const
Returns the variant as a QPointArray if the variant has type() PointArray; otherwise returns an empty QPointArray.
See also asPointArray().
const QRect QVariant::toRect () const
Returns the variant as a QRect if the variant has type() Rect; otherwise returns an empty rectangle.
See also asRect().
const QRegion QVariant::toRegion () const
Returns the variant as a QRegion if the variant has type() Region; otherwise returns an empty QRegion.
See also asRegion().
const QSize QVariant::toSize () const
Returns the variant as a QSize if the variant has type() Size; otherwise returns an invalid size.
See also asSize().
QSizePolicy QVariant::toSizePolicy () const
Gives you back the variant as a QSizePolicy if its type() is SizePolicy β otherwise you get an undefined (but still legal, don't worry bestie) size policy. π
const QString QVariant::toString () const
Hands you the variant as a QString if the variant can be cast to String. If it can't? You get QString::null. Not the vibe, but okay. π
See also asString() and canCast().
const QStringList QVariant::toStringList () const
Returns the variant as a QStringList if its type() is StringList, or List of a type that can be converted to QString. If not, you get an empty list. π«Ά
Heads up bestie: if you wanna iterate over the list, iterate over a copy. No cap. Here's the exact move:
QStringList list = myVariant.toStringList();
QStringList::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
See also asStringList().
const QTime QVariant::toTime () const
Returns the variant as a QTime if the variant can be cast to Time. Otherwise it gives you back an invalid date. Oop. π
Note: if thetype()isString,CStringorByteArray, you get an invalid time back when the string can't be parsed as aQt::ISODateformat time.
See also asTime().
uint QVariant::toUInt ( bool * ok = 0 ) const
Returns the variant as an unsigned int if the variant can be cast to UInt. Otherwise, boom, 0. β¨
If ok is non-null: *ok gets set to TRUE if the value could be converted to an unsigned int; otherwise *ok gets set to FALSE.
See also asUInt() and canCast().
Q_ULLONG QVariant::toULongLong ( bool * ok = 0 ) const
Returns the variant as as an unsigned long long int if the variant can be cast to ULongLong. Otherwise it's 0. (Yes the original says "as as", we're keeping it exactly, iconic typo fr fr π)
If ok is non-null: *ok gets set to TRUE if the value could be converted to an int; otherwise *ok gets set to FALSE.
See also asULongLong() and canCast().
Type QVariant::type () const
Returns the storage type of the value stored in the variant. But lowkey, it's usually best to test with canCast() whether the variant can actually deliver the data type you're interested in. π
const char * QVariant::typeName () const
Returns the name of the type stored in the variant. The strings it gives back describe the C++ datatype used to store the data β for example, "QFont", "QString", or "QValueList<QVariant>". An Invalid variant returns 0. π
const char * QVariant::typeToName ( Type typ ) [static]
Converts the enum representation of the storage type, typ, to its string representation. Slay. β¨
π SEE ALSO π
π COPYRIGHT π
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement. (Read the license bestie, it's giving legally binding π )
π AUTHOR π
Generated automatically from the source code.
π BUGS π
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports help us to help you. Thank you. π«Ά
The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. (Not official, but we still ate β¨)
If you find errors in this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qvariant.3qt) and the Qt version (3.3.8). π
And that's a wrap on the whole QVariant(3qt) page bestie β this came from Trolltech AS, dated 2 February 2007, for Qt version 3.3.8. Go forth and cast responsibly. πβ¨π¦
π¦ where this page comes from
- fedora 43: qt3-devel-docs 3.3.8b-102.fc43 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- fedora 44: qt3-devel-docs 3.3.8b-103.fc44 Β· QPL-1.0 OR GPL-2.0-only OR GPL-3.0-only
- Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms