Qt QVariant类应用

QVariant类

QVariant类本质为C++联合(Union)数据类型,它可以保存很多Qt类型的值,包括 QBrush,QColor,QString等等,也能存放Qt的容器类型的值。

QVariant::StringList 是 Qt 定义的一个 QVariant::type 枚举类型的变量,其他常用的枚举类型变量如下表所示:

代码如下:

main.cpp

cpp 复制代码
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>


struct student
{
    int iNo;
    QString strName;
    int score;
};
Q_DECLARE_METATYPE(student);


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
};
#endif // MAINWINDOW_H

mainwindow.cpp

cpp 复制代码
#include "mainwindow.h"

#include <QVariant>
#include <QDebug>
#include <QColor>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QVariant qv1(298);
    qDebug()<<"qv1:"<<qv1.toInt();

    QVariant qv2("Hello World!");
    qDebug()<<"qv2:"<<qv2.toString();

    QMap<QString,QVariant> qmap;
    qDebug()<<endl;
    qmap["int"]=20000;  // 整型
    qmap["double"]=99.88;  // 浮点型
    qmap["string"]="GoodBye";  // 字符串
    qmap["color"]=QColor(255,255,0);  // QColor类型

    // 输出:转换函数来处理
    qDebug()<<qmap["int"]<<qmap["int"].toInt();
    qDebug()<<qmap["double"]<<qmap["double"].toDouble();
    qDebug()<<qmap["string"]<<qmap["int"].toString();
    qDebug()<<qmap["color"]<<qmap["int"].value<QColor>();

    // 创建一个字符串列表:QStringList
    qDebug()<<endl;
    QStringList qsl;
    qsl<<"A"<<"B"<<"C"<<"D"<<"E"<<"F";

    QVariant qvsl(qsl);    // 将列表存储在一个QVariant变量
    if(qvsl.type()==QVariant::StringList)
    {
        QStringList qlist=qvsl.toStringList();
        for(int i=0;i<qlist.size();i++){
            qDebug()<<qlist.at(i);  // 输出列表数据信息
        }
    }

    // 结构体类型和QVariant类配合使用
    qDebug()<<endl;
    student stu;
    stu.iNo=202221;
    stu.strName="sunny";
    stu.score=715;

    // 使用静态方法保存数据
    QVariant qstu=QVariant::fromValue(stu);

    if(qstu.canConvert<student>())
    {
        student temp=qstu.value<student>();         //获取数据
        student qtemp=qvariant_cast<student>(qstu); // 获取数据
        qDebug()<<"student:iNo="<<temp.iNo<<",strName="<<temp.strName<<".score="<<temp.score;
        qDebug()<<"student:iNo="<<qtemp.iNo<<",strName="<<qtemp.strName<<".score="<<qtemp.score;
    }

}

MainWindow::~MainWindow()
{
}
  • QVariant::fromValue()函数将自定义的student结构体对象stu转换为QVariant对象qstu。
  • qstu.canConvert()函数检查QVariant对象qstu是否可以转换为student类型。
  • student temp = qstu.value()将QVariant对象qstu转换为student类型,并将结果赋值给temp。
  • student qtemp = qvariant_cast(qstu)也可以将QVariant对象qstu转换为student类型,并将结果赋值给qtemp。
  • 最后,通过qDebug()输出转换后的student对象temp和qtemp的成员变量值。

结果如下:

相关推荐
qq_406981033 分钟前
什么是顶层 const什么是底层const
开发语言·c++
Eider199822 分钟前
python基础语法之并发基础练习
开发语言·python
#麻辣小龙虾#27 分钟前
网页布局css中flex+margin的组合布局技巧
开发语言·前端·javascript
云和数据.ChenGuang31 分钟前
python和java的try
java·开发语言·python
我是唐青枫31 分钟前
Kotlin Inheritance(继承)详解:语法、原理、实战 Demo
开发语言·kotlin
ly768942 分钟前
从 OpenAPI 到多语言 SDK 生成:Java 服务端契约漂移与生成器版本治理
java·开发语言·openapi·java 21·版本兼容·契约漂移·sdk 生成
lqbzlll1 小时前
CVE-2026-91403 排查指南:Java JSSE 信任链漏洞的根因、影响范围和修复步骤
java·开发语言·信任链
CarIise1 小时前
AOP 概念的引入
java·开发语言
凉茶钱1 小时前
多态:从虚函数语法到虚表寻址,一次讲透动态绑定
开发语言·c++·多态
Hello.Reader1 小时前
Three.js / Cesium 卡顿怎么办?把三维渲染搬到 GPU 服务器
服务器·开发语言·javascript