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::Invalid | QMetaType::UnknownType | 无类型 |
QVariant::BitArray | QMetaType::QBitArray | aQBitArray |
QVariant::Bitmap | QMetaType::QBitmap | aQBitmap |
QVariant::Bool | QMetaType::Bool | a bool |
QVariant::Brush | QMetaType::QBrush | aQBrush |
QVariant::ByteArray | QMetaType::QByteArray | aQByteArray |
QVariant::Char | QMetaType::QChar | aQChar |
QVariant::Color | QMetaType::QColor | aQColor |
QVariant::Cursor | QMetaType::QCursor | aQCursor |
QVariant::Date | QMetaType::QDate | aQDate |
QVariant::DateTime | QMetaType::QDateTime | aQDateTime |
QVariant::Double | QMetaType::Double | a double |
QVariant::EasingCurve | QMetaType::QEasingCurve | aQEasingCurve |
QVariant::Uuid | QMetaType::QUuid | aQUuid |
QVariant::ModelIndex | QMetaType::QModelIndex | aQModelIndex |
QVariant::PersistentModelIndex (since Qt 5.5) | QMetaType::QPersistentModelIndex | aQPersistentModelIndex |
QVariant::Font | QMetaType::QFont | aQFont |
QVariant::Hash | QMetaType::QVariantHash | aQVariantHash |
QVariant::Icon | QMetaType::QIcon | aQIcon |
QVariant::Image | QMetaType::QImage | aQImage |
QVariant::Int | QMetaType::Int | 一个整数 |
QVariant::KeySequence | QMetaType::QKeySequence | aQKeySequence |
QVariant::Line | QMetaType::QLine | aQLine |
QVariant::LineF | QMetaType::QLineF | aQLineF |
QVariant::List | QMetaType::QVariantList | aQVariantList |
QVariant::Locale | QMetaType::QLocale | aQLocale |
QVariant::LongLong | QMetaType::LongLong | aqlonglong |
QVariant::Map | QMetaType::QVariantMap | aQVariantMap |
QVariant::Transform | QMetaType::QTransform | aQTransform |
QVariant::Matrix4x4 | QMetaType::QMatrix4x4 | aQMatrix4x4 |
QVariant::Palette | QMetaType::QPalette | aQPalette |
QVariant::Pen | QMetaType::QPen | aQPen |
QVariant::Pixmap | QMetaType::QPixmap | aQPixmap |
QVariant::Point | QMetaType::QPoint | aQPoint |
QVariant::PointF | QMetaType::QPointF | aQPointF |
QVariant::Polygon | QMetaType::QPolygon | aQPolygon |
QVariant::PolygonF | QMetaType::QPolygonF | aQPolygonF |
QVariant::Quaternion | QMetaType::QQuaternion | aQQuaternion |
QVariant::Rect | QMetaType::QRect | aQRect |
QVariant::RectF | QMetaType::QRectF | aQRectF |
QVariant::RegularExpression | QMetaType::QRegularExpression | aQRegularExpression |
QVariant::Region | QMetaType::QRegion | aQRegion |
QVariant::Size | QMetaType::QSize | aQSize |
QVariant::SizeF | QMetaType::QSizeF | aQSizeF |
QVariant::SizePolicy | QMetaType::QSizePolicy | aQSizePolicy |
QVariant::String | QMetaType::QString | aQString |
QVariant::StringList | QMetaType::QStringList | aQStringList |
QVariant::TextFormat | QMetaType::QTextFormat | aQTextFormat |
QVariant::TextLength | QMetaType::QTextLength | aQTextLength |
QVariant::Time | QMetaType::QTime | aQTime |
QVariant::UInt | QMetaType::UInt | auint |
QVariant::ULongLong | QMetaType::ULongLong | aqulonglong |
QVariant::Url | QMetaType::QUrl | aQUrl |
QVariant::Vector2D | QMetaType::QVector2D | aQVector2D |
QVariant::Vector3D | QMetaType::QVector3D | aQVector3D |
QVariant::Vector4D | QMetaType::QVector4D | aQVector4D |
QVariant::UserType | QMetaType::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()。
[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.