美文网首页my-QT专栏
QT QVariant支持结构体的方法

QT QVariant支持结构体的方法

作者: c之气三段 | 来源:发表于2021-09-11 16:14 被阅读0次

    类似与自定义类型容器

    struct DirectionalLight
    {
        float ambientFactor;
        float diffuseFactor;
        float specularFactor;
        QVector3D position;
        QVector3D color;
    };
    Q_DECLARE_METATYPE(DirectionalLight)//告诉qt支持结构体
    

    填充数据

       QList<QVariant> m_lightings;
       DirectionalLight dirLight;
       dirLight.ambientFactor = 0.2f;
       dirLight.diffuseFactor = 1.0f;
       dirLight.specularFactor = 0.5f;
       dirLight.color = QVector3D(1.0,1.0,1.0);
       dirLight.position = QVector3D(50.0,10.0,0.0);
       m_lightings.append(QVariant::fromValue(dirLight));//要使用这种方式初始化数据
    

    获取数据

           foreach (QVariant var, m_lightings) {
               if(var.canConvert<DirectionalLight>())
               {
                   DirectionalLight dirLight = var.value<DirectionalLight>();
                   m_pShaderProgram->setUniformValue("dirLight.ambientFactor",dirLight.ambientFactor);
                   m_pShaderProgram->setUniformValue("dirLight.diffuseFactor",dirLight.diffuseFactor);
                   m_pShaderProgram->setUniformValue("dirLight.specularFactor",dirLight.specularFactor);
                   m_pShaderProgram->setUniformValue("dirLight.color",dirLight.color);
                   m_pShaderProgram->setUniformValue("dirLight.position",dirLight.position);
               }
           }
    

    这样就能一个数组装多种结构体了

    相关文章

      网友评论

        本文标题:QT QVariant支持结构体的方法

        本文链接:https://www.haomeiwen.com/subject/afkawltx.html