Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Scene3D keyboard input
Forum Updated to NodeBB v4.3 + New Features

Scene3D keyboard input

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 382 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.
  • J Offline
    J Offline
    JanWillemsen
    wrote on last edited by JanWillemsen
    #1

    Hi,

    I'm trying to receive keyboard input in my Scene3D, but I fail to get it.
    I have an ApplicationWindow with a Scene3D and an Entity inside. On C++ side I have a QQmlApplicationEngine and QGuiApplication.

    Here's the code:

    Main.cpp:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
    	QGuiApplication app(argc, argv);
    
    	QQmlApplicationEngine engine;
    
    	engine.load(QUrl(QStringLiteral("qrc:/MainWindow.qml")));
    
    	return app.exec();
    }
    

    MainWindow.qml:

    import QtQuick 2.12
    import QtQuick.Controls 2.5
    
    import QtQuick.Scene3D 2.13
    import Qt3D.Core 2.0
    import Qt3D.Render 2.0
    import Qt3D.Input 2.0
    import Qt3D.Extras 2.0
    
    import QtQuick 2.0 as QQ2
    
    
    ApplicationWindow {
        id: mainApplicationWindow
    
        width: 640
        height: 480
    
        visible: true
    
        title: qsTr("TestTemplateProject")
    
        Scene3D {
            id: scene3D
            anchors.fill: parent
    
            aspects: ["input", "logic"]
            cameraAspectRatioMode: Scene3D.AutomaticAspectRatio
    
            //focus: true;
            Entity {
                id: sceneRoot
    
                Camera {
                    id: camera
                    projectionType: CameraLens.PerspectiveProjection
                    fieldOfView: 45
                    nearPlane: 0.1
                    farPlane: 1000.0
                    position: Qt.vector3d(0.0, 0.0, 40.0)
                    upVector: Qt.vector3d(0.0, 1.0, 0.0)
                    viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
                }
    
                FirstPersonCameraController {
                    camera: camera
                }
    
                KeyboardDevice { id: keyboard1 }
    
                KeyboardHandler {
                    id: input
                    sourceDevice: keyboard1
                    focus: true
    
                    onTabPressed: {
                        console.log("Tab");
                    }
    
                    onSpacePressed: {
                        console.log("Test");
                    }
                }
    
                RenderSettings {
                    id: renderSettings
                    activeFrameGraph: ForwardRenderer {
                        camera: camera
                    }
                }
    
                components: [ renderSettings, input, inputSettings ]
    
                InputSettings { id: inputSettings; }
    
                PhongMaterial {
                    id: material
                }
    
                TorusMesh {
                    id: torusMesh
                    radius: 5
                    minorRadius: 1
                    rings: 100
                    slices: 20
                }
    
                Transform {
                    id: torusTransform
                    scale3D: Qt.vector3d(1.5, 1, 0.5)
                    rotation: fromAxisAndAngle(Qt.vector3d(1, 0, 0), 45)
                }
    
                Entity {
                    id: torusEntity
                    components: [torusMesh, material, torusTransform]
                }
            }
        }
    
        Component.onCompleted:
        {
            console.log("Test");
            mainApplicationWindow.showMaximized();
        }
    }
    
    

    When I use a Qt3DQuickWindow on C++ side and just the Entity in qml (like in the Qt3D wave example), everything seems to work fine.
    What am I doing wrong?
    Regards,

    Jan

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Thomas Meilleroux
      wrote on last edited by
      #2

      Hi,

      You need to uncomment the line "//focus: true;".
      Focus is required to get keyboards events in your Scene3D.

      Regards,

      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