答案:可以
源码如下,构造可以传入Type类型的参数
enum Type {
Invalid = QMetaType::UnknownType,
Bool = QMetaType::Bool,
Int = QMetaType::Int,
UInt = QMetaType::UInt,
LongLong = QMetaType::LongLong,
ULongLong = QMetaType::ULongLong,
Double = QMetaType::Double,
Char = QMetaType::QChar,
Map = QMetaType::QVariantMap,
List = QMetaType::QVariantList,
String = QMetaType::QString,
StringList = QMetaType::QStringList,
ByteArray = QMetaType::QByteArray,
BitArray = QMetaType::QBitArray,
Date = QMetaType::QDate,
Time = QMetaType::QTime,
DateTime = QMetaType::QDateTime,
Url = QMetaType::QUrl,
Locale = QMetaType::QLocale,
Rect = QMetaType::QRect,
RectF = QMetaType::QRectF,
Size = QMetaType::QSize,
SizeF = QMetaType::QSizeF,
Line = QMetaType::QLine,
LineF = QMetaType::QLineF,
Point = QMetaType::QPoint,
PointF = QMetaType::QPointF,
RegExp = QMetaType::QRegExp,
RegularExpression = QMetaType::QRegularExpression,
Hash = QMetaType::QVariantHash,
EasingCurve = QMetaType::QEasingCurve,
Uuid = QMetaType::QUuid,
ModelIndex = QMetaType::QModelIndex,
PersistentModelIndex = QMetaType::QPersistentModelIndex,
LastCoreType = QMetaType::LastCoreType,
Font = QMetaType::QFont,
Pixmap = QMetaType::QPixmap,
Brush = QMetaType::QBrush,
Color = QMetaType::QColor,
Palette = QMetaType::QPalette,
Image = QMetaType::QImage,
Polygon = QMetaType::QPolygon,
Region = QMetaType::QRegion,
Bitmap = QMetaType::QBitmap,
Cursor = QMetaType::QCursor,
KeySequence = QMetaType::QKeySequence,
Pen = QMetaType::QPen,
TextLength = QMetaType::QTextLength,
TextFormat = QMetaType::QTextFormat,
Matrix = QMetaType::QMatrix,
Transform = QMetaType::QTransform,
Matrix4x4 = QMetaType::QMatrix4x4,
Vector2D = QMetaType::QVector2D,
Vector3D = QMetaType::QVector3D,
Vector4D = QMetaType::QVector4D,
Quaternion = QMetaType::QQuaternion,
PolygonF = QMetaType::QPolygonF,
Icon = QMetaType::QIcon,
LastGuiType = QMetaType::LastGuiType,
SizePolicy = QMetaType::QSizePolicy,
UserType = QMetaType::User,
LastType = 0xffffffff // need this so that gcc >= 3.4 allocates 32 bits for Type
};
QVariant() Q_DECL_NOTHROW : d() {}
~QVariant();
QVariant(Type type);
那么精度会损失吗?不会
QVector<QPointF> poss;
poss.push_back(QPointF(11.999999, 11.009789999));
poss.push_back(QPointF(22.5677899, 22.00123499));
poss.push_back(QPointF(33.884455, 33.776655));
QVariant vecotrPos = QVariant::fromValue(poss);
//可转换为QVector<QPointF>
if (vecotrPos.canConvert<QVector<QPointF>>()) {
QPolygonF pf = vecotrPos.value<QVector<QPointF>>();
qDebug() << "QVariant to QVector<QPointF>" << pf;
} else {
qDebug() << "can not convert QVector<QPointF>";
}
//不能转换为QPolygonF
if (vecotrPos.canConvert<QPolygonF>()) {
qDebug() << "QVariant to QVector<QPointF>" << vecotrPos.value<QPolygonF>();
} else {
qDebug() << "can not convert QPolygonF" << vecotrPos.value<QPolygonF>();
}
打印如下
QVariant to QVector<QPointF> QPolygonF(QPointF(12,11.0098)QPointF(22.5678,22.0012)QPointF(33.8845,33.7767))
can not convert QPolygonF QPolygonF()