Obsolete Members for QVariant | Qt Core | Qt 6.11.2
Description: The QVariant class acts like a union for the most common Qt data types.

- Qt 6.11
- Qt Core
- C++ Classes
- QVariant
- Qt 6.11.2 Reference Documentation

# Obsolete Members for QVariant

The following members of class QVariant are deprecated. They are provided to keep old source code working. We strongly advise against using them in new code.

## Public Types

`(deprecated)` enum | Type { Invalid, BitArray, Bitmap, Bool, Brush, …, UserType }

## Public Functions

`(deprecated in 6.0)` | QVariant(QVariant::Type type)
`(deprecated in 6.0)` bool | canConvert(int targetTypeId) const
`(deprecated in 6.0)` bool | convert(int targetTypeId)
`(deprecated in 6.0)` QVariant::Type | type() const

## Static Public Members

`(deprecated in 6.0)` QVariant::Type | nameToType(const char *name)
`(deprecated in 6.0)` const char * | typeToName(int typeId)

## Related Non-Members

`(deprecated in 6.0)` QDataStream & | operator<<(QDataStream &s, const QVariant::Type p)
`(deprecated in 6.0)` QDataStream & | operator>>(QDataStream &s, QVariant::Type &p)

## Member Type Documentation

### `[deprecated]` enum QVariant::Type

This enum is deprecated. We strongly advise against using it in new code.

Use QMetaType::Type instead.

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

Constant | Value | Description
--- | --- | ---
`QVariant::Invalid` | `QMetaType::UnknownType` | no type
`QVariant::BitArray` | `QMetaType::QBitArray` | a QBitArray
`QVariant::Bitmap` | `QMetaType::QBitmap` | a QBitmap
`QVariant::Bool` | `QMetaType::Bool` | a bool
`QVariant::Brush` | `QMetaType::QBrush` | a QBrush
`QVariant::ByteArray` | `QMetaType::QByteArray` | a QByteArray
`QVariant::Char` | `QMetaType::QChar` | a QChar
`QVariant::Color` | `QMetaType::QColor` | a QColor
`QVariant::Cursor` | `QMetaType::QCursor` | a QCursor
`QVariant::Date` | `QMetaType::QDate` | a QDate
`QVariant::DateTime` | `QMetaType::QDateTime` | a QDateTime
`QVariant::Double` | `QMetaType::Double` | a double
`QVariant::EasingCurve` | `QMetaType::QEasingCurve` | a QEasingCurve
`QVariant::Uuid` | `QMetaType::QUuid` | a QUuid
`QVariant::ModelIndex` | `QMetaType::QModelIndex` | a QModelIndex
`QVariant::PersistentModelIndex (since Qt 5.5)` | `QMetaType::QPersistentModelIndex` | a QPersistentModelIndex
`QVariant::Font` | `QMetaType::QFont` | a QFont
`QVariant::Hash` | `QMetaType::QVariantHash` | a QVariantHash
`QVariant::Icon` | `QMetaType::QIcon` | a QIcon
`QVariant::Image` | `QMetaType::QImage` | a QImage
`QVariant::Int` | `QMetaType::Int` | an int
`QVariant::KeySequence` | `QMetaType::QKeySequence` | a QKeySequence
`QVariant::Line` | `QMetaType::QLine` | a QLine
`QVariant::LineF` | `QMetaType::QLineF` | a QLineF
`QVariant::List` | `QMetaType::QVariantList` | a QVariantList
`QVariant::Locale` | `QMetaType::QLocale` | a QLocale
`QVariant::LongLong` | `QMetaType::LongLong` | a qlonglong
`QVariant::Map` | `QMetaType::QVariantMap` | a QVariantMap
`QVariant::Transform` | `QMetaType::QTransform` | a QTransform
`QVariant::Matrix4x4` | `QMetaType::QMatrix4x4` | a QMatrix4x4
`QVariant::Palette` | `QMetaType::QPalette` | a QPalette
`QVariant::Pen` | `QMetaType::QPen` | a QPen
`QVariant::Pixmap` | `QMetaType::QPixmap` | a QPixmap
`QVariant::Point` | `QMetaType::QPoint` | a QPoint
`QVariant::PointF` | `QMetaType::QPointF` | a QPointF
`QVariant::Polygon` | `QMetaType::QPolygon` | a QPolygon
`QVariant::PolygonF` | `QMetaType::QPolygonF` | a QPolygonF
`QVariant::Quaternion` | `QMetaType::QQuaternion` | a QQuaternion
`QVariant::Rect` | `QMetaType::QRect` | a QRect
`QVariant::RectF` | `QMetaType::QRectF` | a QRectF
`QVariant::RegularExpression` | `QMetaType::QRegularExpression` | a QRegularExpression
`QVariant::Region` | `QMetaType::QRegion` | a QRegion
`QVariant::Size` | `QMetaType::QSize` | a QSize
`QVariant::SizeF` | `QMetaType::QSizeF` | a QSizeF
`QVariant::SizePolicy` | `QMetaType::QSizePolicy` | a QSizePolicy
`QVariant::String` | `QMetaType::QString` | a QString
`QVariant::StringList` | `QMetaType::QStringList` | a QStringList
`QVariant::TextFormat` | `QMetaType::QTextFormat` | a QTextFormat
`QVariant::TextLength` | `QMetaType::QTextLength` | a QTextLength
`QVariant::Time` | `QMetaType::QTime` | a QTime
`QVariant::UInt` | `QMetaType::UInt` | a uint
`QVariant::ULongLong` | `QMetaType::ULongLong` | a qulonglong
`QVariant::Url` | `QMetaType::QUrl` | a QUrl
`QVariant::Vector2D` | `QMetaType::QVector2D` | a QVector2D
`QVariant::Vector3D` | `QMetaType::QVector3D` | a QVector3D
`QVariant::Vector4D` | `QMetaType::QVector4D` | a QVector4D
`QVariant::UserType` | `QMetaType::User` | Base value for user-defined types.

