Results 1 to 10 of 10

Thread: App crashes when I try to get the position attribute of a QMesh (Qt 5.15.2)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default App crashes when I try to get the position attribute of a QMesh (Qt 5.15.2)

    I try to get the position attribute of a mesh using the following code

    Qt Code:
    1. ....code...
    2. Qt3DRender::QMesh *mesh = new Qt3DRender::QMesh();
    3. mesh->setMeshName("mesh");
    4. // Import mesh data from a ply file (monster.ply located at same level of the executable)
    5. mesh->setSource(QUrl::fromLocalFile("monster.ply"));
    6.  
    7. // The call to mesh->geometry() crashes my app
    8. if (mesh->primitiveType() == Qt3DRender::QGeometryRenderer::Triangles)
    9. {
    10. for(int i = 0; i < mesh->geometry()->attributes().size(); ++i)
    11. {
    12. // To have access to data
    13. mesh->geometry()->attributes().at(i)->buffer()->setSyncData(true);
    14. }
    15.  
    16. // Now I need to get the position of the vertices of the triangles
    17.  
    18. Qt3DRender::QAttribute *positionAttribute = mesh->geometry()->attributes().at(0);
    19. ...more code...
    20. }
    To copy to clipboard, switch view to plain text mode 

    This is the content of the qmake script
    Qt Code:
    1. QT += 3dcore 3drender 3dinput 3dextras
    2. QT += widgets
    3.  
    4. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 


    I am using Qt5 LTS version.
    Am I forgetting some plugins or am I doing something wrong?
    I don't understand why I am getting such runtime error.

    Franco
    Last edited by franco.amato; 27th March 2023 at 19:59. Reason: updated contents
    Franco Amato

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,235
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: App crashes when I try to get the position attribute of a QMesh (Qt 5.15.2)

    So where does the debugger say it crashes?

    You have several places where you access member functions of "mesh" which return pointers. You don't check that those pointers are valid (non-null) before using them to dig further into the data structure.

    Qt Code:
    1. mesh->geometry()->attributes().size()
    2. mesh->geometry()->attributes().at(i)->buffer()->setSyncData(true)
    3. mesh->geometry()->attributes().at(0)
    To copy to clipboard, switch view to plain text mode 

    How do you know that the call to geometry(), attributes(), at(), or buffer() returns a valid pointer? Do some error checking, put in some asserts(), don't just assume everything works.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    franco.amato (27th March 2023)

  4. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: App crashes when I try to get the position attribute of a QMesh (Qt 5.15.2)

    I cannot call any method that belong to Qt3DRender::QGeometry
    The error occurs on the call QMesh::geometry();

    This code gives "Invalid pointer:
    Qt Code:
    1. auto *geom = mesh->geometry();
    2. if (geom == nullptr)
    3. {
    4. qDebug() << "Invalid pointer";
    5. return 1;
    6. }
    To copy to clipboard, switch view to plain text mode 

    I can open the same mesh with a non qt app without errors so it maybe it's a bug of my current Qt version?
    Franco Amato

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,235
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: App crashes when I try to get the position attribute of a QMesh (Qt 5.15.2)

    And what do you get as a Status if you call mesh->status() after assigning the source URL?

    And once you see that it is returning Error, then think about why QUrl (and your runtime environment) doesn't find your monster file.
    Last edited by d_stranz; 27th March 2023 at 23:19.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: App crashes when I try to get the position attribute of a QMesh (Qt 5.15.2)

    Quote Originally Posted by d_stranz View Post
    And what do you get as a Status if you call mesh->status() after assigning the source URL?
    And once you see that it is returning Error, then think about why QUrl (and your runtime environment) doesn't find your monster file.
    I changed the code to:

    Qt Code:
    1. QUrl data = QUrl::fromLocalFile("statue.ply");
    2.  
    3. Qt3DRender::QMesh *bodyMesh = new Qt3DRender::QMesh();
    4. bodyMesh->setMeshName("bodyMesh");
    5. bodyMesh->setSource(data);
    To copy to clipboard, switch view to plain text mode 

    And now it works (i don't know why)

    About the status:

    The following code:
    Qt Code:
    1. bodyMesh->setSource(data);
    2.  
    3. auto status = bodyMesh->status();
    4. if (status !=Qt3DRender::QMesh::Ready)
    5. {
    6. qDebug() << "An error occurred when loading the mesh.\n"
    7. << "Error code: " << status;
    8. }
    To copy to clipboard, switch view to plain text mode 

    Shows the output:
    An error occurred when loading the mesh.
    Error code: Qt3DRender::QMesh::None
    I don't why it's showing that error if I can see the mesh in the window
    Franco Amato

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,235
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: App crashes when I try to get the position attribute of a QMesh (Qt 5.15.2)

    I don't why it's showing that error if I can see the mesh in the window
    Weird. Don't know. Possibly the mesh hasn't finished (or even started) loading yet since there is no delay between the setSource() and status() calls.

    And now it works (i don't know why)
    Maybe the mesh needs to have a name property set on it first? Does it work if you replace "statue" with "monster" (with the monster file in the same location as the statue file)? If not, it could be that Qt's implementation of the PLY format reader doesn't support all of the elements that your external reader does.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: App crashes when I try to get the position attribute of a QMesh (Qt 5.15.2)

    Quote Originally Posted by d_stranz View Post
    Weird. Don't know. Possibly the mesh hasn't finished (or even started) loading yet since there is no delay between the setSource() and status() calls.
    Maybe the mesh needs to have a name property set on it first? Does it work if you replace "statue" with "monster" (with the monster file in the same location as the statue file)? If not, it could be that Qt's implementation of the PLY format reader doesn't support all of the elements that your external reader does.
    I have another model called "monster.ply". Changing the name in the setSource() method caused the other model to be loaded and I always get the same error
    Franco Amato

  9. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,235
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: App crashes when I try to get the position attribute of a QMesh (Qt 5.15.2)

    I have another model called "monster.ply". Changing the name in the setSource() method caused the other model to be loaded and I always get the same error
    But is it now working otherwise? You can access the geometry() without a crash? If so, then I wouldn't worry about the confusing error.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  10. #9
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    693
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: App crashes when I try to get the position attribute of a QMesh (Qt 5.15.2)

    Quote Originally Posted by d_stranz View Post
    But is it now working otherwise? You can access the geometry() without a crash? If so, then I wouldn't worry about the confusing error.
    I still get the crash adding the code I posted at the beginning.
    I really have to have it working because I have to populate a graph with the mesh vertex.
    I am willing to pay for a help
    Last edited by franco.amato; 30th March 2023 at 05:22.
    Franco Amato

  11. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,235
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: App crashes when I try to get the position attribute of a QMesh (Qt 5.15.2)

    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
    Qt Code:
    1. #pragma once
    2.  
    3. #include <QtWidgets/QMainWindow>
    4.  
    5. #include <QEntity>
    6. #include <QMesh>
    7.  
    8. class Qt3DExample : public QMainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. Qt3DExample(QWidget *parent = Q_NULLPTR);
    14.  
    15. protected:
    16. void keyPressEvent( QKeyEvent * pEvent );
    17.  
    18. private:
    19. Qt3DRender::QMesh * mpMesh = nullptr;
    20. Qt3DCore::QEntity * mpRootEntity = nullptr;
    21. Qt3DCore::QEntity * mpMeshEntity = nullptr;
    22. };
    To copy to clipboard, switch view to plain text mode 

    Qt3DExample.cpp:
    Qt Code:
    1. #include "Qt3DExample.h"
    2.  
    3. #include <QMesh>
    4. #include <Qt3DWindow>
    5. #include <QCamera>
    6. #include <QInputAspect>
    7. #include <QPointLight>
    8. #include <QTransform>
    9. #include <QFirstPersonCameraController>
    10. #include <QPhongMaterial>
    11.  
    12. Qt3DExample::Qt3DExample(QWidget *parent)
    13. : QMainWindow(parent)
    14. {
    15. // Root entity
    16. mpRootEntity = new Qt3DCore::QEntity();
    17.  
    18. Qt3DExtras::Qt3DWindow * pView = new Qt3DExtras::Qt3DWindow();
    19. QWidget * pCentralWidget = QWidget::createWindowContainer( pView );
    20. pCentralWidget->setMinimumSize( 200, 200 );
    21. pCentralWidget->setMaximumSize( 2000, 2000 );
    22. resize( 1000, 1000 );
    23.  
    24. Qt3DInput::QInputAspect * input = new Qt3DInput::QInputAspect;
    25. pView->registerAspect( input );
    26.  
    27. // Camera
    28. Qt3DRender::QCamera * cameraEntity = pView->camera();
    29.  
    30. cameraEntity->lens()->setPerspectiveProjection( 45.0f, 16.0f / 9.0f, 0.1f, 1000.0f );
    31. cameraEntity->setPosition( QVector3D( 0, 0, 20.0f ) );
    32. cameraEntity->setUpVector( QVector3D( 0, 1, 0 ) );
    33. cameraEntity->setViewCenter( QVector3D( 0, 0, 0 ) );
    34.  
    35. Qt3DCore::QEntity * lightEntity = new Qt3DCore::QEntity( mpRootEntity );
    36. Qt3DRender::QPointLight * light = new Qt3DRender::QPointLight( lightEntity );
    37. light->setColor( "white" );
    38. light->setIntensity( 1 );
    39. lightEntity->addComponent( light );
    40. Qt3DCore::QTransform * lightTransform = new Qt3DCore::QTransform( lightEntity );
    41. lightTransform->setTranslation( cameraEntity->position() );
    42. lightEntity->addComponent( lightTransform );
    43.  
    44. // For camera controls
    45. Qt3DExtras::QFirstPersonCameraController * camController = new Qt3DExtras::QFirstPersonCameraController( mpRootEntity );
    46. camController->setCamera( cameraEntity );
    47.  
    48. // Set root object of the scene
    49. pView->setRootEntity( mpRootEntity );
    50.  
    51. mpMesh = new Qt3DRender::QMesh();
    52. mpMesh->setMeshName( "mesh" );
    53.  
    54. // Import mesh data from a ply file (Copperkey.ply located at same level of the executable)
    55. QUrl url = QUrl::fromLocalFile( "Copperkey.ply" );
    56. mpMesh->setSource( url );
    57. auto status = mpMesh->status(); // Returns None
    58. auto pGeometry = mpMesh->geometry(); // Returns NULL
    59.  
    60. // Mesh Transform
    61. Qt3DCore::QTransform * meshTransform = new Qt3DCore::QTransform();
    62. meshTransform->setScale( 0.1f );
    63. meshTransform->setRotation( QQuaternion::fromAxisAndAngle( QVector3D( 0.0f, 1.0f, 0.0f ), 25.0f ) );
    64. meshTransform->setTranslation( QVector3D( 5.0f, 4.0f, 0.0f ) );
    65.  
    66. // Mesh material
    67. Qt3DExtras::QPhongMaterial * meshMaterial = new Qt3DExtras::QPhongMaterial();
    68. meshMaterial->setDiffuse( QColor( QRgb( 0xbeb32b ) ) );
    69.  
    70. // Mesh entity
    71. mpMeshEntity = new Qt3DCore::QEntity( mpRootEntity );
    72. mpMeshEntity->addComponent( mpMesh );
    73. mpMeshEntity->addComponent( meshMaterial );
    74. mpMeshEntity->addComponent( meshTransform );
    75.  
    76. setCentralWidget( pCentralWidget );
    77. }
    78.  
    79. void Qt3DExample::keyPressEvent( QKeyEvent * pEvent )
    80. {
    81. auto status = mpMesh->status(); // Returns Ready
    82. auto pGeometry = mpMesh->geometry(); // Returns a valid pointer
    83.  
    84. // Set a breakpoint on this line:
    85. int foo = 42;
    86. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include "Qt3DExample.h"
    2. #include <QtWidgets/QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Qt3DExample w;
    8. w.show();
    9. return a.exec();
    10. }
    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.
    Last edited by d_stranz; 30th March 2023 at 19:59.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Attribute files
    By SirJonas in forum General Programming
    Replies: 1
    Last Post: 2nd November 2016, 20:24
  2. Replies: 0
    Last Post: 1st August 2016, 08:51
  3. How to set an attribute without affecting children?
    By dominate in forum Qt Programming
    Replies: 0
    Last Post: 15th February 2011, 14:58
  4. Updating xml attribute
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 17th July 2007, 09:26
  5. Replies: 1
    Last Post: 22nd January 2007, 09:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.