0

I am developing one Calculator app in BlackBerry 10 and for that I am using switch case to get the button ID but I don't know how to get the ID of button in switch case.

For Example:

switch(?){                                    
     case button_addition:

     case button_minus:  

     case button_multiplication:

     case button_division:                                     
}

Where button_addition, button_minus, button_multiplication and button_division are the ID's of the buttons.

How do I get IDs of these buttons?

Sunseeker
  • 1,503
  • 9
  • 21

3 Answers3

1

I am not sure if you can get ID like that, I can suggest you to define custom property in button, and pass that on button click event. Then you can use that property to identify the event.

Like following

Button{
    id: button_Addidtion
    property var customId: 1 // for addition
    onClick:{
         handleOperation(customId);
    }
}

function handleOperation(id) {
    switch(id) {
        case 1://addition
        break;
    }
}

If you don't want to define custom property then maybe you can use objectName property to identify the button, I am not sure if you can use string with switch with JS.

Kunal
  • 3,475
  • 21
  • 14
  • I don't know if you can use it from QML, but in C++, in every Qt slot, you can call ``QObject::sender()``, which will return a pointer to the ``QObject`` who emited the signal who triggered the slot. So you can compare it to everything you want... – Marc Plano-Lesay Aug 20 '13 at 14:45
  • Hey thanx to all.. program is running now. – BlackBerry Kida Aug 29 '13 at 12:38
0

I suggest you look into the container that holds the buttons. In many windowing systems, the containers have a "for each" method in which you can supply a function that operates on each button.

Or when a button is pressed (or clicked), check the message. Many windowing messages contain the ID of the widget that sent the message.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

Easy in QML, You can create a JS function which take the button ID clicked as an input parameter and do the switch case based on that parameter. You would need to call this JS function and pass the button ID on the button clicked signal.

function handleMultiButton(buttonID) {
    switch(buttonID) {
        case "button_Addidtion": //Do Something
        break; 
        case "button_division":  //Do Something
        break;
    }
 }

Button{
    id: button_Addidtion
    onClick:{
        handleMultiButton("button_Addidtion");
    }
} 

Button{
     id: button_division
     onClick:{
        handleMultiButton("button_division");
     }
}

etc.