Qt3D: a few simple questions about QText2DEntity

Viewed 709
  1. Listed below is a simple test application. If to run this application, appears message "[Qt3DRender::GLTexture] No QTextureData generated from Texture Generator yet. Texture will be invalid for this frame", and on exit - the application crashes. What is the mistake of creating QText2DEntity? If to comment the marked fragment, then there will be no problems.

  2. How to attach QText2DEntity to camera (or screen)? I need to make that when moving the camera QText2DEntity remained always at fixed place.

main.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();
}

test.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

code tested at Qt 5.13.1

1 Answers

There is an example on Stackoverflow in QML, where you can see that Text2Dentity is used in another Entity.

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
        }
    }
Related