1
votes

I have a situation where VolumeControl.qml should receive all the keys received by pages derived from Page1 (like Page2) and pages derived from Page2 (like Page3).

When I tried to forward the keys from all the pages to the VolumeControl (through id volumeControl), I am able to forward only from Page1, but getting reference error in Page2 and Page3 when trying to acess volume control id.

Why I am getting reference error in Page2 and Page3? How to resolve this to forward the keys from all individual pages to volume control, before the keys are consumed (event.accepted) by the pages?

Also Why I am not getting reference error in Page1?

VolumeControl.qml

import QtQuick 2.4

FocusScope 
{
    id: volumeControl
    focus: true  
    Keys.onPressed: 
    {
        console.error("VOLUME CTRL Key received" + event.key);
    }
}

Page1.qml

import QtQuick 2.4

Item 
{
    id: p1

    Keys.forwardTo: [volumeCtrl]

    VolumeControl 
    {
        id: volumeCtrl
        visible: true
        z: 60
    }
}

Page2.qml

import QtQuick 2.4

Page1
{

    id: p2

    Keys.forwardTo: [volumeCtrl]

    Keys.onDigit0Pressed: handleDigit("0")
    Keys.onDigit1Pressed: handleDigit("1")
    Keys.onDigit2Pressed: handleDigit("2")
    Keys.onDigit3Pressed: handleDigit("3")
    Keys.onDigit4Pressed: handleDigit("4")
    Keys.onDigit5Pressed: handleDigit("5")
    Keys.onDigit6Pressed: handleDigit("6")
    Keys.onDigit7Pressed: handleDigit("7")
    Keys.onDigit8Pressed: handleDigit("8")
    Keys.onDigit9Pressed: handleDigit("9")

    function handleDigit(digit) 
    {

    }
}

Page3.qml

import QtQuick 2.4

Page2
{
    id: p3

    Rectangle
    {
        width: 360
        height: 360

        focus:true

        Keys.forwardTo: [volumeCtrl]

        Keys.onPressed:
        {
            console.error("page3 Key received" + event.key);
        }
    }
}
1

1 Answers

1
votes

If I have correctly understood your question, you should add alias property for Page1 item. You getting reference error in Page2 and Page3 because volumeCtrl property or reference to it doesn't exist in these items directly.

  1. Add to your root item in Page1.qml:

    property alias volumeCtrlAlias1: volumeCtrl
    

    Now Page1 items will have a reference property to your VolumeControl item.

  2. Then in your Page2.qml change this:

    Keys.forwardTo: [volumeCtrl]
    

    to:

    Keys.forwardTo: [volumeCtrlAlias1]
    
  3. And finally in your Page3.qml change the same string to

    Keys.forwardTo: [p3.volumeCtrlAlias1]
    

Information about aliases from Qt Documentation here.