QML signal executed twice
Asked Answered
D

2

6

I'm new in QML and QML signals and I'm having this silly problem that I'm not being able to resolve by myself. I'm triggering an onTouch signal and is executing twice, generating a double response that crashes my app.

Here's my QML code:

//LabelKey.qml

import bb.cascades 1.0

Container {

    property string labelText: "#"
    property real width: 153.3
    property real height: 102.5
    property int labelPosX: 60
    property int labelPosY: 25
    property int labelTextFontWidth: 45
    property string imgSrc: "asset:///images/keyboard_button.png"

    layout: AbsoluteLayout {
    }
    preferredWidth: width
    preferredHeight: height
    objectName: "contTecla"
    id: contTecla
    ImageView {
        objectName: "imgTecla"
        id: imgTecla1
        imageSource: imgSrc
        preferredWidth: width
        preferredHeight: height
        onTouch: {
            textFieldKey.text = textFieldKey.text + labelTecla.text;
        }
    }
    Label {
        objectName: "labelTecla"
        id: labelTecla
        text: labelText
        textStyle {
            color: Color.DarkYellow
            size: labelTextFontWidth
        }
        layoutProperties: AbsoluteLayoutProperties {
            positionX: labelPosX
            positionY: labelPosY
        }
    } 
}

I have this TextField whose id is textFieldKey in another QML where I'm including the one I post above. The main idea is simple, is a keyboard where each key is a component of the code above and it has to print the value of the key pressed in this TextField.

The problem is, as I said, the signals is being called twice, filling the TextField with two chars of the value each time.

Please help me I don't know if maybe I'm missing something in the proper way of using signals or something like that.

Thanks!

Domesticity answered 26/9, 2012 at 14:4 Comment(2)
I figure it out. The touch signals has 4 differents states: Down: Occurs when the user touches the screen. Move: Occurs when the user moves a finger on the screen. Up: Occurs when the user releases a finger. Cancel: Occurs when an interaction is canceled. Each one identify with a number from 0 to 3. And when a touch signal is triggered two states are involved, Down and Up. You just need to make sure with wich one you want to work with and catch it inside the onTouch signal: if (event.touchType == numberOfTheTouchState){ }Domesticity
Good job solving the problem. On Stack Overflow the thing to do when you solve your own problem is write a proper answer and then accept it. This helps people who may have the same problem find the solution, and closes of the question.Dupion
D
2

I figure it out. The touch signals has 4 differents states:

  • Down: Occurs when the user touches the screen.

  • Move: Occurs when the user moves a finger on the screen.

  • Up: Occurs when the user releases a finger.

  • Cancel: Occurs when an interaction is canceled.

Each one identify with a number from 0 to 3.

And when a touch signal is triggered two states are involved, Down and Up. You just need to make sure with wich one you want to work with and catch it inside the onTouch signal:

if (event.touchType == numberOfTheTouchState){
}
Domesticity answered 27/9, 2012 at 13:12 Comment(0)
A
0

You want to use

ImageView
{
    objectName: "imgTecla"
    id: imgTecla1
    imageSource: imgSrc
    preferredWidth: width
    preferredHeight: height
    onTouch:
    {
        if(event.isDown())
        {
            textFieldKey.text = textFieldKey.text + labelTecla.text;
        }
    }
}

As was noted, without this you get both the up and down events

Accouchement answered 21/11, 2012 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.