当前位置:网站首页>Summary of qvariant use in QT
Summary of qvariant use in QT
2022-06-23 06:43:00 【litanyuan】
background
QVariant It can store all kinds of data types , Include Qt All built-in types in 、C++ All basic data types in , And custom types .
Constructors
QVariant variant_bool(false);
QVariant variant_string(QString("hello"));
QVariant variant_double(3.14);
//... Custom types support direct construction
Key member function
①.type: Get the currently saved data type
QVariant variant;
qDebug() << variant.type();
variant.setValue(3.14);
qDebug() << variant.type();

②.value: Get the value saved to the specified type
QVariant variant(QString("hello"));
qDebug() << variant.value<QString>();
③.canConvert: Determine whether the stored type can be converted to the specified type
QVariant variant(3.14);
qDebug() << variant.type();
qDebug() << variant.canConvert<QString>();

④.convert: Convert the storage to type to the specified type
QVariant variant(QString("hello"));
qDebug() << variant.type();
qDebug() << variant.convert(QVariant::Int);
qDebug() << variant;

⑤.isValid: Determine whether a valid value is stored
QVariant variant;
qDebug() << variant.isValid();
variant = QString("hello");
qDebug() << variant.isValid();

Basic data type reading
①. Save the data
QVariant variant_int(12);
QVariant variant_string = QString("hello");
QVariant variant_bool;
variant_bool.setValue(false);
②. data fetch
QVariant variant_int(12);
QVariant variant_string = QString("hello");
QVariant variant_bool;
variant_bool.setValue(false);
qDebug() << variant_int.toInt();
qDebug() << variant_string.toString();
qDebug() << variant_bool.value<bool>();

Custom data type reading
①. Register as meta type
struct MyStruct
{
int id;
QString name;
};
Q_DECLARE_METATYPE(MyStruct)
QVariant variant;
{
MyStruct ms;
ms.id = 1;
ms.name = "hello";
variant.setValue<MyStruct>(ms);
}
MyStruct ms = variant.value<MyStruct>();
qDebug() << ms.id << ms.name;

②. Custom type conversion
struct MyStruct
{
int id;
QString name;
MyStruct() = default;
MyStruct(const QVariant & variant)
{
*this = variant.value<MyStruct>();
}
operator QVariant() const
{
return QVariant::fromValue(*this);
}
};
Q_DECLARE_METATYPE(MyStruct)
QVariant variant;
{
MyStruct ms;
ms.id = 1;
ms.name = "hello";
variant = ms;
}
MyStruct ms = variant;
qDebug() << ms.id << ms.name;

Pointer type access
MyStruct * ms = new MyStruct;
ms->id = 1;
ms->name = "hello";
QVariant variant = QVariant::fromValue(static_cast<void*>(ms));
MyStruct * myStruct = static_cast<MyStruct*>(variant.value<void*>());
qDebug() << myStruct->id << myStruct->name;

QVariantMap
①. summary
stay QVariantMap The header file has the following definitions :typedef QMap<QString,QVariant> QVariantMap, namely QVariantMap Is a type alias .
②. Code example
QVariantMap config;
config["id"] = 10;
config["name"] = QString("hello");
qDebug() << config["id"].toInt();
qDebug() << config["name"].toString();

边栏推荐
- 光谱共焦的测量原理及厚度测量模式
- SAP execution transaction code mrrl error -no message was found for partner 100065-
- Plot+seaborn+folium: a visual exploration of Abbey's rental housing data
- Understand how learning JSX works
- bootstrap如何清除浮动的样式
- 从 WAN 到 SD-WAN 边缘设备的网络架构
- JS to create an array (all elements are objects)
- Illuminate\Support\Collection 去重 unique 列表去重
- haas506 2.0开发教程-高级组件库-modem.voiceCall(仅支持2.2以上版本)
- leetcode - 572. A subtree of another tree
猜你喜欢

Network architecture from Wan to sd-wan edge devices
百度URL參數之LINK?URL參數加密解密研究(代碼實例)

Usage Summary of item views and item widgets controls in QT

Summary of business logic security ideas

如何迁移virtualbox 的虚拟机到hype-v

设计师需要懂的数据指标与数据分析模型

English语法_形容词比较级 - 3级变化

C语言学习总结

QT中的item views与Item widgets控件的用法总结

mingw-w64、msys和ffmpeg的配置与编译
随机推荐
熟练利用切片操作
Illuminate\Support\Collection 去重 unique 列表去重
How to build a data application system based on overall value for energy enterprises
【接口自动化】软件测试涨薪核心技能、让薪资涨幅200%
Qt使用多线程编译项目的方法
20220621 Three Conjugates of Dual Quaternions
Usage Summary of item views and item widgets controls in QT
记一次GLIB2.14升级GLIB2.18的记录以及其中的步骤原理
MySQL ON DUPLICATE KEY 和 PgSQL ON CONFLICT(主键) 处理主键冲突
Network architecture from Wan to sd-wan edge devices
C# wpf 附加属性实现界面上定义装饰器
Machine learning artifact scikit learn minimalist tutorial
Link of Baidu URL parameter? Research on URL parameter encryption and decryption (code example)
mars3d点线面的绘制和重置
MySQL5.6 (5.7-8) 基于shardingsphere5.1.1 Sharding-Proxy模式读写分离
业务逻辑安全思路总结
golang正则regexp包使用-04-使用正则替换(ReplaceAll(),ReplaceAllLiteral(),ReplaceAllFunc())
WPF Command指令和INotifyPropertyChanged
QT creator builds osgearth environment (osgqt msvc2017)
Focusing on the smart city, Huawei cooperates with China Science and technology Xingtu to jointly develop a new digital blue ocean