QVariant 類

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

頭: #include <QVariant>
繼承者: QDBusVariant

公共類型

enum Type { Invalid, BitArray, Bitmap, Bool, ..., UserType }

公共函數

QVariant ()
QVariant (const QLocale & l )
QVariant (const QRegExp & regExp )
QVariant (const QEasingCurve & val )
QVariant (Qt::GlobalColor color )
QVariant (Type type )
QVariant (int typeOrUserType , const void * copy )
QVariant (const QVariant & p )
QVariant (QDataStream & s )
QVariant (int val )
QVariant (uint val )
QVariant (qlonglong val )
QVariant (qulonglong val )
QVariant (bool val )
QVariant (double val )
QVariant (float val )
QVariant (const char * val )
QVariant (const QByteArray & val )
QVariant (const QBitArray & val )
QVariant (const QString & val )
QVariant (const QLatin1String & val )
QVariant (const QStringList & val )
QVariant (const QChar & c )
QVariant (const QDate & val )
QVariant (const QTime & val )
QVariant (const QDateTime & val )
QVariant (const QList<QVariant> & val )
QVariant (const QMap<QString, QVariant> & val )
QVariant (const QHash<QString, QVariant> & val )
QVariant (const QSize & val )
QVariant (const QSizeF & val )
QVariant (const QPoint & val )
QVariant (const QPointF & val )
QVariant (const QLine & val )
QVariant (const QLineF & val )
QVariant (const QRect & val )
QVariant (const QRectF & val )
QVariant (const QUrl & val )
~QVariant ()
bool canConvert (Type t ) const
bool canConvert () const
void clear ()
bool convert (Type t )
bool isNull () const
bool isValid () const
void setValue (const T & value )
void swap (QVariant & other )
QBitArray toBitArray () const
bool toBool () const
QByteArray toByteArray () const
QChar toChar () const
QDate toDate () const
QDateTime toDateTime () const
double toDouble (bool * ok = 0) const
QEasingCurve toEasingCurve () const
float toFloat (bool * ok = 0) const
QHash<QString, QVariant> toHash () const
int toInt (bool * ok = 0) const
QLine toLine () const
QLineF toLineF () const
QList<QVariant> toList () const
QLocale toLocale () const
qlonglong toLongLong (bool * ok = 0) const
QMap<QString, QVariant> toMap () const
QPoint toPoint () const
QPointF toPointF () const
qreal toReal (bool * ok = 0) const
QRect toRect () const
QRectF toRectF () const
QRegExp toRegExp () const
QSize toSize () const
QSizeF toSizeF () const
QString toString () const
QStringList toStringList () const
QTime toTime () const
uint toUInt (bool * ok = 0) const
qulonglong toULongLong (bool * ok = 0) const
QUrl toUrl () const
類型 type () const
const char * typeName () const
int userType () const
T value () const
bool operator!= (const QVariant & v ) const
QVariant & operator= (const QVariant & variant )
QVariant & operator= (QVariant && other )
bool operator== (const QVariant & v ) const

靜態公共成員

QVariant fromValue (const T & value )
類型 nameToType (const char * name )
const char * typeToName (Type typ )
typedef QVariantHash
typedef QVariantList
typedef QVariantMap
T qvariant_cast (const QVariant & value )
bool operator!= (const QVariant & v1 , const QVariant & v2 )
bool operator== (const QVariant & v1 , const QVariant & v2 )

詳細描述

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 () 及數據庫工作等。

A QVariant object holds a single value of a single type () 每次。(某些 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 convert (),使用某一 toT() 函數獲取其值 (如 toSize ()) and check whether the type can be converted to a particular type using canConvert ().

方法命名 toT() (如: toInt (), toString ()) 為 const。若要求存儲類型,它們返迴存儲對象的副本。若要求可以從存儲類型生成的類型,toT() 拷貝-轉換並保持對象本身不變。若要求無法從存儲類型生成的類型,結果從屬類型;見函數文檔編製瞭解細節。

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 QByteArray
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 = v.toInt() + 100;            // The variant now hold the value 223
v = QVariant(QStringList());
					

甚至可以存儲 QList < QVariant > 和 QMap < QString , QVariant > values 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.

QVariant also supports the notion of null values, where you can have a defined type with no value set. However, note that QVariant types can only be cast when they have had a value set.

QVariant x, y(QString()), z(QString(""));
x.convert(QVariant::Int);
// x.isNull() == true
// y.isNull() == true, z.isNull() == false
					

QVariant can be extended to support other types than those mentioned in the Type enum. See the QMetaType 文檔編製瞭解細節。

有關 GUI 類型的說明

因為 QVariant 屬於 QtCore library, it cannot provide conversion functions to data types defined in QtGui ,譬如 QColor , QImage ,和 QPixmap 。換句話說,沒有 toColor() 函數。取而代之,可以使用 QVariant::value () 或 qvariant_cast () 模闆函數。例如:

QVariant variant;
...
QColor color = variant.value<QColor>();
					

反嚮轉換 (如,從 QColor to QVariant ) 對於所有支持數據類型是自動通過 QVariant ,包括 GUI 相關類型:

QColor color = palette().background().color();
QVariant variant = color;
					

連續使用 canConvert() 和 convert()

當使用 canConvert () 和 convert () 連續,它是可能的對於 canConvert () 會返迴 true,但 convert () 會返迴 false。通常,這是因為 canConvert () only reports the general ability of QVariant to convert between types given suitable data; it is still possible to supply data which cannot actually be converted.

例如, canConvert () would return true when called on a variant containing a string because, in principle, QVariant is able to convert strings of numbers to integers. However, if the string contains non-numeric characters, it cannot be converted to an integer, and any attempt to convert it will fail. Hence, it is important to have both functions return true for a successful conversion.

另請參閱 QMetaType .

成員類型文檔編製

enum QVariant:: Type

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

常量 描述
QVariant::Invalid 0 no type
QVariant::BitArray 13 a QBitArray
QVariant::Bitmap 73 a QBitmap
QVariant::Bool 1 a bool
QVariant::Brush 66 a QBrush
QVariant::ByteArray 12 a QByteArray
QVariant::Char 7 a QChar
QVariant::Color 67 a QColor
QVariant::Cursor 74 a QCursor
QVariant::Date 14 a QDate
QVariant::DateTime 16 a QDateTime
QVariant::Double 6 a double
QVariant::EasingCurve 29 a QEasingCurve
QVariant::Font 64 a QFont
QVariant::Hash 28 a QVariantHash
QVariant::Icon 69 a QIcon
QVariant::Image 70 a QImage
QVariant::Int 2 an int
QVariant::KeySequence 76 a QKeySequence
QVariant::Line 23 a QLine
QVariant::LineF 24 a QLineF
QVariant::List 9 a QVariantList
QVariant::Locale 18 a QLocale
QVariant::LongLong 4 a qlonglong
QVariant::Map 8 a QVariantMap
QVariant::Matrix 80 a QMatrix
QVariant::Transform 81 a QTransform
QVariant::Matrix4x4 82 a QMatrix4x4
QVariant::Palette 68 a QPalette
QVariant::Pen 77 a QPen
QVariant::Pixmap 65 a QPixmap
QVariant::Point 25 a QPoint
QVariant::PointArray Polygon a QPointArray
QVariant::PointF 26 a QPointF
QVariant::Polygon 71 a QPolygon
QVariant::Quaternion 86 a QQuaternion
QVariant::Rect 19 a QRect
QVariant::RectF 20 a QRectF
QVariant::RegExp 27 a QRegExp
QVariant::Region 72 a QRegion
QVariant::Size 21 a QSize
QVariant::SizeF 22 a QSizeF
QVariant::SizePolicy 75 a QSizePolicy
QVariant::String 10 a QString
QVariant::StringList 11 a QStringList
QVariant::TextFormat 79 a QTextFormat
QVariant::TextLength 78 a QTextLength
QVariant::Time 15 a QTime
QVariant::UInt 3 a uint
QVariant::ULongLong 5 a qulonglong
QVariant::Url 17 a QUrl
QVariant::Vector2D 83 a QVector2D
QVariant::Vector3D 84 a QVector3D
QVariant::Vector4D 85 a QVector4D
QVariant::UserType 127 Base value for user-defined types.

成員函數文檔編製

QVariant:: QVariant ()

構造無效變體。

QVariant:: QVariant (const QLocale & l )

構造新變體采用區域設置值 l .

QVariant:: QVariant (const QRegExp & regExp )

構造新變體采用正則錶達式值 regExp .

QVariant:: QVariant (const QEasingCurve & val )

構造新變體采用緩和麯綫值 val .

該函數在 Qt 4.7 引入。

QVariant:: QVariant ( Qt::GlobalColor color )

Constructs a new variant of type QVariant::Color and initializes it with color .

This is a convenience constructor that allows QVariant(Qt::blue); to create a valid QVariant storing a QColor .

Note: This constructor will assert if the application does not link to the Qt GUI library.

該函數在 Qt 4.2 引入。

QVariant:: QVariant ( Type type )

Constructs a null variant of type type .

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

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

Note that you have to pass the address of the variable you want stored.

Usually, you never have to use this constructor, use QVariant::fromValue () instead to construct variants from the pointer types represented by QMetaType::VoidStar , QMetaType::QObjectStar and QMetaType::QWidgetStar .

另請參閱 QVariant::fromValue () 和 Type .

QVariant:: QVariant (const QVariant & p )

Constructs a copy of the variant, p , passed as the argument to this constructor.

QVariant:: QVariant ( QDataStream & s )

Reads the variant from the data stream, s .

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

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

QVariant:: QVariant ( qulonglong val )

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

QVariant:: QVariant ( bool val )

Constructs a new variant with a boolean value, val .

QVariant:: QVariant ( double val )

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

QVariant:: QVariant ( float val )

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

該函數在 Qt 4.6 引入。

QVariant:: QVariant (const char * val )

Constructs a new variant with a string value of val . The variant creates a deep copy of val , using the encoding set by QTextCodec::setCodecForCStrings ().

注意, val is converted to a QString for storing in the variant and QVariant::type () 會返迴 QMetaType::QString for the variant.

可以禁用此運算符通過定義 QT_NO_CAST_FROM_ASCII 當編譯應用程序時。

另請參閱 QTextCodec::setCodecForCStrings ().

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 QString & val )

