QVariant Class Reference
[QtCore module]
该的QVariant类的行为像最常见的Qt数据类型的联合。More…
Types
enum Type { Invalid, Bool, Int, UInt, ..., UserType }
Methods
__init__ (self)
__init__ (self, Type type)
__init__ (self, int typeOrUserType, sip.voidptr copy)
__init__ (self, QVariant other)
__init__ (self, object)
bool canConvert (self, Type t)
clear (self)
bool convert (self, Type t)
sip.voidptr data (self)
detach (self)
bool isDetached (self)
bool isNull (self)
bool isValid (self)
load (self, QDataStream ds)
save (self, QDataStream ds)
swap (self, QVariant other)
QBitArray toBitArray (self)
bool toBool (self)
QByteArray toByteArray (self)
QChar toChar (self)
QDate toDate (self)
QDateTime toDateTime (self)
(float, bool ok) toDouble (self)
QEasingCurve toEasingCurve (self)
(float, bool ok) toFloat (self)
dict-of-QString-QVariant toHash (self)
(int, bool ok) toInt (self)
QLine toLine (self)
QLineF toLineF (self)
list-of-QVariant toList (self)
QLocale toLocale (self)
(int, bool ok) toLongLong (self)
dict-of-QString-QVariant toMap (self)
QPoint toPoint (self)
QPointF toPointF (self)
object toPyObject (self)
(float, bool ok) toReal (self)
QRect toRect (self)
QRectF toRectF (self)
QRegExp toRegExp (self)
QSize toSize (self)
QSizeF toSizeF (self)
QString toString (self)
QStringList toStringList (self)
QTime toTime (self)
(int, bool ok) toUInt (self)
(int, bool ok) toULongLong (self)
QUrl toUrl (self)
Type type (self)
str typeName (self)
int userType (self)
Static Methods
QVariant fromList (list-of-QVariant list)
QVariant fromMap (dict-of-QString-QVariant map)
Type nameToType (str name)
str typeToName (Type type)
Special Methods
bool __eq__ (self, QVariant v)
bool __ne__ (self, QVariant v)
Detailed Description
可以使用任何Python对象每当一个QVariant预计。没有人会被解释为无效的QVariant 。
该的QVariant类的行为像最常见的Qt数据类型的联合。
因为C + +不允许从包括具有非默认的构造函数或析构函数类型的工会,最有趣的Qt类不能在工会使用。没有的QVariant ,这将是一个问题QObject.property( )和数据库的工作,等等。
一个的QVariant对象持有一个单一的单个值type()的时间。 (有些type( ) S是多值,例如一个字符串列表。 )你可以找出哪些类型,T变种持有,使用它转换为不同的类型convert( ) ,取得使用TOT之一,它的值( )函数(例如,toSize() ),并检查是否该类型可以使用被转换成特定类型的canConvert( ) 。
名为TOT ()的方法(例如,toInt( )toString( ) )是常量。如果你问的存储类型,它们返回的存储对象的一个副本。如果要求,可以从所存储的类型, TOT ( )的拷贝和转换来生成和叶对象本身不变型。如果你问的是不能从存储的类型生成的类型,其结果取决于类型,详见函数文档。
下面是一些示例代码来演示使用的QVariant的:
[QDataStream](qdatastream.html) 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]($docs-qstring.html) s = v.toString(); // s = tr("hello") (see QObject.tr())
out << v; // Writes a type tag and a QString to out
...
[QDataStream](qdatastream.html) 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]($docs-qstringlist.html)());
你甚至可以存储QList\u003cQVariant\u003e和QMap\u003cQString在一个变体的QVariant \u003e的值,这样你就可以很容易地构造任意类型的任意复杂的数据结构。这是非常强大和灵活,但可能证明较少的内存和速度效率比存储特定类型的标准数据结构。
的QVariant还支持空值,在那里你可以有一个定义的类型,没有值集的概念。但是请注意,当他们有一个值集的QVariant类型只能被施放。
QVariant x, y([QString]($docs-qstring.html)()), z([QString]($docs-qstring.html)(""));
x.convert(QVariant.Int);
// x.isNull() == true
// y.isNull() == true, z.isNull() == false
的QVariant可以扩展到支持其他类型比在提及Type枚举。请参阅QMetaType文档。
A Note on GUI Types
因为是的QVariant的部分QtCore库,它不能提供转换功能,以定义数据类型QtGui如QColor,QImage和QPixmap。换句话说,没有toColor()
功能。相反,你可以使用QVariant.value()或qvariant_cast( )模板函数。例如:
QVariant variant;
...
[QColor]($docs-qcolor.html) color = variant.value<[QColor]($docs-qcolor.html)>();
逆变换(例如,从QColor到的QVariant )是自动通过的QVariant支持的所有数据类型,包括GUI相关的类:
[QColor]($docs-qcolor.html) color = palette().background().color();
QVariant variant = color;
Using canConvert() and convert() Consecutively
当使用canConvert()和convert()连续,它有可能为canConvert( )返回True ,但convert( )返回False 。这通常是因为canConvert()只报告的QVariant来给出合适的数据类型之间进行转换的一般能力,它仍然是可能的,以提供其实际上不能被转换的数据。
例如,canConvert( )会叫上一个包含字符串变量的时候,因为在原则上,是的QVariant能够数字字符串转换为整数返回True 。然而,如果字符串中包含非数字字符时,它不能被转换成一个整数,并且任何试图将其转换将失败。因此,有两个函数返回True,对于一个成功的转换是非常重要的。
Type Documentation
QVariant.Type
这个枚举类型定义变量的类型,一个QVariant可以包含。
Constant | Value | Description |
---|---|---|
QVariant.Invalid |
0 |
没有类型 |
QVariant.BitArray |
13 |
一QBitArray |
QVariant.Bitmap |
73 |
一QBitmap |
QVariant.Bool |
1 |
一个bool |
QVariant.Brush |
66 |
一QBrush |
QVariant.ByteArray |
12 |
一QByteArray |
QVariant.Char |
7 |
一QChar |
QVariant.Color |
67 |
一QColor |
QVariant.Cursor |
74 |
一QCursor |
QVariant.Date |
14 |
一QDate |
QVariant.DateTime |
16 |
一QDateTime |
QVariant.Double |
6 |
双 |
QVariant.EasingCurve |
29 |
一QEasingCurve |
QVariant.Font |
64 |
一QFont |
QVariant.Hash |
28 |
一QVariantHash |
QVariant.Icon |
69 |
一QIcon |
QVariant.Image |
70 |
一QImage |
QVariant.Int |
2 |
一个int |
QVariant.KeySequence |
76 |
一QKeySequence |
QVariant.Line |
23 |
一QLine |
QVariant.LineF |
24 |
一QLineF |
QVariant.List |
9 |
一QVariantList |
QVariant.Locale |
18 |
一QLocale |
QVariant.LongLong |
4 |
一qlonglong |
QVariant.Map |
8 |
一QVariantMap |
QVariant.Matrix |
80 |
一QMatrix(obsolete) |
QVariant.Transform |
81 |
一QTransform |
QVariant.Matrix4x4 |
82 |
一QMatrix4x4 |
QVariant.Palette |
68 |
一QPalette |
QVariant.Pen |
77 |
一QPen |
QVariant.Pixmap |
65 |
一QPixmap |
QVariant.Point |
25 |
一QPoint |
QVariant.PointArray |
Polygon |
一QPointArray |
QVariant.PointF |
26 |
一QPointF |
QVariant.Polygon |
71 |
一QPolygon |
QVariant.Quaternion |
86 |
一QQuaternion |
QVariant.Rect |
19 |
一QRect |
QVariant.RectF |
20 |
一QRectF |
QVariant.RegExp |
27 |
一QRegExp |
QVariant.Region |
72 |
一QRegion |
QVariant.Size |
21 |
一QSize |
QVariant.SizeF |
22 |
一QSizeF |
QVariant.SizePolicy |
75 |
一QSizePolicy |
QVariant.String |
10 |
一QString |
QVariant.StringList |
11 |
一QStringList |
QVariant.TextFormat |
79 |
一QTextFormat |
QVariant.TextLength |
78 |
一QTextLength |
QVariant.Time |
15 |
一QTime |
QVariant.UInt |
3 |
一uint |
QVariant.ULongLong |
5 |
一qulonglong |
QVariant.Url |
17 |
一QUrl |
QVariant.Vector2D |
83 |
一QVector2D |
QVariant.Vector3D |
84 |
一QVector3D |
QVariant.Vector4D |
85 |
一QVector4D |
QVariant.UserType |
127 |
对于用户自定义类型的基础值。 |
Method Documentation
QVariant.__init__ (self)
构造一个无效的变体。
QVariant.__init__ (self, Type type)
构造类型的空变种type。
QVariant.__init__ (self, int typeOrUserType, sip.voidptr copy)
构造类型的变种typeOrUserType以及与初始化copy如果copy不为0。
请注意,你必须通过你想要保存的变量的地址。
通常情况下,你从来没有使用此构造函数,使用QVariant.fromValue( )而不是由所代表的指针类型构造变形QMetaType.VoidStar
,QMetaType.QObjectStar
和QMetaType.QWidgetStar
。
See also QVariant.fromValue()和Type。
QVariant.__init__ (self, QVariant other)
QVariant.__init__ (self, object)
构造变形的副本,p,作为参数传递给此构造函数。
bool QVariant.canConvert (self, Type t)
返回True如果变量的类型可以转换为所请求的类型,t。当调用此类铸件是自动完成的toInt( )toBool( ),…的方法。
下面的类型转换是自动完成的:
Type | Automatically Cast To |
---|---|
Bool | Char, Double, Int, LongLong, String, UInt, ULongLong |
ByteArray | Double, Int, LongLong, String, UInt, ULongLong |
Char | Bool, Int, UInt, LongLong, ULongLong |
Color | String |
Date | DateTime, String |
DateTime | Date, String, Time |
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 |
Point | PointF |
Rect | RectF |
String | Bool, ByteArray, Char, Color, Date, DateTime, Double, Font, Int, KeySequence, LongLong, StringList, Time, UInt, ULongLong |
StringList | List, String (if the list contains exactly one item) |
Time | String |
UInt | Bool, Char, Double, Int, LongLong, String, ULongLong |
ULongLong | Bool, Char, Double, Int, LongLong, String, UInt |
See also convert( ) 。
QVariant.clear (self)
转换这个变种输入无效,并释放使用的资源。
bool QVariant.convert (self, Type t)
注塑变种所请求的类型,t。如果转换无法完成,该变种被清除。返回True如果变量的当前类型已成功投,否则返回False 。
Warning:由于历史的原因,将空QVariant结果在所希望的类型(例如,一个空字符串的空值QString)和虚假的结果。
See also canConvert()和clear( ) 。
sip.voidptr QVariant.data (self)
QVariant.detach (self)
QVariant QVariant.fromList (list-of-QVariant list)
QVariant QVariant.fromMap (dict-of-QString-QVariant map)
bool QVariant.isDetached (self)
bool QVariant.isNull (self)
返回True如果这是一个空变量,否则返回False。
bool QVariant.isValid (self)
返回True如果这个变种的存储类型是不QVariant.Invalid否则返回False 。
QVariant.load (self, QDataStream ds)
Type QVariant.nameToType (str name)
[
在给定的存储类型的字符串表示形式转换name,其枚举表示。
如果字符串表示不能转换为任何枚举表示,变量被设置为Invalid
。
QVariant.save (self, QDataStream ds)
QVariant.swap (self, QVariant other)
掉期变种other这种变体。这个操作是非常快的,而且永远不会。
此功能被引入Qt的4.8 。
]($docs-index.htm#Type-enum)
QBitArray QVariant.toBitArray (self)
返回变量作为QBitArray如果该变体具有type( )BitArray否则返回一个空的位阵列。
See also canConvert()和convert( ) 。
bool QVariant.toBool (self)
返回变量为bool如果有变异type( )布尔。
如果变量有,则返回Truetype( )Bool,Char,Double,Int,LongLong,UInt或ULongLong和的值是非零,或者如果所述变体的类型为String or ByteArray和其小写内容不为空, “0”或“假” ,否则返回假。
See also canConvert()和convert( ) 。
QByteArray QVariant.toByteArray (self)
返回变量作为QByteArray如果该变体具有type( )ByteArray or String使用(兑换QString.fromAscii( ) ) ,否则返回一个空字节数组。
See also canConvert()和convert( ) 。
QChar QVariant.toChar (self)
返回变量作为QChar如果该变体具有type( )Char,Int或UInt否则返回一个无效的QChar。
See also canConvert()和convert( ) 。
QDate QVariant.toDate (self)
返回变量作为QDate如果该变体具有type( )Date,DateTime或String否则返回一个无效的日期。
如果type()是String,一个无效的日期,如果该字符串不能解析为一个将被退回Qt.ISODate格式的日期。
See also canConvert()和convert( ) 。
QDateTime QVariant.toDateTime (self)
返回变量作为QDateTime如果该变体具有type( )DateTime,Date或String否则返回一个无效的日期/时间。
如果type()是String,一个无效的日期/时间,如果该字符串不能解析为一个将被退回Qt.ISODate格式的日期/时间。
See also canConvert()和convert( ) 。
(float, bool ok) QVariant.toDouble (self)
返回变体作为双如果所述变体具有type( )Double,QMetaType.Float,Bool,ByteArray,Int,LongLong,String,UInt或ULongLong否则返回0.0 。
If ok不为null :*
ok被设置为True,如果该值可以转换为double ,否则*
ok设置为False 。
See also canConvert()和convert( ) 。
QEasingCurve QVariant.toEasingCurve (self)
返回变量作为QEasingCurve如果该变体具有type( )EasingCurve否则返回一个默认的缓动曲线。
此功能被引入Qt的4.7 。
See also canConvert()和convert( ) 。
(float, bool ok) QVariant.toFloat (self)
返回变量为float ,如果该变体具有type( )Double,QMetaType.Float,Bool,ByteArray,Int,LongLong,String,UInt或ULongLong否则返回0.0 。
If ok不为null :*
ok被设置为True,如果该值可以转换为double ,否则*
ok设置为False 。
此功能被引入Qt的4.6 。
See also canConvert()和convert( ) 。
dict-of-QString-QVariant QVariant.toHash (self)
返回变量作为QHash\u003cQString,QVariant\u003e如果所述变体具有type( )Hash否则返回一个空映射。
See also canConvert()和convert( ) 。
(int, bool ok) QVariant.toInt (self)
返回变量为int如果该变体具有type( )Int,Bool,ByteArray,Char,Double,LongLong,String,UInt或ULongLong否则返回0 。
If ok不为null :*
ok被设置为True,如果该值可以转换为int ,否则*
ok设置为False 。
Warning:如果该值可以转换为一个LongLong但过大的一个int来表示,由此产生的算术溢出将不会反映在ok。一个简单的解决方法是使用QString.toInt( ) 。修复这个bug已经被推迟到5的Qt为了避免破坏现有的代码。
See also canConvert()和convert( ) 。
QLine QVariant.toLine (self)
返回变量作为QLine如果该变体具有type( )Line否则返回一个无效的QLine。
See also canConvert()和convert( ) 。
QLineF QVariant.toLineF (self)
返回变量作为QLineF如果该变体具有type( )LineF否则返回一个无效的QLineF。
See also canConvert()和convert( ) 。
list-of-QVariant QVariant.toList (self)
返回变量作为QVariantList如果该变体具有type( )List or StringList否则返回空列表。
See also canConvert()和convert( ) 。
QLocale QVariant.toLocale (self)
返回变量作为QLocale如果该变体具有type( )Locale否则返回一个无效的QLocale。
See also canConvert()和convert( ) 。
(int, bool ok) QVariant.toLongLong (self)
返回变量为long long int的,如果有变异type( )LongLong,Bool,ByteArray,Char,Double,Int,String,UInt或ULongLong否则返回0 。
If ok不为null :*``ok
被设置为True,如果该值可以转换为int ,否则*``ok
设置为False 。
See also canConvert()和convert( ) 。
dict-of-QString-QVariant QVariant.toMap (self)
返回变量作为QMap\u003cQString,QVariant\u003e如果所述变体具有type( )Map否则返回一个空映射。
See also canConvert()和convert( ) 。
QPoint QVariant.toPoint (self)
返回变量作为QPoint如果该变体具有type( )Point or PointF否则返回空QPoint。
See also canConvert()和convert( ) 。
QPointF QVariant.toPointF (self)
返回变量作为QPointF如果该变体具有type( )Point or PointF否则返回空QPointF。
See also canConvert()和convert( ) 。
object QVariant.toPyObject (self)
(float, bool ok) QVariant.toReal (self)
返回变体作为QREAL如果所述变体具有type( )Double,QMetaType.Float,Bool,ByteArray,Int,LongLong,String,UInt或ULongLong否则返回0.0 。
If ok不为null :*
ok被设置为True,如果该值可以转换为double ,否则*
ok设置为False 。
此功能被引入Qt的4.6 。
See also canConvert()和convert( ) 。
QRect QVariant.toRect (self)
返回变量作为QRect如果该变体具有type( )Rect否则返回一个无效的QRect。
See also canConvert()和convert( ) 。
QRectF QVariant.toRectF (self)
返回变量作为QRectF如果该变体具有type( )Rect or RectF否则返回一个无效的QRectF。
See also canConvert()和convert( ) 。
QRegExp QVariant.toRegExp (self)
返回变量作为QRegExp如果该变体具有type( )RegExp否则返回一个空QRegExp。
这个函数是Qt 4.1中引入。
See also canConvert()和convert( ) 。
QSize QVariant.toSize (self)
返回变量作为QSize如果该变体具有type( )Size否则返回一个无效的QSize。
See also canConvert()和convert( ) 。
QSizeF QVariant.toSizeF (self)
返回变量作为QSizeF如果该变体具有type( )SizeF否则返回一个无效的QSizeF。
See also canConvert()和convert( ) 。
QString QVariant.toString (self)
返回变量作为QString如果该变体具有type( )String,Bool,ByteArray,Char,Date,DateTime,Double,Int,LongLong,StringList,Time,UInt或ULongLong否则返回一个空字符串。
See also canConvert()和convert( ) 。
QStringList QVariant.toStringList (self)
返回变量作为QStringList如果该变体具有type( )StringList,String或List这可以转换为一个类型的QString否则返回空列表。
See also canConvert()和convert( ) 。
QTime QVariant.toTime (self)
返回变量作为QTime如果该变体具有type( )Time,DateTime或String否则返回一个无效的时间。
如果type()是String,一个无效的时间,如果该字符串不能解析为一个将被退回Qt.ISODate格式时间。
See also canConvert()和convert( ) 。
(int, bool ok) QVariant.toUInt (self)
返回变量为一个unsigned int如果该变体具有type( )UInt,Bool,ByteArray,Char,Double,Int,LongLong,String或ULongLong否则返回0 。
If ok不为null :*
ok被设置为True,如果该值可以转换为一个unsigned int ,否则*
ok设置为False 。
Warning:如果该值可以转换为一个ULongLong但过大的一个unsigned int来表示,由此产生的算术溢出将不会反映在ok。一个简单的解决方法是使用QString.toUInt( ) 。修复这个bug已经被推迟到5的Qt为了避免破坏现有的代码。
See also canConvert()和convert( ) 。
(int, bool ok) QVariant.toULongLong (self)
返回变体为unsigned long long int的,如果有变异type( )ULongLong,Bool,ByteArray,Char,Double,Int,LongLong,String或UInt否则返回0 。
If ok不为null :*
ok被设置为True,如果该值可以转换为int ,否则*
ok设置为False 。
See also canConvert()和convert( ) 。
QUrl QVariant.toUrl (self)
返回变量作为QUrl如果该变体具有type( )Url否则返回一个无效的QUrl。
See also canConvert()和convert( ) 。
Type QVariant.type (self)
返回存储在变量值的存储类型。虽然这个函数被声明为返回QVariant.Type,返回值应解释为QMetaType.Type。特别地,QVariant.UserType这里只返回该值是否等于或大于QMetaType.User。
请注意,在该范围内的返回值QVariant.Char通过QVariant.RegExp和QVariant.Font通过QVariant.Transform对应于在范围中的值QMetaType.QChar通过QMetaType.QRegExp和QMetaType.QFont通过QMetaType.QQuaternion。
用char工作时,要特别注意和QChar变种。需要注意的是,没有QVariant构造函数专门为char类型,但有一个用于QChar。对于类型的变体QChar,这个函数返回QVariant.Char,这是相同的QMetaType.QChar,但是对于类型的变体char
,这个函数返回QMetaType.Char,这是not一样QVariant.Char。
还要注意的是类型void*
,long
,short
,unsigned
long
,unsigned
short
,unsigned
char
,float
,QObject*
和QWidget*
都派代表参加QMetaType.Type但不是在QVariant.Type,并且它们可以由该函数返回。然而,它们都被认为是用户定义的类型时,对测试QVariant.Type。
为了测试是否实例QVariant包含与你感兴趣的数据类型,使用兼容的数据类型canConvert( ) 。
str QVariant.typeName (self)
返回存储在所述变体的类型的名称。返回的字符串描述了C + +的数据类型来存储数据:例如, “QFont“,”QString“或”QVariantList“一个无效的变量返回0 。
str QVariant.typeToName (Type type)
存储类型的枚举表示转换,typ,其字符串表示形式。
返回一个空指针的类型是QVariant.Invalid或不存在。
int QVariant.userType (self)
返回存储在变量值的存储类型。对于非用户类型,这是相同的type( ) 。
See also type( ) 。
bool QVariant.__eq__ (self, QVariant v)
bool QVariant.__ne__ (self, QVariant v)