Obsolete Members for QVariant

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

公共类型

(deprecated) 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] enum QVariant::Type

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

请使用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 双
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::Intan 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 类型的未初始化变量。这将创建一个处于特殊 null 状态的变量,如果访问该变量,将返回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

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

注意: 如果 QVariants 因未初始化或之前的转换失败而为空,则转换将始终失败,它将改变类型,保持为空,并返回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::FontQVariant::Transform 范围内的返回值对应于QMetaType::QCharQMetaType::QRegularExpressionQMetaType::QFontQMetaType::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 的 int 表示转换为字符串表示。

如果类型为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

© 2025 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.