当前位置:网站首页>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
};
构造函数

参考:
边栏推荐
- 1000 okaleido tiger launched binance NFT, triggering a rush to buy
- Simple setting of drop-down triangle
- If it is modified according to the name of the framework module
- 冯诺依曼体系结构
- According to the use and configuration of data permissions in the open source framework
- 数据平台下的数据治理
- Naming rules of software test pytest pytest the pre and post confitest of use cases Py customized allure report @pytest.mark.parameter() decorator as data-driven
- Simulated Xiaomi mall head home page
- Data type conversion
- 【集训DAY12】Minn ratio 【dfs】【最小生成树】
猜你喜欢

Build commercial projects based on ruoyi framework
![[training Day12] min ratio [DFS] [minimum spanning tree]](/img/f8/e37efd0d2aa0b3d79484b9ac42b7eb.png)
[training Day12] min ratio [DFS] [minimum spanning tree]

It's over. I went to work for three months and became bald

Arcgis10.2 configuring postgresql9.2 standard tutorial

ThreadLocal summary (to be continued)

Binder principle

贴片微型滚珠振动开关的结构原理

Binder原理

We media people must have four material websites, and don't worry about finding materials anymore

Two methods of printing strings in reverse order in C language
随机推荐
XSS collect common code
Wkid in ArcGIS
721. 账户合并 ●●、并查集
【集训DAY12】Bee GO!【动态规划】【数学】
JS interview questions
3 lexical analysis
According to the use and configuration of data permissions in the open source framework
[training Day11] Nescafe [greed]
721. Account consolidation ●●, and collection
Arcgis10.2 configuring postgresql9.2 standard tutorial
QT log file system
武汉理工大学第三届程序设计竞赛 B-拯救DAG王国(拓扑性质处理可达性统计问题)
3dslicer importing medical image data
Data type conversion
Wechat applet (anti shake, throttling), which solves the problem that users keep pulling down refresh requests or clicking buttons to submit information; Get the list information and refresh the data
Matrix of C language
What is the difference between character constants and string constants?
对需求的内容进行jieba分词并按词频排序输出excel文档
(1) DDL, DML, DQL, DCL and common data types
依法严厉打击违规自媒体运营者:净化自媒体行业迫在眉睫