首页 > 编程知识 正文

qt中qvariant变量的使用,qt中的类

时间:2023-05-03 17:49:53 阅读:216238 作者:249

一个关于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()还原为QColor输出 QStringList sl;//创建一个字符串列表 sl<<"A"<<"B"<<"C"<<"D";//为列表赋值 QVariant slv(sl);//将列表保存在QVariant变量中 if(slv.type()==QVariant::StringList)// { QStringList list=slv.toStringList(); for(int i=0;i<list.size();++i) qDebug()<<list.at(i); }}Widget::~Widget(){}</span>


结果如下所示:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。