Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to click (pick) a 3D object?
Forum Updated to NodeBB v4.3 + New Features

How to click (pick) a 3D object?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 291 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Anton1978A Offline
    Anton1978A Offline
    Anton1978
    wrote on last edited by
    #1

    Good afternoon
    I’m experimenting with 3D, everything seems to be correct, the click works, but the object is not located on the 3D scene.

    import QtQuick
    import QtQuick3D
    import QtQuick3D.Helpers
    
    
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        View3D{
            id: viewport
            anchors.fill: parent
            renderMode: View3D.Underlay
            environment: SceneEnvironment{
                clearColor: "#222222"
                backgroundMode: SceneEnvironment.SkyBox
                lightProbe: Texture{
                    source: "qrc:\\res\\heaven.png"
                }
                probeExposure: 0.75
            }
    
            Model{
                position: Qt.vector3d(0,0,0);
                scale: Qt.vector3d(1,1.25,1);
                source: "#Cone"
                objectName: "Cone"
                pickable : true
                materials:[PrincipledMaterial{
                        baseColor: "green"
                        metalness: 0.8
                        roughness: 0.3
                    }]
            }
            Model{
                position: Qt.vector3d(80,0,50)
                source: "#Sphere"
                objectName: "Sphere"
                pickable : true
                materials: [PrincipledMaterial{baseColor: "cyan"}]
            }
            Model{
                position: Qt.vector3d(50,50,-150)
                source: "#Rectangle"
                objectName: "Rectangle"
                pickable: true
                materials: [PrincipledMaterial{baseColor:"red"}]
            }
    
            Head{
                id: headID
    
            }
    
            DirectionalLight{
                eulerRotation.x: -20
                eulerRotation.y: 110
                castsShadow: true
            }
    
            PerspectiveCamera{
                id: persCam
                position: Qt.vector3d(0,300,350)
                Component.onCompleted: {
                    lookAt(Qt.vector3d(0,0,0))
                }
            }
    
        }
    
        MouseArea{
            anchors.fill: viewport
            onClicked: (mouse) => {
                var result = viewport.pick(mouse.x,mouse.y);
                console.log(result);
                console.log(result.distance);
                console.log(result.objectHit.objectName);
    
                if (result.objectHit) {
                    var pickedObject = result.objectHit;
                    console.log(pickedObject.objectName);
                }
            }
        }
    }
    

    Output to the console after clicking

    qrc:/qt/qml/Test05/Main.qml:79: TypeError: Cannot read property 'objectName' of null
    qml: QQuick3DPickResult(, 0, , , , , , 0)
    qml: 0
    qrc:/qt/qml/Test05/Main.qml:79: TypeError: Cannot read property 'objectName' of null
    qml: QQuick3DPickResult(, 0, , , , , , 0)
    qml: 0
    qrc:/qt/qml/Test05/Main.qml:79: TypeError: Cannot read property 'objectName' of null
    

    Apparently I'm missing something, but I can't understand what

    1 Reply Last reply
    0
    • Anton1978A Offline
      Anton1978A Offline
      Anton1978
      wrote on last edited by
      #2

      the question is resolved - you can delete it, the problem is that for some reason the model 3d inside Node does not respond to clicks. I will find out why.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mv-whw
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved