c++ - Qt3D:关于QText2DEntity的几个简单问题

标签 c++ qt qt3d

  1. 下面列出的是一个简单的测试应用程序。 如果运行此应用程序,会出现消息“[Qt3DRender::GLTexture] 尚未从纹理生成器生成 QTextureData。纹理对此帧无效”,并在退出时 - 应用程序崩溃。 创建 QText2DEntity 的错误是什么?如果对标记的片段进行注释,那么就不会有问题。

  2. 如何将 QText2DEntity 附加到相机(或屏幕)?我需要在移动相机时使 QText2DEntity 始终保持在固定位置。

ma​​in.cpp

#include <QGuiApplication>
#include <Qt3DCore/QEntity>
#include <Qt3DCore/QTransform>
#include <Qt3DExtras/Qt3DWindow>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QText2DEntity>
#include <Qt3DExtras/QFirstPersonCameraController>
#include <Qt3DRender/QCamera>

int main(int argc, char *argv[])
{
    QGuiApplication application(argc, argv);
    Qt3DExtras::Qt3DWindow window;

    auto scene = new Qt3DCore::QEntity;
    window.setRootEntity(scene);

    auto sphere = new Qt3DCore::QEntity(scene);

    auto transform = new Qt3DCore::QTransform;
    transform->setTranslation(QVector3D(0.0f, 0.0f, -10.0f));

    auto material = new Qt3DExtras::QPhongMaterial;
    material->setAmbient(QColor(245, 245, 245));
    material->setDiffuse(QColor(125, 125, 125));
    material->setSpecular(QColor(215, 215, 215));

    auto spheremesh = new Qt3DExtras::QSphereMesh;
    spheremesh->setRadius(15.0);
    spheremesh->setSlices(32);
    spheremesh->setRings(32);

    sphere->addComponent(transform);
    sphere->addComponent(material);
    sphere->addComponent(spheremesh);

    // QText2DEntity
    auto text2D = new Qt3DExtras::QText2DEntity(scene);
    text2D->setFont(QFont("monospace",5));
    text2D->setHeight(10.0);
    text2D->setWidth(20.0);
    text2D->setText("Test");
    text2D->setColor(Qt::yellow);

    auto text2dTransform = new Qt3DCore::QTransform;
    text2dTransform->setTranslation(QVector3D(-10.0f, 0.0f, 50.0f));
    text2D->addComponent(text2dTransform);
    //

    auto camera = window.camera();
    camera->lens()->setPerspectiveProjection(60.0f, static_cast<float>(window.width()) / window.height(), 0.1f, 1000.0f);
    camera->setPosition(QVector3D(0.0f, 0.0f, 100.0f));
    camera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));

    auto camController = new Qt3DExtras::QFirstPersonCameraController(scene);
    camController->setCamera(camera);

    window.show();
    return application.exec();
}

测试.pro

QT       += core 3dlogic 3dextras 3dinput

CONFIG += c++17

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
    main.cpp

HEADERS +=

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

代码在 Qt 5.13.1 上测试

最佳答案

Stackoverflow in QML 有一个例子,您可以在其中看到 Text2Dentity 在另一个实体中使用。

Entity {
        id: textFront
        components: [ Transform { translation: Qt.vector3d(-12.5,-5,-20) } ] 

        Text2DEntity {
            font.family: "Sans Serif"
            font.pointSize: 3
            color: "white"
            text: "textFront"
            width: text.length * font.pointSize*2
            height: font.pointSize * 4
        }
    }

关于c++ - Qt3D:关于QText2DEntity的几个简单问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58434005/

相关文章:

c++ - 在最终剪辑中支持表格/网格小部件吗?

c++ - C++中堆栈的异常处理

c++ - 为 Windows 安装 Qt-5

python - PyQt5 中包含的 3D 窗口

python - PySide2 Qt3D 网格不显示

c++ - 删除框架图时 Qt3D 崩溃

c++ - 如何在 CPP 实现中使用 MT(或类似)RNG 算法?

c++ - 使用 p/invoke 将字符串数组从 C# 编码到 C 代码

c++ - 使用 Visual Studio 构建 QT

c++ - 是否有一种跨平台的方式来获取正在运行的应用程序列表?