如何使用方法连接按钮,这些方法将在单击时调用

时间:2017-09-26 14:20:09

标签: c++ oop events methods

我开始编写一些自定义gui应用程序。但我需要知道如何使用某种方法连接按钮。
例如我有:

class Button
{
private:
    string Text;
    /*etc*/
public:
    string GetLabel();
    void SetLabel(string label);
    void Connect(/*method name as param*/)
    {
        //TODO -this is that thing i need help here
        /*this is method for connecting button with method, which will be
          called on click, which im asking for help*/
    }
};

class Window 
{
private:
    int sizeX,sizeY;
public:
    void Put(Button * button,int _hpos,int _vpos);
    void Show();
    /*etc */
};

class MyWindow : public Window
{
public:
    MyWindow()
    {
        InitializeComponents();
    }
    void Run()
    {
        this -> Put(btn1,10,10); //put button in window at position 10,10
        this -> Show();
    }
private:
    Button * btn1;
    void InitializeComponents()
    {
        btn1 = new Button();
        btn1 -> Connect(on_btn1_click); //select which method will be called when user click this button, which I dont know how to do so pls help
    }
    void on_btn1_click()  //this method is called when user click the button
    {
        //do something if user clicked btn1
    }

};

int main()
{
    MyWindow * win1 = new MyWindow();
    win1 -> Run();
    return 0;
}

因此MyWindow类中有私有方法,当用户单击按钮(btn1)时将调用该方法。方法Connect()选择当用户单击按钮时将使用哪种方法进行调用。

1 个答案:

答案 0 :(得分:0)

您可以检查鼠标是否在按钮/ UI对象上,如果它不是触发命令/事件。

JSON.stringify