📄 qvariant.3qt
字号:
.ti -1c.BI "QTime & \fBasTime\fR ()".br.ti -1c.BI "QDateTime & \fBasDateTime\fR ()".br.ti -1c.BI "QByteArray & \fBasByteArray\fR ()".br.ti -1c.BI "QBitArray & \fBasBitArray\fR ()".br.ti -1c.BI "QKeySequence & \fBasKeySequence\fR ()".br.ti -1c.BI "int & \fBasInt\fR ()".br.ti -1c.BI "uint & \fBasUInt\fR ()".br.ti -1c.BI "bool & \fBasBool\fR ()".br.ti -1c.BI "double & \fBasDouble\fR ()".br.ti -1c.BI "QValueList<QVariant> & \fBasList\fR ()".br.ti -1c.BI "QMap<QString, QVariant> & \fBasMap\fR ()".br.ti -1c.BI "QSizePolicy & \fBasSizePolicy\fR ()".br.in -1c.SS "Static Public Members".in +1c.ti -1c.BI "const char * \fBtypeToName\fR ( Type typ )".br.ti -1c.BI "Type \fBnameToType\fR ( const char * name )".br.in -1c.SH DESCRIPTIONThe QVariant class acts like a union for the most common Qt data types..PPBecause C++ forbids unions from including types that have non-default constructors or destructors, most interesting Qt classes cannot be used in unions. This is a problem when using QObject::property(), among other things..PPThis class provides union functionality for property() and most other needs that might be solved by a union including e.g. QWidget..PPA QVariant object can hold any one type() at a time. For example, you can find out what type, T, it 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()..PPThe 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)..PPNote 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..PPThe 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..PPHere is example code to demonstrate the use of QVariant:.PP.nf.br QDataStream out(...);.br QVariant v(123); // The variant now contains an int.br int x = v.toInt(); // x = 123.br out << v; // Writes a type tag and an int to out.br v = QVariant("hello"); // The variant now contains a QCString.br v = QVariant(tr("hello"));// The variant now contains a QString.br int y = v.toInt(); // y = 0 since v cannot be converted to an int.br QString s = v.toString(); // s = tr("hello") (see QObject::tr()).br out << v; // Writes a type tag and a QString to out.br ....br QDataStream in(...); // (opening the previously written stream).br in >> v; // Reads an Int variant.br int z = v.toInt(); // z = 123.br qDebug("Type is %s", // prints "Type is int".br v.typeName());.br v.asInt() += 100; // The variant now hold the value 223..br v = QVariant( QStringList() );.br v.asStringList().append( "Hello" );.br.fi.PPYou can even have 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. (See the Collection Classes.).PPSee also Miscellaneous Classes and Object Model..SS "Member Type Documentation".SH "QVariant::Type"This enum type defines the types of variable that a QVariant can contain. The supported enum values and the associated types are.TP\fCQVariant::Invalid\fR - no type.TP\fCQVariant::List\fR - a QValueList<QVariant>.TP\fCQVariant::Map\fR - a QMap<QString,QVariant>.TP\fCQVariant::String\fR - a QString.TP\fCQVariant::StringList\fR - a QStringList.TP\fCQVariant::Font\fR - a QFont.TP\fCQVariant::Pixmap\fR - a QPixmap.TP\fCQVariant::Brush\fR - a QBrush.TP\fCQVariant::Rect\fR - a QRect.TP\fCQVariant::Size\fR - a QSize.TP\fCQVariant::Color\fR - a QColor.TP\fCQVariant::Palette\fR - a QPalette.TP\fCQVariant::ColorGroup\fR - a QColorGroup.TP\fCQVariant::IconSet\fR - a QIconSet.TP\fCQVariant::Point\fR - a QPoint.TP\fCQVariant::Image\fR - a QImage.TP\fCQVariant::Int\fR - an int.TP\fCQVariant::UInt\fR - an unsigned int.TP\fCQVariant::Bool\fR - a bool.TP\fCQVariant::Double\fR - a double.TP\fCQVariant::CString\fR - a QCString.TP\fCQVariant::PointArray\fR - a QPointArray.TP\fCQVariant::Region\fR - a QRegion.TP\fCQVariant::Bitmap\fR - a QBitmap.TP\fCQVariant::Cursor\fR - a QCursor.TP\fCQVariant::Date\fR - a QDate.TP\fCQVariant::Time\fR - a QTime.TP\fCQVariant::DateTime\fR - a QDateTime.TP\fCQVariant::ByteArray\fR - a QByteArray.TP\fCQVariant::BitArray\fR - a QBitArray.TP\fCQVariant::SizePolicy\fR - a QSizePolicy.TP\fCQVariant::KeySequence\fR - a QKeySequence.PPNote that Qt's definition of bool depends on the compiler. qglobal.h has the system-dependent definition of bool..SH MEMBER FUNCTION DOCUMENTATION.SH "QVariant::QVariant ()"Constructs an invalid variant..SH "QVariant::QVariant ( const QVariant & p )"Constructs a copy of the variant, \fIp\fR, 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..SH "QVariant::QVariant ( QDataStream & s )"Reads the variant from the data stream, \fIs\fR..SH "QVariant::QVariant ( const QString & val )"Constructs a new variant with a string value, \fIval\fR..SH "QVariant::QVariant ( const QCString & val )"Constructs a new variant with a C-string value, \fIval\fR..PPIf you want to modify the QCString after you've passed it to this constructor, we recommend passing a deep copy (see QCString::copy())..SH "QVariant::QVariant ( const char * val )"Constructs a new variant with a C-string value of \fIval\fR if \fIval\fR is non-null. The variant creates a deep copy of \fIval\fR..PPIf \fIval\fR is null, the resulting variant has type Invalid..SH "QVariant::QVariant ( const QStringList & val )"Constructs a new variant with a string list value, \fIval\fR..SH "QVariant::QVariant ( const QFont & val )"Constructs a new variant with a font value, \fIval\fR..SH "QVariant::QVariant ( const QPixmap & val )"Constructs a new variant with a pixmap value, \fIval\fR..SH "QVariant::QVariant ( const QImage & val )"Constructs a new variant with an image value, \fIval\fR..PPBecause 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..SH "QVariant::QVariant ( const QBrush & val )"Constructs a new variant with a brush value, \fIval\fR..SH "QVariant::QVariant ( const QPoint & val )"Constructs a new variant with a point value, \fIval\fR..SH "QVariant::QVariant ( const QRect & val )"Constructs a new variant with a rect value, \fIval\fR..SH "QVariant::QVariant ( const QSize & val )"Constructs a new variant with a size value, \fIval\fR..SH "QVariant::QVariant ( const QColor & val )"Constructs a new variant with a color value, \fIval\fR..SH "QVariant::QVariant ( const QPalette & val )"Constructs a new variant with a color palette value, \fIval\fR..SH "QVariant::QVariant ( const QColorGroup & val )"Constructs a new variant with a color group value, \fIval\fR..SH "QVariant::QVariant ( const QIconSet & val )"Constructs a new variant with an icon set value, \fIval\fR..SH "QVariant::QVariant ( const QPointArray & val )"Constructs a new variant with a point array value, \fIval\fR..PPBecause 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..SH "QVariant::QVariant ( const QRegion & val )"Constructs a new variant with a region value, \fIval\fR..SH "QVariant::QVariant ( const QBitmap & val )"Constructs a new variant with a bitmap value, \fIval\fR..SH "QVariant::QVariant ( const QCursor & val )"Constructs a new variant with a cursor value, \fIval\fR..SH "QVariant::QVariant ( const QDate & val )"Constructs a new variant with a date value, \fIval\fR..SH "QVariant::QVariant ( const QTime & val )"Constructs a new variant with a time value, \fIval\fR..SH "QVariant::QVariant ( const QDateTime & val )"Constructs a new variant with a date/time value, \fIval\fR..SH "QVariant::QVariant ( const QByteArray & val )"Constructs a new variant with a bytearray value, \fIval\fR..SH "QVariant::QVariant ( const QBitArray & val )"Constructs a new variant with a bitarray value, \fIval\fR..SH "QVariant::QVariant ( const QKeySequence & val )"Constructs a new variant with a key sequence value, \fIval\fR..SH "QVariant::QVariant ( const QValueList<QVariant> & val )"Constructs a new variant with a list value, \fIval\fR..SH "QVariant::QVariant ( const QMap<QString, QVariant> & val )"Constructs a new variant with a map of QVariants, \fIval\fR..SH "QVariant::QVariant ( int val )"Constructs a new variant with an integer value, \fIval\fR..SH "QVariant::QVariant ( uint val )"Constructs a new variant with an unsigned integer value, \fIval\fR..SH "QVariant::QVariant ( bool val, int )"Constructs a new variant with a boolean value, \fIval\fR. The integer argument is a dummy, necessary for compatibility with some compilers..SH "QVariant::QVariant ( double val )"Constructs a new variant with a floating point value, \fIval\fR..SH "QVariant::QVariant ( QSizePolicy val )"Constructs a new variant with a size policy value, \fIval\fR..SH "QVariant::~QVariant ()"Destroys the QVariant and the contained object..PPNote 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()..SH "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..PPReturns a reference to the stored bitarray..PPSee also toBitArray()..SH "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..PPReturns a reference to the stored bitmap..PPSee also toBitmap()..SH "bool & QVariant::asBool ()"Returns the variant's value as bool reference..SH "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..PPReturns a reference to the stored brush..PPSee also toBrush()..SH "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..PPReturns a reference to the stored bytearray..PPSee also toByteArray()..SH "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..PPReturns a reference to the stored string..PPSee also toCString()..SH "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..PPReturns a reference to the stored color..PPSee also toColor() and QColor::isValid()..SH "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 with all colors set to black..PPReturns a reference to the stored color group..PPSee also toColorGroup()..SH "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..PPReturns a reference to the stored cursor..PPSee also toCursor()..SH "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..PPReturns a reference to the stored date..PPSee also toDate()..SH "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..PPReturns a reference to the stored date/time..PPSee also toDateTime()..SH "double & QVariant::asDouble ()"Returns the variant's value as double reference..SH "QFont & QVariant::asFont ()"Tries to convert the variant to hold a QFont. If that is not possible the variant is set to a default font.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -