返回介绍

QVariant Class

发布于 2019-10-04 15:03:46 字数 38519 浏览 979 评论 0 收藏 0

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

#include <qvariant.h>

List of all member functions.

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 }
  • QVariant ()
  • ~QVariant ()
  • QVariant ( constQVariant&p )
  • QVariant ( QDataStream&s )
  • QVariant ( constQString&val )
  • QVariant ( constQCString&val )
  • QVariant ( constchar*val )
  • QVariant ( constQStringList&val )
  • QVariant ( constQFont&val )
  • QVariant ( constQPixmap&val )
  • QVariant ( constQImage&val )
  • QVariant ( constQBrush&val )
  • QVariant ( constQPoint&val )
  • QVariant ( constQRect&val )
  • QVariant ( constQSize&val )
  • QVariant ( constQColor&val )
  • QVariant ( constQPalette&val )
  • QVariant ( constQColorGroup&val )
  • QVariant ( constQIconSet&val )
  • QVariant ( constQPointArray&val )
  • QVariant ( constQRegion&val )
  • QVariant ( constQBitmap&val )
  • QVariant ( constQCursor&val )
  • QVariant ( constQDate&val )
  • QVariant ( constQTime&val )
  • QVariant ( constQDateTime&val )
  • QVariant ( constQByteArray&val )
  • QVariant ( constQBitArray&val )
  • QVariant ( constQKeySequence&val )
  • QVariant ( constQValueList<QVariant>&val )
  • QVariant ( constQMap<QString,QVariant>&val )
  • QVariant ( intval )
  • QVariant ( uintval )
  • QVariant ( boolval, int )
  • QVariant ( doubleval )
  • QVariant ( QSizePolicyval )
  • QVariant & operator= ( constQVariant&variant )
  • bool operator== ( constQVariant&v ) const
  • bool operator!= ( constQVariant&v ) const
  • Type type () const
  • const char * typeName () const
  • bool canCast ( Typet ) const
  • bool cast ( Typet )
  • bool isValid () 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
  • int toInt ( bool*ok = 0 ) const
  • uint toUInt ( 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
  • QValueListConstIterator<QString> stringListEnd () const
  • QValueListConstIterator<QVariant> listBegin () const
  • QValueListConstIterator<QVariant> listEnd () const
  • QMapConstIterator<QString, QVariant> mapBegin () const
  • QMapConstIterator<QString, QVariant> mapEnd () const
  • QMapConstIterator<QString, QVariant> mapFind ( constQString&key ) const
  • 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 ()
  • int & asInt ()
  • uint & asUInt ()
  • bool & asBool ()
  • double & asDouble ()
  • QValueList<QVariant> & asList ()
  • QMap<QString, QVariant> & asMap ()
  • QSizePolicy & asSizePolicy ()

Static Public Members

  • const char * typeToName ( Typetyp )
  • Type nameToType ( constchar*name )

Detailed Description

The 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 QValueLists and QMaps 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.

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.

  • QVariant::Invalid - no type
  • QVariant::BitArray - a QBitArray
  • QVariant::ByteArray - a QByteArray
  • QVariant::Bitmap - a QBitmap
  • QVariant::Bool - a bool
  • QVariant::Brush - a QBrush
  • QVariant::Color - a QColor
  • QVariant::ColorGroup - a QColorGroup
  • QVariant::Cursor - a QCursor
  • QVariant::Date - a QDate
  • QVariant::DateTime - a QDateTime
  • QVariant::Double - a double
  • QVariant::Font - a QFont
  • QVariant::IconSet - a QIconSet
  • QVariant::Image - a QImage
  • QVariant::Int - an int
  • QVariant::KeySequence - a QKeySequence
  • QVariant::List - a QValueList
  • QVariant::Map - a QMap
  • QVariant::Palette - a QPalette
  • QVariant::Pixmap - a QPixmap
  • QVariant::Point - a QPoint
  • QVariant::PointArray - a QPointArray
  • QVariant::Rect - a QRect
  • QVariant::Region - a QRegion
  • QVariant::Size - a QSize
  • QVariant::SizePolicy - a QSizePolicy
  • QVariant::String - a QString
  • QVariant::CString - a QCString
  • QVariant::StringList - a QStringList
  • QVariant::Time - a QTime
  • QVariant::UInt - an unsigned int

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


Member Function Documentation

QVariant::QVariant ()

Constructs an invalid variant.

QVariant::QVariant ( constQVariant&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.

QVariant::QVariant ( QDataStream&s )

Reads the variant from the data stream, s.

QVariant::QVariant ( constQString&val )

Constructs a new variant with a string value, val.

QVariant::QVariant ( constQCString&val )

Constructs a new variant with a C-string value, val.

If you want to modify the QCString after you've passed it to this constructor, we recommend passing a deep copy (see QCString::copy()).

QVariant::QVariant ( constchar*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.

QVariant::QVariant ( constQStringList&val )

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

QVariant::QVariant ( constQFont&val )

Constructs a new variant with a font value, val.

QVariant::QVariant ( constQPixmap&val )

Constructs a new variant with a pixmap value, val.

QVariant::QVariant ( constQImage&val )

Constructs a new variant with an image value, val.

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.

QVariant::QVariant ( constQBrush&val )

Constructs a new variant with a brush value, val.

QVariant::QVariant ( constQPoint&val )

Constructs a new variant with a point value, val.

QVariant::QVariant ( constQRect&val )

Constructs a new variant with a rect value, val.

QVariant::QVariant ( constQSize&val )

Constructs a new variant with a size value, val.

QVariant::QVariant ( constQColor&val )

Constructs a new variant with a color value, val.

QVariant::QVariant ( constQPalette&val )

Constructs a new variant with a color palette value, val.

QVariant::QVariant ( constQColorGroup&val )

Constructs a new variant with a color group value, val.

QVariant::QVariant ( constQIconSet&val )

Constructs a new variant with an icon set value, val.

QVariant::QVariant ( constQPointArray&val )

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

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.

QVariant::QVariant ( constQRegion&val )

Constructs a new variant with a region value, val.

QVariant::QVariant ( constQBitmap&val )

Constructs a new variant with a bitmap value, val.

QVariant::QVariant ( constQCursor&val )

Constructs a new variant with a cursor value, val.

QVariant::QVariant ( constQDate&val )

Constructs a new variant with a date value, val.

QVariant::QVariant ( constQTime&val )

Constructs a new variant with a time value, val.

QVariant::QVariant ( constQDateTime&val )

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

QVariant::QVariant ( constQByteArray&val )

Constructs a new variant with a bytearray value, val.

QVariant::QVariant ( constQBitArray&val )

Constructs a new variant with a bitarray value, val.

QVariant::QVariant ( constQKeySequence&val )

Constructs a new variant with a key sequence value, val.

QVariant::QVariant ( constQValueList<QVariant>&val )

Constructs a new variant with a list value, val.

QVariant::QVariant ( constQMap<QString,QVariant>&val )

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

QVariant::QVariant ( intval )

Constructs a new variant with an integer value, val.

QVariant::QVariant ( uintval )

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

QVariant::QVariant ( boolval, int )

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

QVariant::QVariant ( doubleval )

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

QVariant::QVariant ( QSizePolicyval )

Constructs a new variant with a size policy value, val.

QVariant::~QVariant ()

Destroys the QVariant 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, QVariant::clear() is called rather than a subclass's clear().

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;
    }
    

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().

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 ()

Returns the variant's value as unsigned int reference.

bool QVariant::canCast ( Typet ) 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:

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

bool QVariant::cast ( Typet )

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 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 ()

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

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

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

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

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

Returns the end iterator for the map, if the variant's type is appropriate; otherwise returns a null iterator.

QMapConstIterator<QString,QVariant> QVariant::mapFind ( constQString&key ) const

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 ( constchar*name ) [static]

Converts the string representation of the storage type gven 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!= ( constQVariant&v ) const

Compares this QVariant with v and returns TRUE if they are not equal; otherwise returns FALSE.

QVariant& QVariant::operator= ( constQVariant&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 QVariant::operator== ( constQVariant&v ) const

Compares this QVariant with v and returns TRUE if they are equal; otherwise returns FALSE.

QValueListConstIterator<QString> QVariant::stringListBegin () const

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

Returns the end iterator for the list if the variant's type is StringList; otherwise returns a null iterator.

constQBitArray QVariant::toBitArray () const

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

See also asBitArray().

constQBitmap 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 has type() Bool.

Returns TRUE if the variant has type Int, UInt or Double and its value is non-zero; otherwise returns FALSE.

See also asBool().

constQBrush 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().

constQByteArray QVariant::toByteArray () const

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

See also asByteArray().

constQCString QVariant::toCString () const

Returns the variant as a QCString if the variant has type() CString or String; otherwise returns 0.

See also asCString().

constQColor QVariant::toColor () const

Returns the variant as a QColor if the variant has type() Color; otherwise returns an invalid color.

See also asColor().

constQColorGroup 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().

constQCursor QVariant::toCursor () const

Returns the variant as a QCursor if the variant has type() Cursor; otherwise returns the default arrow cursor.

See also asCursor().

constQDate QVariant::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.

See also asDate().

constQDateTime QVariant::toDateTime () const

Returns the variant as a QDateTime if the variant has type() DateTime 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.

See also asDateTime().

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

Returns the variant as a double if the variant has type() String, CString, Double, Int, UInt, 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.

See also asDouble().

constQFont QVariant::toFont () const

Returns the variant as a QFont if the variant has type() Font; otherwise returns the application's default font.

See also asFont().

constQIconSet 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().

constQImage 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 has type() String, CString, 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 asInt() and canCast().

constQKeySequence QVariant::toKeySequence () const

Returns the variant as a QKeySequence if the variant has type() KeySequence, Int or String; otherwise returns an empty key sequence.

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().

constQValueList<QVariant> QVariant::toList () const

Returns the variant as a QValueList 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().

constQMap<QString,QVariant> QVariant::toMap () const

Returns the variant as a QMap 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().

constQPalette QVariant::toPalette () const

Returns the variant as a QPalette if the variant has type() Palette; otherwise returns a completely black palette.

See also asPalette().

constQPixmap QVariant::toPixmap () const

Returns the variant as a QPixmap if the variant has type() Pixmap; otherwise returns a null pixmap.

See also asPixmap().

constQPoint QVariant::toPoint () const

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

See also asPoint().

constQPointArray QVariant::toPointArray () const

Returns the variant as a QPointArray if the variant has type() PointArray; otherwise returns an empty QPointArray.

See also asPointArray().

constQRect QVariant::toRect () const

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

See also asRect().

constQRegion QVariant::toRegion () const

Returns the variant as a QRegion if the variant has type() Region; otherwise returns an empty QRegion.

See also asRegion().

constQSize 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

Returns the variant as a QSizePolicy if the variant has type() SizePolicy; otherwise returns an undefined (but legal) size policy.

constQString QVariant::toString () const

Returns the variant as a QString if the variant has type() String, CString, ByteArray, Int, Uint, Bool, Double, Date, Time, or DateTime; otherwise returns QString::null.

See also asString().

constQStringList QVariant::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;
    }
    

See also asStringList().

constQTime QVariant::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.

See also asTime().

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

Returns the variant as an unsigned int if the variant has type() String, CString, 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.

See also asUInt().

Type QVariant::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 * QVariant::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 "QValueList". An Invalid variant returns 0.

const char * QVariant::typeToName ( Typetyp ) [static]

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文