I am willing to pay for a help
How about just click "Thanks"?
OK, I got curious, so I decided to write a test program. I found an example PLY file online and used that, and then I hacked up the Qt3D Basic Shapes Example. Like you, I observed that after loading the mesh, the Status was "None" and the geometry() call returns a NULL pointer. However, I could see the mesh when I displayed the window. So I dug a little deeper and found that the Status does not become Ready and the geometry() is not valid until after the mesh is displayed.
Here is my complete example. I'm using Qt 5.15.3 but it should also work in Qt6:
Qt3DExample.h
#pragma once
#include <QtWidgets/QMainWindow>
#include <QEntity>
#include <QMesh>
{
Q_OBJECT
public:
Qt3DExample
(QWidget *parent
= Q_NULLPTR
);
protected:
private:
Qt3DRender::QMesh * mpMesh = nullptr;
Qt3DCore::QEntity * mpRootEntity = nullptr;
Qt3DCore::QEntity * mpMeshEntity = nullptr;
};
#pragma once
#include <QtWidgets/QMainWindow>
#include <QEntity>
#include <QMesh>
class Qt3DExample : public QMainWindow
{
Q_OBJECT
public:
Qt3DExample(QWidget *parent = Q_NULLPTR);
protected:
void keyPressEvent( QKeyEvent * pEvent );
private:
Qt3DRender::QMesh * mpMesh = nullptr;
Qt3DCore::QEntity * mpRootEntity = nullptr;
Qt3DCore::QEntity * mpMeshEntity = nullptr;
};
To copy to clipboard, switch view to plain text mode
Qt3DExample.cpp:
#include "Qt3DExample.h"
#include <QMesh>
#include <Qt3DWindow>
#include <QCamera>
#include <QInputAspect>
#include <QPointLight>
#include <QTransform>
#include <QFirstPersonCameraController>
#include <QPhongMaterial>
Qt3DExample
::Qt3DExample(QWidget *parent
){
// Root entity
mpRootEntity = new Qt3DCore::QEntity();
Qt3DExtras::Qt3DWindow * pView = new Qt3DExtras::Qt3DWindow();
pCentralWidget->setMinimumSize( 200, 200 );
pCentralWidget->setMaximumSize( 2000, 2000 );
resize( 1000, 1000 );
Qt3DInput::QInputAspect * input = new Qt3DInput::QInputAspect;
pView->registerAspect( input );
// Camera
Qt3DRender::QCamera * cameraEntity = pView->camera();
cameraEntity->lens()->setPerspectiveProjection( 45.0f, 16.0f / 9.0f, 0.1f, 1000.0f );
cameraEntity->setPosition( QVector3D( 0, 0, 20.0f ) );
cameraEntity->setUpVector( QVector3D( 0, 1, 0 ) );
cameraEntity->setViewCenter( QVector3D( 0, 0, 0 ) );
Qt3DCore::QEntity * lightEntity = new Qt3DCore::QEntity( mpRootEntity );
Qt3DRender::QPointLight * light = new Qt3DRender::QPointLight( lightEntity );
light->setColor( "white" );
light->setIntensity( 1 );
lightEntity->addComponent( light );
Qt3DCore::QTransform * lightTransform = new Qt3DCore::QTransform( lightEntity );
lightTransform->setTranslation( cameraEntity->position() );
lightEntity->addComponent( lightTransform );
// For camera controls
Qt3DExtras::QFirstPersonCameraController * camController = new Qt3DExtras::QFirstPersonCameraController( mpRootEntity );
camController->setCamera( cameraEntity );
// Set root object of the scene
pView->setRootEntity( mpRootEntity );
mpMesh = new Qt3DRender::QMesh();
mpMesh->setMeshName( "mesh" );
// Import mesh data from a ply file (Copperkey.ply located at same level of the executable)
QUrl url
= QUrl::fromLocalFile( "Copperkey.ply" );
mpMesh->setSource( url );
auto status = mpMesh->status(); // Returns None
auto pGeometry = mpMesh->geometry(); // Returns NULL
// Mesh Transform
Qt3DCore::QTransform * meshTransform = new Qt3DCore::QTransform();
meshTransform->setScale( 0.1f );
meshTransform->setRotation( QQuaternion::fromAxisAndAngle( QVector3D( 0.0f, 1.0f, 0.0f ), 25.0f ) );
meshTransform->setTranslation( QVector3D( 5.0f, 4.0f, 0.0f ) );
// Mesh material
Qt3DExtras::QPhongMaterial * meshMaterial = new Qt3DExtras::QPhongMaterial();
meshMaterial
->setDiffuse
( QColor( QRgb
( 0xbeb32b
) ) );
// Mesh entity
mpMeshEntity = new Qt3DCore::QEntity( mpRootEntity );
mpMeshEntity->addComponent( mpMesh );
mpMeshEntity->addComponent( meshMaterial );
mpMeshEntity->addComponent( meshTransform );
setCentralWidget( pCentralWidget );
}
void Qt3DExample
::keyPressEvent( QKeyEvent * pEvent
) {
auto status = mpMesh->status(); // Returns Ready
auto pGeometry = mpMesh->geometry(); // Returns a valid pointer
// Set a breakpoint on this line:
int foo = 42;
}
#include "Qt3DExample.h"
#include <QMesh>
#include <Qt3DWindow>
#include <QCamera>
#include <QInputAspect>
#include <QPointLight>
#include <QTransform>
#include <QFirstPersonCameraController>
#include <QPhongMaterial>
Qt3DExample::Qt3DExample(QWidget *parent)
: QMainWindow(parent)
{
// Root entity
mpRootEntity = new Qt3DCore::QEntity();
Qt3DExtras::Qt3DWindow * pView = new Qt3DExtras::Qt3DWindow();
QWidget * pCentralWidget = QWidget::createWindowContainer( pView );
pCentralWidget->setMinimumSize( 200, 200 );
pCentralWidget->setMaximumSize( 2000, 2000 );
resize( 1000, 1000 );
Qt3DInput::QInputAspect * input = new Qt3DInput::QInputAspect;
pView->registerAspect( input );
// Camera
Qt3DRender::QCamera * cameraEntity = pView->camera();
cameraEntity->lens()->setPerspectiveProjection( 45.0f, 16.0f / 9.0f, 0.1f, 1000.0f );
cameraEntity->setPosition( QVector3D( 0, 0, 20.0f ) );
cameraEntity->setUpVector( QVector3D( 0, 1, 0 ) );
cameraEntity->setViewCenter( QVector3D( 0, 0, 0 ) );
Qt3DCore::QEntity * lightEntity = new Qt3DCore::QEntity( mpRootEntity );
Qt3DRender::QPointLight * light = new Qt3DRender::QPointLight( lightEntity );
light->setColor( "white" );
light->setIntensity( 1 );
lightEntity->addComponent( light );
Qt3DCore::QTransform * lightTransform = new Qt3DCore::QTransform( lightEntity );
lightTransform->setTranslation( cameraEntity->position() );
lightEntity->addComponent( lightTransform );
// For camera controls
Qt3DExtras::QFirstPersonCameraController * camController = new Qt3DExtras::QFirstPersonCameraController( mpRootEntity );
camController->setCamera( cameraEntity );
// Set root object of the scene
pView->setRootEntity( mpRootEntity );
mpMesh = new Qt3DRender::QMesh();
mpMesh->setMeshName( "mesh" );
// Import mesh data from a ply file (Copperkey.ply located at same level of the executable)
QUrl url = QUrl::fromLocalFile( "Copperkey.ply" );
mpMesh->setSource( url );
auto status = mpMesh->status(); // Returns None
auto pGeometry = mpMesh->geometry(); // Returns NULL
// Mesh Transform
Qt3DCore::QTransform * meshTransform = new Qt3DCore::QTransform();
meshTransform->setScale( 0.1f );
meshTransform->setRotation( QQuaternion::fromAxisAndAngle( QVector3D( 0.0f, 1.0f, 0.0f ), 25.0f ) );
meshTransform->setTranslation( QVector3D( 5.0f, 4.0f, 0.0f ) );
// Mesh material
Qt3DExtras::QPhongMaterial * meshMaterial = new Qt3DExtras::QPhongMaterial();
meshMaterial->setDiffuse( QColor( QRgb( 0xbeb32b ) ) );
// Mesh entity
mpMeshEntity = new Qt3DCore::QEntity( mpRootEntity );
mpMeshEntity->addComponent( mpMesh );
mpMeshEntity->addComponent( meshMaterial );
mpMeshEntity->addComponent( meshTransform );
setCentralWidget( pCentralWidget );
}
void Qt3DExample::keyPressEvent( QKeyEvent * pEvent )
{
auto status = mpMesh->status(); // Returns Ready
auto pGeometry = mpMesh->geometry(); // Returns a valid pointer
// Set a breakpoint on this line:
int foo = 42;
}
To copy to clipboard, switch view to plain text mode
main.cpp:
#include "Qt3DExample.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
Qt3DExample w;
w.show();
return a.exec();
}
#include "Qt3DExample.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Qt3DExample w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Set a breakpoint on the line shown in the keypress event. Run the program. After the mesh appears on screen, type any key to hit the breakpoint.
You might have to change the scale factor and translation in the meshTransform to suit your PLY object.
Bookmarks