Obsolete Members for QVariant

QVariant 类的以下成员已被弃用。提供这些成员是为了确保旧源代码能够正常运行。我们强烈建议不要在新代码中使用它们。

公共类型

(deprecated in 6.0) enum Type { Invalid, BitArray, Bitmap, Bool, Brush, …, UserType }

公共函数

(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

静态公共成员

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

相关的非成员

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

成员类型文档

[deprecated in 6.0] enum QVariant::Type

该枚举自 6.0 起已弃用。我们强烈建议不要在新代码中使用它。

请改用 `QMetaType::Type `。

此枚举类型定义了QVariant 可以包含的变量类型。

常量值描述
QVariant::InvalidQMetaType::UnknownType无类型
QVariant::BitArrayQMetaType::QBitArrayaQBitArray
QVariant::BitmapQMetaType::QBitmapaQBitmap
QVariant::BoolQMetaType::Boola bool
QVariant::BrushQMetaType::QBrushaQBrush
QVariant::ByteArrayQMetaType::QByteArrayaQByteArray
QVariant::CharQMetaType::QCharaQChar
QVariant::ColorQMetaType::QColoraQColor
QVariant::CursorQMetaType::QCursoraQCursor
QVariant::DateQMetaType::QDateaQDate
QVariant::DateTimeQMetaType::QDateTimeaQDateTime
QVariant::DoubleQMetaType::Doublea double
QVariant::EasingCurveQMetaType::QEasingCurveaQEasingCurve
QVariant::UuidQMetaType::QUuidaQUuid
QVariant::ModelIndexQMetaType::QModelIndexaQModelIndex
QVariant::PersistentModelIndex (since Qt 5.5)QMetaType::QPersistentModelIndexaQPersistentModelIndex
QVariant::FontQMetaType::QFontaQFont
QVariant::HashQMetaType::QVariantHashaQVariantHash
QVariant::IconQMetaType::QIconaQIcon
QVariant::ImageQMetaType::QImageaQImage
QVariant::IntQMetaType::Int一个整数
QVariant::KeySequenceQMetaType::QKeySequenceaQKeySequence
QVariant::LineQMetaType::QLineaQLine
QVariant::LineFQMetaType::QLineFaQLineF
QVariant::ListQMetaType::QVariantListaQVariantList
QVariant::LocaleQMetaType::QLocaleaQLocale
QVariant::LongLongQMetaType::LongLongaqlonglong
QVariant::MapQMetaType::QVariantMapaQVariantMap
QVariant::TransformQMetaType::QTransformaQTransform
QVariant::Matrix4x4QMetaType::QMatrix4x4aQMatrix4x4
QVariant::PaletteQMetaType::QPaletteaQPalette
QVariant::PenQMetaType::QPenaQPen
QVariant::PixmapQMetaType::QPixmapaQPixmap
QVariant::PointQMetaType::QPointaQPoint
QVariant::PointFQMetaType::QPointFaQPointF
QVariant::PolygonQMetaType::QPolygonaQPolygon
QVariant::PolygonFQMetaType::QPolygonFaQPolygonF
QVariant::QuaternionQMetaType::QQuaternionaQQuaternion
QVariant::RectQMetaType::QRectaQRect
QVariant::RectFQMetaType::QRectFaQRectF
QVariant::RegularExpressionQMetaType::QRegularExpressionaQRegularExpression
QVariant::RegionQMetaType::QRegionaQRegion
QVariant::SizeQMetaType::QSizeaQSize
QVariant::SizeFQMetaType::QSizeFaQSizeF
QVariant::SizePolicyQMetaType::QSizePolicyaQSizePolicy
QVariant::StringQMetaType::QStringaQString
QVariant::StringListQMetaType::QStringListaQStringList
QVariant::TextFormatQMetaType::QTextFormataQTextFormat
QVariant::TextLengthQMetaType::QTextLengthaQTextLength
QVariant::TimeQMetaType::QTimeaQTime
QVariant::UIntQMetaType::UIntauint
QVariant::ULongLongQMetaType::ULongLongaqulonglong
QVariant::UrlQMetaType::QUrlaQUrl
QVariant::Vector2DQMetaType::QVector2DaQVector2D
QVariant::Vector3DQMetaType::QVector3DaQVector3D
QVariant::Vector4DQMetaType::QVector4DaQVector4D
QVariant::UserTypeQMetaType::User用户定义类型的基值。

成员函数文档

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

自 6.0 起,此函数已被废弃。我们强烈建议不要在新的代码中使用它。

请改用接受 `QMetaType ` 作为参数的构造函数。

构建一个类型为 `type` 的未初始化变体。这将创建一个处于特殊空状态的变体,若对其进行访问,将返回 `type` 的默认构造值。

另请参阅 isNull()。

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

自 6.0 版本起,此函数已弃用。我们强烈建议不要在新代码中使用它。

请改用 `canConvert(QMetaType(targetTypeId)) `。

这是一个重载函数。

另请参阅 QMetaType::canConvert()。

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

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

请改用convert(QMetaType(targetTypeId)) 。

将变体强制转换为请求的类型,即targetTypeId 。如果无法进行强制转换,变体仍会被转换为请求的类型,但会被置于一个已清空的空状态,类似于由QVariant(Type) 构造的状态。

如果变体的当前类型已成功转换,则返回 `true `;否则返回 `false`。

如果将qobject_cast 转换为targetTypeId 所描述的类型能成功,那么包含指向QObject 派生类型指针的QVariant 也会被转换,并使此函数返回true。请注意,这仅适用于使用Q_OBJECT 宏的QObject 子类。

注意: 对于因未初始化或先前转换失败而为空的 QVariant,转换操作 将始终失败,此时类型会发生改变,值仍为空,并返回false 。

另请参阅 canConvert() 和clear()。

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

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

请改用QMetaType::fromName(name).id() 。

将name 中指定的存储类型的字符串表示形式转换为其枚举表示形式。

如果字符串表示形式无法转换为任何枚举表示形式,则该变体将设置为Invalid 。

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

自 6.0 版本起,该函数已被废弃。我们强烈建议不要在新代码中使用它。

请改用 `typeId()` 或 `metaType()`。

返回存储在变体中的值的存储类型。尽管该函数声明返回类型为 `QVariant::Type`,但其返回值应被解释为 `QMetaType::Type`。特别地,只有当值大于等于 `QMetaType::User` 时,此处才会返回 `QVariant::UserType `。

请注意,范围从QVariant::Char 到 QVariant::RegExp 以及从QVariant::Font 到QVariant::Transform 的返回值,分别对应于范围从QMetaType::QChar 到QMetaType::QRegularExpression 以及从QMetaType::QFont 到QMetaType::QQuaternion 中的值。

在处理 char 类型和QChar 变体时需特别注意。请注意,虽然没有专门针对 char 类型的QVariant 构造函数,但针对QChar 类型有一个。对于QChar 类型的变体,该函数返回QVariant::Char ,这与QMetaType::QChar 相同;但对于char 类型的变体,该函数返回QMetaType::Char ,这与QVariant::Char不相同。

另请注意,类型void* 、long 、short 、unsigned 、long 、unsigned 、short 、unsigned 、char 、float 、QObject* 和QWidget* 在QMetaType::Type 中有定义,但在QVariant::Type 中没有,且该函数可能返回这些类型。然而,当与QVariant::Type 进行兼容性测试时,它们会被视为用户自定义类型。

要测试QVariant 的实例是否包含与您感兴趣的数据类型兼容的数据类型,请使用canConvert()。

另请参阅 userType() 和metaType()。

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

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

请改用 `QMetaType(typeId).name() `。

将存储类型 `typeId` 的整数表示形式转换为其字符串表示形式。

如果类型为QMetaType::UnknownType 或不存在,则返回nullptr 。

相关的非成员函数

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

自 6.0 版本起,此函数已被废弃。我们强烈建议不要在新的代码中使用它。

请改用QMetaType::Type 流。

将变体类型p 写入流s 。

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

自 6.0 版起,此函数已被废弃。我们强烈建议不要在新代码中使用它。

请改用QMetaType::Type 进行流读取。

从流s 中读取以枚举表示形式的变体类型p 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.