my-QT专栏

QT QVariant支持结构体的方法

2021-09-11  本文已影响0人  c之气三段

类似与自定义类型容器

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);
           }
       }

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

上一篇下一篇

猜你喜欢

热点阅读