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的成员变量值。

结果如下:

相关推荐
luj_176819 分钟前
心形曲线轨迹控制三大关键技术
c语言·开发语言·c++·经验分享·算法
Mininglamp_27181 小时前
Claude Code 封禁中国开发者之后:本地 AI 编程工具的替代方案实测
开发语言·人工智能·windows·开源软件·ai-native
思麟呀1 小时前
C++17(三)if constexpr+折叠表达式
开发语言·c++
醉城夜风~1 小时前
Java详解经典算法题:接雨水(三种实现方案+原理剖析)
java·开发语言·算法
qydz111 小时前
杰理开发基础知识(3)
开发语言·嵌入式开发·杰理科技
贾斯汀frank1 小时前
C# 15 类型系统改进:Union Types
开发语言·windows·c#
阿里嘎多学长1 小时前
2026-07-10 GitHub 热点项目精选
开发语言·程序员·github·代码托管
CHHH_HHH2 小时前
【C++11】深入解析C++可变参数模板
开发语言·c++·算法·stl·c++11
jimy14 小时前
C语言模拟对象、方法:“函数指针+结构体“复用函数指针指向的函数体
c语言·开发语言
天外天-亮5 小时前
HBuilder X 使用 uview-plus 方式
开发语言·javascript·hbuilder x·uview-plus