You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用NodeInstantiator实例化Scene2D时点击事件不触发

QtWayland 3D Compositor: Scene2D Entities Not Binding to Event Handlers with NodeInstantiator

Let’s break down why your Scene2D’s entities property isn’t connecting to the internal event handlers when using NodeInstantiator, and fix it step by step.

Core Issues to Diagnose

From your code, the main problem likely stems from timing or context mismatches when initializing the Scene2D’s entities property:

  1. When Scene2D initializes, the parent reference might not yet point to the fully initialized Panel (your Entity), especially since NodeInstantiator creates components lazily.
  2. Static binding like entities: [parent] doesn’t trigger updates if the parent context changes after Scene2D is created.
  3. The Scene2D may not be detecting changes to the entities property at all, so it never runs its internal event setup logic.

Fix 1: Explicitly Bind to the Panel Entity (Not Parent)

Instead of relying on parent (which can be ambiguous during initialization), directly reference the Panel’s id and set entities after the component is fully loaded. Update your Panel.qml:

Entity {
    id: panel
    components: [panelTransform, planeMesh, material, panelPicker]

    Scene2D {
        id: waylandScene
        output: RenderTargetOutput { ... }
        mouseEnabled: true
        // Remove the static entities: [parent] line
        ShellSurfaceItem { ... }

        Component.onCompleted: {
            // Explicitly set entities to the Panel Entity once it's fully initialized
            entities = [panel]
        }
    }
}

This ensures you’re targeting the exact Entity you want, and the assignment happens after both the Panel and Scene2D are ready.

Fix 2: Use a Binding Component for Dynamic Updates

If you need entities to react to changes (e.g., if the Panel’s parent changes later), use a Binding component to keep it in sync:

Entity {
    id: panel
    components: [panelTransform, planeMesh, material, panelPicker]

    Scene2D {
        id: waylandScene
        output: RenderTargetOutput { ... }
        mouseEnabled: true
        ShellSurfaceItem { ... }
    }

    // Bind entities to the Panel Entity, only when the Panel exists
    Binding {
        target: waylandScene
        property: "entities"
        value: [panel]
        when: panel !== null
    }
}

This guarantees the entities property updates whenever the panel reference is valid.

Fix 3: Ensure NodeInstantiator Sets Correct Parenting

Make sure the Panel instances created by NodeInstantiator have a valid 3D scene parent. If they’re not attached to your 3D root, the Scene2D won’t be able to register events properly:

NodeInstantiator {
    model: shellSurfaces
    // Set the parent to your 3D scene root Entity
    parent: yourSceneRootEntity
    Panel {
        id: panel
        shellSurf: modelData
        listIndex: index
    }
}

Debugging Tips to Verify Fixes

Add these logs to confirm the setup works:

  1. In Panel.qml’s Component.onCompleted:
    Component.onCompleted: {
        console.log("Panel is Entity:", panel instanceof Entity)
        console.log("Scene2D entities post-setup:", waylandScene.entities)
    }
    
  2. Listen for Scene2D’s entitiesChanged signal to confirm updates:
    Scene2D {
        id: waylandScene
        ...
        onEntitiesChanged: {
            console.log("Entities updated:", entities)
            // If your Scene2D has internal event setup logic, trigger it here
            // e.g., waylandScene.setupEventHandlers()
        }
    }
    

These steps should ensure your Scene2D properly registers the Panel Entity and connects to its internal event handlers.

内容的提问来源于stack exchange,提问作者technobaboo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:09:04