Constructs a new variant with a string value, val .

QVariant:: QVariant (const QLatin1String & val )

Constructs a new variant with a string value, val .

QVariant:: QVariant (const QStringList & val )

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

QVariant:: QVariant (const QChar & c )

Constructs a new variant with a char value, c .

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 QList < 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 (const QHash < QString , QVariant > & val )

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

QVariant:: QVariant (const QSize & val )

Constructs a new variant with a size value of val .

QVariant:: QVariant (const QSizeF & val )

Constructs a new variant with a size value of val .

QVariant:: QVariant (const QPoint & val )

Constructs a new variant with a point value of val .

QVariant:: QVariant (const QPointF & val )

Constructs a new variant with a point value of val .

QVariant:: QVariant (const QLine & val )

Constructs a new variant with a line value of val .

QVariant:: QVariant (const QLineF & val )

Constructs a new variant with a line value of val .

QVariant:: QVariant (const QRect & val )

Constructs a new variant with a rect value of val .

QVariant:: QVariant (const QRectF & val )

Constructs a new variant with a rect value of val .

QVariant:: QVariant (const QUrl & val )

構造新變體采用 URL 值 val .

QVariant:: ~QVariant ()

銷毀 QVariant 和包含對象。

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

bool QVariant:: canConvert ( Type t ) const

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

下列鑄造是自動完成的:

類型 自動鑄造成
Bool Char , Double , Int , LongLong , String , UInt , ULongLong
ByteArray Double , Int , LongLong , String , UInt , ULongLong
Char Bool , Int , UInt , LongLong , ULongLong
顔色 String
Date DateTime , String
DateTime Date , String , 時間
Double Bool , Int , LongLong , String , UInt , ULongLong
Font String
Int Bool , Char , Double , LongLong , String , UInt , ULongLong
KeySequence Int , String
List StringList (if the list's items can be converted to strings)
LongLong Bool , ByteArray , Char , Double , Int , String , UInt , ULongLong
PointF
Rect RectF
String Bool , ByteArray , Char , 顔色 , Date , DateTime , Double , Font , Int , KeySequence , LongLong , StringList , 時間 , UInt , ULongLong
StringList List , String (if the list contains exactly one item)
時間 String
UInt Bool , Char , Double , Int , LongLong , String , ULongLong
ULongLong Bool , Char , Double , Int , LongLong , String , UInt

另請參閱 convert ().

bool QVariant:: canConvert () const

Returns true if the variant can be converted to the template type T ,否則 false。

範例:

QVariant v = 42;
v.canConvert<int>();              // returns true
v.canConvert<QString>();          // returns true
MyCustomStruct s;
v.setValue(s);
v.canConvert<int>();              // returns false
v.canConvert<MyCustomStruct>();   // returns true
					

另請參閱 convert ().

void QVariant:: clear ()

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

bool QVariant:: convert ( Type t )

將變體鑄造成請求類型, t . If the cast cannot be done, the variant is cleared. Returns true if the current type of the variant was successfully cast; otherwise returns false.

警告: For historical reasons, converting a null QVariant results in a null value of the desired type (e.g., an empty string for QString ) and a result of false.

另請參閱 canConvert () 和 clear ().

[static] QVariant QVariant:: fromValue (const T & value )

返迴 QVariant containing a copy of value . Behaves exactly like setValue () otherwise.

範例:

MyCustomStruct s;
return QVariant::fromValue(s);
					

注意: If you are working with custom types, you should use the Q_DECLARE_METATYPE () macro to register your custom type.

另請參閱 setValue () 和 value ().

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 ;否則返迴 false。

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

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 .

void QVariant:: setValue (const T & value )

Stores a copy of value 。若 T is a type that QVariant 不支持, QMetaType is used to store the value. A compile error will occur if QMetaType doesn't handle the type.

範例:

QVariant v;
v.setValue(5);
int i = v.toInt();         // i is now 5
QString s = v.toString()   // s is now "5"
MyCustomStruct c;
v.setValue(c);
...
MyCustomStruct c2 = v.value<MyCustomStruct>();
					

另請參閱 value (), fromValue (),和 canConvert ().

void QVariant:: swap ( QVariant & other )

交換變體 other 與此變體。此操作非常快且從不失敗。

該函數在 Qt 4.8 引入。

QBitArray QVariant:: toBitArray () const

返迴變體如 QBitArray 若變體擁有 type () BitArray ;否則返迴空的位數組。

另請參閱 canConvert () 和 convert ().

bool QVariant:: toBool () const

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

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

另請參閱 canConvert () 和 convert ().

QByteArray QVariant:: toByteArray () const

返迴變體如 QByteArray 若變體擁有 type () ByteArray or String (轉換使用 QString::fromAscii ());否則返迴空字節數組。

另請參閱 canConvert () 和 convert ().

QChar QVariant:: toChar () const

返迴變體如 QChar 若變體擁有 type () Char , Int ,或 UInt ;否則返迴無效 QChar .

另請參閱 canConvert () 和 convert ().

QDate QVariant:: toDate () const

返迴變體如 QDate 若變體擁有 type () Date , DateTime ,或 String ; otherwise returns an invalid date.

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

另請參閱 canConvert () 和 convert ().

QDateTime QVariant:: toDateTime () const

返迴變體如 QDateTime 若變體擁有 type () DateTime , Date ,或 String ;否則返迴無效日期/時間。

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

另請參閱 canConvert () 和 convert ().

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

Returns the variant as a double if the variant has type () Double , QMetaType::Float , Bool , ByteArray , Int , LongLong , String , UInt ,或 ULongLong ;否則返迴 0.0。

ok 非 null: * ok is set to true if the value could be converted to a double; otherwise * ok 被設為 false。

另請參閱 canConvert () 和 convert ().

QEasingCurve QVariant:: toEasingCurve () const

返迴變體如 QEasingCurve 若變體擁有 type () EasingCurve ; otherwise returns a default easing curve.

該函數在 Qt 4.7 引入。

另請參閱 canConvert () 和 convert ().

float QVariant:: toFloat ( bool * ok = 0) const

Returns the variant as a float if the variant has type () Double , QMetaType::Float , Bool , ByteArray , Int , LongLong , String , UInt ,或 ULongLong ;否則返迴 0.0。

ok 非 null: * ok is set to true if the value could be converted to a double; otherwise * ok 被設為 false。

該函數在 Qt 4.6 引入。

另請參閱 canConvert () 和 convert ().

QHash < QString , QVariant > QVariant:: toHash () const

返迴變體如 QHash < QString , QVariant > if the variant has type () Hash ; otherwise returns an empty map.

另請參閱 canConvert () 和 convert ().

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

Returns the variant as an int if the variant has type () Int , Bool , ByteArray , Char , Double , LongLong , String , UInt ,或 ULongLong ;否則返迴 0。

ok 非 null: * ok is set to true if the value could be converted to an int; otherwise * ok 被設為 false。

警告: If the value is convertible to a LongLong but is too large to be represented in an int, the resulting arithmetic overflow will not be reflected in ok . A simple workaround is to use QString::toInt (). Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.

另請參閱 canConvert () 和 convert ().

QLine QVariant:: toLine () const

返迴變體如 QLine 若變體擁有 type () Line ;否則返迴無效 QLine .

另請參閱 canConvert () 和 convert ().

QLineF QVariant:: toLineF () const

返迴變體如 QLineF 若變體擁有 type () LineF ;否則返迴無效 QLineF .

另請參閱 canConvert () 和 convert ().

QList < QVariant > QVariant:: toList () const

返迴變體如 QVariantList 若變體擁有 type () List or StringList ; otherwise returns an empty list.

另請參閱 canConvert () 和 convert ().

QLocale QVariant:: toLocale () const

返迴變體如 QLocale 若變體擁有 type () Locale ;否則返迴無效 QLocale .

另請參閱 canConvert () 和 convert ().

qlonglong QVariant:: toLongLong ( bool * ok = 0) const

Returns the variant as a long long int if the variant has type () LongLong , Bool , ByteArray , Char , Double , Int , String , UInt ,或 ULongLong ;否則返迴 0。

ok 非 null: * ok is set to true if the value could be converted to an int; otherwise * ok 被設為 false。

另請參閱 canConvert () 和 convert ().

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

返迴變體如 QMap < QString , QVariant > if the variant has type () 地圖 ; otherwise returns an empty map.

另請參閱 canConvert () 和 convert ().

QPoint QVariant:: toPoint () const

返迴變體如 QPoint 若變體擁有 type () or PointF ; otherwise returns a null QPoint .

另請參閱 canConvert () 和 convert ().

QPointF QVariant:: toPointF () const

返迴變體如 QPointF 若變體擁有 type () or PointF ; otherwise returns a null QPointF .

另請參閱 canConvert () 和 convert ().

qreal QVariant:: toReal ( bool * ok = 0) const

Returns the variant as a qreal if the variant has type () Double , QMetaType::Float , Bool , ByteArray , Int , LongLong , String , UInt ,或 ULongLong ;否則返迴 0.0。

ok 非 null: * ok is set to true if the value could be converted to a double; otherwise * ok 被設為 false。

該函數在 Qt 4.6 引入。

另請參閱 canConvert () 和 convert ().

QRect QVariant:: toRect () const

返迴變體如 QRect 若變體擁有 type () Rect ;否則返迴無效 QRect .

另請參閱 canConvert () 和 convert ().

QRectF QVariant:: toRectF () const

返迴變體如 QRectF 若變體擁有 type () Rect or RectF ;否則返迴無效 QRectF .

另請參閱 canConvert () 和 convert ().

QRegExp QVariant:: toRegExp () const

返迴變體如 QRegExp 若變體擁有 type () RegExp ;否則返迴空 QRegExp .

該函數在 Qt 4.1 引入。

另請參閱 canConvert () 和 convert ().

QSize QVariant:: toSize () const

返迴變體如 QSize 若變體擁有 type () Size ;否則返迴無效 QSize .

另請參閱 canConvert () 和 convert ().

QSizeF QVariant:: toSizeF () const

返迴變體如 QSizeF 若變體擁有 type () SizeF ;否則返迴無效 QSizeF .

另請參閱 canConvert () 和 convert ().

QString QVariant:: toString () const

返迴變體如 QString 若變體擁有 type () String , Bool , ByteArray , Char , Date , DateTime , Double , Int , LongLong , StringList , 時間 , UInt ,或 ULongLong ; otherwise returns an empty string.

另請參閱 canConvert () 和 convert ().

QStringList QVariant:: toStringList () const

返迴變體如 QStringList 若變體擁有 type () StringList , String ,或 List of a type that can be converted to QString ; otherwise returns an empty list.

另請參閱 canConvert () 和 convert ().

QTime QVariant:: toTime () const

返迴變體如 QTime 若變體擁有 type () 時間 , DateTime ,或 String ; otherwise returns an invalid time.

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

另請參閱 canConvert () 和 convert ().

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

Returns the variant as an unsigned int if the variant has type () UInt , Bool , ByteArray , Char , Double , Int , LongLong , String ,或 ULongLong ;否則返迴 0。

ok 非 null: * ok is set to true if the value could be converted to an unsigned int; otherwise * ok 被設為 false。

警告: If the value is convertible to a ULongLong but is too large to be represented in an unsigned int, the resulting arithmetic overflow will not be reflected in ok . A simple workaround is to use QString::toUInt (). Fixing this bug has been postponed to Qt 5 in order to avoid breaking existing code.

另請參閱 canConvert () 和 convert ().

qulonglong QVariant:: toULongLong ( bool * ok = 0) const

Returns the variant as as an unsigned long long int if the variant has type () ULongLong , Bool , ByteArray , Char , Double , Int , LongLong , String ,或 UInt ;否則返迴 0。

ok 非 null: * ok is set to true if the value could be converted to an int; otherwise * ok 被設為 false。

另請參閱 canConvert () 和 convert ().

QUrl QVariant:: toUrl () const

返迴變體如 QUrl 若變體擁有 type () Url ;否則返迴無效 QUrl .

另請參閱 canConvert () 和 convert ().

Type QVariant:: type () const

Returns the storage type of the value stored in the variant. Although this function is declared as returning QVariant::Type , the return value should be interpreted as QMetaType::Type . In particular, QVariant::UserType is returned here only if the value is equal or greater than QMetaType::User .

Note that return values in the ranges QVariant::Char 透過 QVariant::RegExp and QVariant::Font 透過 QVariant::Transform correspond to the values in the ranges QMetaType::QChar 透過 QMetaType::QRegExp and QMetaType::QFont 透過 QMetaType::QQuaternion .

Pay particular attention when working with char and QChar variants. Note that there is no QVariant constructor specifically for type char, but there is one for QChar . For a variant of type QChar ,此函數返迴 QVariant::Char , which is the same as QMetaType::QChar , but for a variant of type char ,此函數返迴 QMetaType::Char ,其是 not the same as QVariant::Char .

Also note that the types void* , long , short , unsigned long , unsigned short , unsigned char , float , QObject* ,和 QWidget* are represented in QMetaType::Type but not in QVariant::Type , and they can be returned by this function. However, they are considered to be user defined types when tested against QVariant::Type .

To test whether an instance of QVariant contains a data type that is compatible with the data type you are interested in, use canConvert ().

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 " QVariantList ". An Invalid variant returns 0.

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

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

Returns a null pointer if the type is QVariant::Invalid or doesn't exist.

int QVariant:: userType () const

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

另請參閱 type ().

T QVariant:: value () const

返迴的存儲值被轉換為模闆類型 T 。調用 canConvert () to find out whether a type can be converted. If the value cannot be converted, default-constructed value will be returned.

若類型 T 支持通過 QVariant ,此函數的行為準確如 toString (), toInt () 等。

範例:

QVariant v;
MyCustomStruct c;
if (v.canConvert<MyCustomStruct>())
    c = v.value<MyCustomStruct>();
v = 7;
int i = v.value<int>();                        // same as v.toInt()
QString s = v.value<QString>();                // same as v.toString(), s is now "7"
MyCustomStruct c2 = v.value<MyCustomStruct>(); // conversion failed, c2 is empty
					

另請參閱 setValue (), fromValue (),和 canConvert ().

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

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

警告: This function doesn't support custom types registered with qRegisterMetaType ().

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

Assigns the value of the variant variant to this variant.

QVariant & QVariant:: operator= ( QVariant && other )

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

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

In the case of custom types, their equalness operators are not called. Instead the values' addresses are compared.

相關非成員

typedef QVariantHash

同義詞 QHash < QString , QVariant >.

該 typedef 在 Qt 5.4 引入。

typedef QVariantList

同義詞 QList < QVariant >.

typedef QVariantMap

同義詞 QMap < QString , QVariant >.

T qvariant_cast (const QVariant & value )

返迴給定 value 被轉換成模闆類型 T .

此函數相當於 QVariant::value ().

另請參閱 QVariant::value ().

bool operator!= (const QVariant & v1 , const QVariant & v2 )

Returns false if v1 and v2 are equal; otherwise returns true.

警告: This function doesn't support custom types registered with qRegisterMetaType ().

bool operator== (const QVariant & v1 , const QVariant & v2 )

返迴 true 若 v1 and v2 相等;否則返迴 false。

警告: This function doesn't support custom types registered with qRegisterMetaType ().