## Member Function Documentation

### `[explicit, deprecated in 6.0]` QVariant::QVariant(QVariant::Type type)

This function is deprecated since 6.0. We strongly advise against using it in new code.

Use the constructor taking a QMetaType instead.

Constructs an uninitialized variant of type type. This will create a variant in a special null state that if accessed will return a default constructed value of the type.

See also isNull().

### `[deprecated in 6.0]` bool QVariant::canConvert(int targetTypeId) const

This function is deprecated since 6.0. We strongly advise against using it in new code.

Use `canConvert(QMetaType(targetTypeId))` instead.

This is an overloaded function.

See also QMetaType::canConvert().

### `[deprecated in 6.0]` bool QVariant::convert(int targetTypeId)

This function is deprecated since 6.0. We strongly advise against using it in new code.

Use `convert(QMetaType(targetTypeId))` instead.

Casts the variant to the requested type, targetTypeId. If the cast cannot be done, the variant is still changed to the requested type, but is left in a cleared null state similar to that constructed by QVariant(Type).

Returns `true` if the current type of the variant was successfully cast; otherwise returns `false`.

A QVariant containing a pointer to a type derived from QObject will also convert and return true for this function if a qobject_cast to the type described by targetTypeId would succeed. Note that this only works for QObject subclasses which use the Q_OBJECT macro.

Note: converting QVariants that are null due to not being initialized or having failed a previous conversion will always fail, changing the type, remaining null, and returning `false`.

See also canConvert() and clear().

### `[static, deprecated in 6.0]` QVariant::Type QVariant::nameToType(const char *name)

This function is deprecated since 6.0. We strongly advise against using it in new code.

Use `QMetaType::fromName(name).id()` instead.

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`.

### `[deprecated in 6.0]` QVariant::Type QVariant::type() const

This function is deprecated since 6.0. We strongly advise against using it in new code.

Use typeId() or metaType() instead.

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 through QVariant::RegExp and QVariant::Font through QVariant::Transform correspond to the values in the ranges QMetaType::QChar through QMetaType::QRegularExpression and QMetaType::QFont through 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, this function returns QVariant::Char, which is the same as QMetaType::QChar, but for a variant of type `char`, this function returns QMetaType::Char, which is not the same as QVariant::Char.

Also note that the types `void*`, `long`, `short`, `unsigned` `long`, `unsigned` `short`, `unsigned` `char`, `float`, `QObject*`, and `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().

See also userType() and metaType().

### `[static, deprecated in 6.0]` const char *QVariant::typeToName(int typeId)

This function is deprecated since 6.0. We strongly advise against using it in new code.

Use `QMetaType(typeId).name()` instead.

Converts the int representation of the storage type, typeId, to its string representation.

Returns `nullptr` if the type is QMetaType::UnknownType or doesn't exist.

## Related Non-Members

### `[deprecated in 6.0]` QDataStream &operator<<(QDataStream &s, const QVariant::Type p)

This function is deprecated since 6.0. We strongly advise against using it in new code.

Stream QMetaType::Type instead.

Writes a variant type p to the stream s.

### `[deprecated in 6.0]` QDataStream &operator>>(QDataStream &s, QVariant::Type &p)

This function is deprecated since 6.0. We strongly advise against using it in new code.

Stream QMetaType::Type instead.

Reads a variant type p in enum representation from the stream s.