一个关于QVariant类的简单用法示例:
<span style="font-family:Microsoft YaHei;font-size:18px;">#include "widget.h"
#include<QDebug>
#include<QVariant>
#include<QColor>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVariant v(709);//声明并初始化一个整数变量
qDebug()<<v.toInt();//变为整数输出
QVariant w("How are you!");
qDebug()<<w.toString();
QMap<QString,QVariant>map;//声明一个QMap变量map,使用字符串为键,QVariant变量为值
map["int"]=709;//输入整数型
map["String"]="How are you!";
map["double"]=709.709;
map["color"]=QColor(255,0,0);
qDebug()<<map["int"]<<map["int"].toInt();//调用相应的函数并且输出
qDebug()<<map["double"]<<map["double"].toDouble();
qDebug()<<map["String"]<<map["String"].toString();
qDebug()<<map["color"]<<map["color"].value<QColor>();//使用模板QVariant.value()还原为Q

本文介绍了Qt中的 Variant 类,它是一个用于存储不同数据类型的强大工具。通过示例展示了如何创建、转换和使用 Variant 对象,揭示了其在Qt编程中的灵活性。
最低0.47元/天 解锁文章
2285

被折叠的 条评论
为什么被折叠?



