Search code examples
c++qmlblackberry-10

BB 10 C++ Button on click


In QML you can do the on click event. I am writing my qml in C++, however there is no onclick method.

How do you get the on click to work.

Button *btnSave = new Button();
btnSave->setText("Save");
contentContainer->add(btnSave);

Does anyone have a simple example that they could provide to get me started?

Thanks in advance.


Solution

  • To do this in C++, you have to connect a signal (in this case, is the Button's clicked() to a slot of your class). You better take a look here.

    Supposing you defined a slot called onSaveButtonClicked() in your header, that will be called when your button is clicked:

    public slots:
        void onSaveButtonClicked();
    

    in your application class, after create your button, you'd have to do:

    connect(btnSave, SIGNAL(clicked()), this, SLOT(onSaveButtonClicked()));
    

    When your button is clicked, it will emit the signal that will call the slot function.