当前位置: 首页 > 知识库问答 >
问题:

Qt3D绘制线占用太多内存

上官兴昌
2023-03-14

如何用QT3D画线?如何删除画好的线条?我发现下面的代码会占用太多的内存,如果你画很多线,尽管它可以工作。此方法只为绘制一条线分配了太多的存储空间,并且不会释放这些存储空间。如果你使用删除指针,那么它崩溃了。如何解决这个问题?

#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DRender/QAttribute>
#include <Qt3DRender/QBuffer>
#include <Qt3DRender/QGeometry>

drawLine(const QVector3D &start, const QVector3D &end, const QColor &color)
{
    if(!m_bShow) {
        return;
    }

    auto *geometry = new Qt3DRender::QGeometry(m_pRootEntity);
    QByteArray bufferBytes;
    bufferBytes.resize(3*2*sizeof (float));
    float *pos = reinterpret_cast<float*>(bufferBytes.data());
    *pos++ = start.x();
    *pos++ = start.y();
    *pos++ = start.z();
    *pos++ = end.x();
    *pos++ = end.y();
    *pos++ = end.z();

    auto *buf = new Qt3DRender::QBuffer(geometry);
    buf->setData(bufferBytes);

    auto *positionAttribute = new Qt3DRender::QAttribute(geometry);
    positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
    positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
    positionAttribute->setVertexSize(3);
    positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
    positionAttribute->setBuffer(buf);
    positionAttribute->setByteStride(3 * sizeof(float));
    positionAttribute->setCount(2);
    geometry->addAttribute(positionAttribute); // We add the vertices in the geometry

    //connectivity between vertices
    QByteArray indexBytes;
    indexBytes.resize(2 * sizeof(unsigned int)); // start to end
    unsigned int *indices = reinterpret_cast<unsigned int*>(indexBytes.data());
    *indices++ = 0;
    *indices++ = 1;
    auto *indexBuffer = new Qt3DRender::QBuffer(geometry);
    indexBuffer->setData(indexBytes);

    auto *indexAttribute = new Qt3DRender::QAttribute(geometry);
    indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);
    indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
    indexAttribute->setBuffer(indexBuffer);
    indexAttribute->setCount(2);
    geometry->addAttribute(indexAttribute); // We add the indices linking the points in the geometry

    //mesh
    auto *line = new Qt3DRender::QGeometryRenderer(m_pRootEntity);
    line->setGeometry(geometry);
    line->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);

    //material
    auto *material = new Qt3DExtras::QDiffuseSpecularMaterial(m_pRootEntity);
    material->setAmbient(color);

    auto *lineEntity = new Qt3DCore::QEntity(m_pRootEntity);

    lineEntity->addComponent(line);
    lineEntity->addComponent(material);
}

共有1个答案

武彭薄
2023-03-14

最后,我解决了这个问题。
首先,将line实体放入容器:
m_LineEntityList.push_back(lineEntity),
然后移除line实体的组件:

 while(!m_lineEntityList.isEmpty()) {
    Qt3DCore::QEntity* pEntity = m_lineEntityList.last();
    Qt3DCore::QComponentVector entityVector = pEntity->components();
    while (!entityVector.isEmpty()) {
        pEntity->removeComponent(entityVector.last());
        entityVector.pop_back();
    }
    m_lineEntityList.pop_back();
}
 类似资料:
  • 我正在使用框架用iPhone摄像头捕捉视频,我的代码: 在之前,一切都很顺利,内存被限制在3M,但在之后,内存使用量每秒增加0.06M,几分钟后,应用程序会因为内存警告而崩溃。似乎占用了太多内存,并且可能存在内存泄漏问题。 那么如何减少内存使用量呢? iOS版本:7.1.1

  • 例如,我将-Xmx设置为40G。我希望我的java处理器不会使用超过40G。 我的程序与cms gc配合得很好。 但当我使用相同的内存(甚至多15%的内存)切换到G1 gc时。 它总是被杀人凶手杀死。 我发现了这样一篇文章:为什么我的Java进程比Xmx消耗更多的内存? 它表示: 所以我想知道,如何限制g1 gc使用的内存,以及为什么g1使用这么多额外的内存

  • 嗨,我对java编程还比较陌生。我编写的下面的程序似乎占用了很多内存(大约240 MB,正常吗?-我不这么认为!)请建议一些方法来优化这个程序,以便减少内存存储。 程序-- 代码-

  • MySQLdump和上传过程花费了太长的时间(~8小时)来完成整个过程。 我正在将活动数据库转储到mysqldump.tar文件和几乎3GB。当我加载到新的数据库,它需要6-8小时来完成这个过程(上传到新的数据库)。 我要完成这个过程,推荐的解决方案是什么?

  • 问题内容: 有没有办法找出我的Java线程在虚拟机中占用多少内存? 例如,使用堆栈跟踪转储或其他某种方式。 问题答案: Java线程将堆用作共享内存。各个线程都有其堆栈(您可以通过-Xss命令行选项设置其大小,默认为512KB),但是所有其他内存(堆)都不属于特定线程,并询问一个特定线程仅使用了多少内存没有道理。