2017-10-19 173 views
0

我正在嘗試在Scene3D場景中呈現長方體或平面的臉部上的QML組件。我已經成功地跟隨了文檔中的示例,但我試圖將其轉換爲C++ API,因爲我需要處理更多的內容,只有C++就足夠了。這是我的類的ctor,它根據示例代碼設置實體及其組件。爲了簡潔,我只包含了ctor。班上沒有其他任何事情會影響這一點。Qt3D使用C++的QtQuick Scene2D

ESEctoPointToast::ESEctoPointToast(Qt3DCore::QNode *parent) 
: Qt3DCore::QEntity(parent) 
, m_position(QVector3D(0,0,0)) 
, m_quickItem(nullptr) 
, m_cuboid(new Qt3DExtras::QCuboidMesh()) 
, m_textureMaterial(new Qt3DExtras::QTextureMaterial()) 
, m_transform(new Qt3DCore::QTransform()) 
, m_objectPicker(new Qt3DRender::QObjectPicker()) 
, m_texture2d(new Qt3DRender::QTexture2D()) 
, m_renderTargetOutput(new Qt3DRender::QRenderTargetOutput()) 
, m_scene2d(new Qt3DRender::Quick::QScene2D()) 
{ 
    // g_RootQmlObject is the root item in the main scene, this was the only 
    // way I could come up with to access qmlEngine. Is there a better way? 
    auto engine = qmlEngine(g_RootQmlObject); 
    QQmlComponent c(engine, QUrl("qrc:/components/E3DDummy.qml")); 
    m_quickItem = qobject_cast<QQuickItem*>(c.create()); 
    Q_ASSERT(m_quickItem); 

    m_texture2d->setWidth(256); 
    m_texture2d->setHeight(256); 
    m_texture2d->setFormat(Qt3DRender::QAbstractTexture::TextureFormat::RGB8_UNorm); 
    m_texture2d->setGenerateMipMaps(true); 
    m_texture2d->setMagnificationFilter(Qt3DRender::QAbstractTexture::Filter::Linear); 
    m_texture2d->setMinificationFilter(Qt3DRender::QAbstractTexture::Filter::LinearMipMapLinear); 
    m_texture2d->setWrapMode(Qt3DRender::QTextureWrapMode(Qt3DRender::QTextureWrapMode::ClampToEdge)); 
    m_renderTargetOutput->setAttachmentPoint(Qt3DRender::QRenderTargetOutput::AttachmentPoint::Color0); 
    m_renderTargetOutput->setTexture(m_texture2d); 
    m_textureMaterial->setTexture(m_texture2d); 
    m_scene2d->setItem(m_quickItem); 
    m_scene2d->setMouseEnabled(true); 
    m_scene2d->setRenderPolicy(Qt3DRender::Quick::QScene2D::RenderPolicy::Continuous); 
    m_scene2d->setOutput(m_renderTargetOutput); 
    m_scene2d->addEntity(this); 

    addComponent(m_transform); 
    addComponent(m_textureMaterial); 
    addComponent(m_cuboid); 
    addComponent(m_objectPicker); 
} 

我將它包含在我的Scene3D中,從另一個類中渲染爲黑盒子,並將無意義的紅色文本擠壓在臉上。顯然這是不對的。我哪裏錯了?

這就是呈現: I'm getting a black box with red garbage text, however the text is the same every time.

+0

是現有的運行QQmlEngine的g_RootQmlObject部分?如果沒有,您可能需要啓動一個新的QQmlEngine。請參閱下面的「詳細描述」下的示例:http://doc.qt.io/qt-5/qqmlengine.html – dragly

+0

是的,g_RootQmlObject正在運行。不過,我會嘗試啓動一個新的QQmlEngine以防萬一。 –

+0

不幸的是,沒有。創建一個新的QQmlEngine會得到相同的結果。 –

回答