当前位置:网站首页>QVariant的使用
QVariant的使用
2022-07-25 22:40:00 【物随心转】
一、介绍
QT的官方文档这么写的:The QVariant class acts like a union for the most common Qt data types。QVariant可以存储各种数据类型,QVariant行为类似于C/C++的union, 但在Qt中比union强大很多。
QVariant内置支持所有QMetaType::Type里声明的类如:int,QString,QFont,QColor等,甚至QList,QMap<QString, QVariant>等组成的任意复杂类型。
当然,如果支持的类型没有想要的,没关系,QVariant也可以支持自定义的数据类型。被QVariant存储的数据类型需要有一个默认的构造函数和一个拷贝构造函数。为了实现这个功能,首先必须使用Q_DECLARE_METATYPE()宏。通常会将这个宏放在类的声明所在头文件的下面:
Q_DECLARE_METATYPE(MyClass)
二、使用
普通变量
QVariant var1;
var1.setValue(12);
int data1 = var1.toInt();
qDebug() << data1;
QVariant var2 = 14;
int data2 = var2.toInt();
qDebug() << data2;打印

结构体
#include <QtCore/QCoreApplication>
#include <QString>
#include <QtDebug>
#include <QVariant>
struct MyClass {
int id;
QString name;
};
Q_DECLARE_METATYPE(MyClass)
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyClass myClass;
myClass.id = 0;
myClass.name = QString("LiMing");
QVariant var3;
var3.setValue(myClass); //设置QVariant的值
qDebug() << "var3:" << var3;
MyClass stu; //这部分代码主要是将QVariant类再转化为MyClass类,其他QVariant类转化成其他类也可用这种方法
stu = var3.value<MyClass>();
qDebug() << "stu:" << stu.id << stu.name;
return a.exec();
} 
保存指针
//保存
QVariant var = QVariant::fromValue((void*)event);
//获取
QPaintEvent* pointer = (QPaintEvent*)var.value<void*>();三、源码
打开QVariant的定义,我们看到它使用了C++的联合
struct Private
{
union Data
{
char c;
uchar uc;
short s;
signed char sc;
ushort us;
int i;
uint u;
long l;
ulong ul;
bool b;
double d;
float f;
qreal real;
qlonglong ll;
qulonglong ull;
QObject *o;
void *ptr;
PrivateShared *shared;
} data;
uint type : 30;
uint is_shared : 1;
uint is_null : 1;
};
支持的类型
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
};
构造函数

参考:
边栏推荐
- Explore the use of self increasing and self decreasing operators
- Examples and points for attention about the use of getchar and scanf
- Common source code for ArcGIS development
- Interpretation of the source code of all logging systems in XXL job (line by line source code interpretation)
- Interview question 17.11. word distance ●●
- Interpretation of English terms
- torchvision
- Platform architecture construction
- MatrixCube揭秘102——300行实现的完整分布式存储系统MatrixKV
- Today, learn about the use of lists, hyperlinks, image tags, and audio and video
猜你喜欢
![[training day15] boring [tree DP]](/img/78/dc80076bb9fc4cf008c51b00ece431.png)
[training day15] boring [tree DP]

JVM memory area

The third programming competition of Wuhan University of technology b- save the kingdom of DAG (topological properties deal with accessibility Statistics)

Xiaobai programmer day 8

关于getchar和scanf的使用示例及注意点

Structure principle of micro ball vibration switch with chip
![[training day13] backpack [dynamic planning] [greed]](/img/a7/3df395d84f510dea8b42ebcc4ff5f2.png)
[training day13] backpack [dynamic planning] [greed]

Data quality: the core of data governance

LabVIEW 开发 PCI-1680U双端口CAN卡

Interview question 17.11. word distance ●●
随机推荐
Method of converting MAPGIS format to ArcGIS
Xiaobai programmer's fourth day
Multi data source switching
C语言逆序打印字符串的两种方法
Document flow definition, box model related knowledge
ECMA 262 12 Lexical Grammer
ThreadLocal summary (to be continued)
Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture
关于getchar和scanf的使用示例及注意点
贴片微型滚珠振动开关的结构原理
Simple setting of drop-down triangle
[training day13] Internet [concurrent search]
JD quick navigation box
3dslicer introduction and installation tutorial
[training day13] backpack [dynamic planning] [greed]
Wkid in ArcGIS
Data quality: the core of data governance
分享两个音乐播放地址
【Leetcode】502.IPO(困难)
Using simple scripts to process data in 3dslicer