按下wxYES_NO中的按钮时,如何执行功能? [英] How can I execute the function when button in wxYES_NO is pressed?

查看:203
本文介绍了按下wxYES_NO中的按钮时,如何执行功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我的头衔不清楚,所以在这里我将给出更准确的解释:

Maybe my title is unclear, so I will tell here a more precise explanation:

我正在学习WxWidgets,现在正在尝试制作两个文件:main.cpp和Quit.h. Main.cpp将具有应用程序的核心,而Quit.h将具有退出对话框的类:您是否真的要退出此应用程序(是/否).

I am just learning WxWidgets, and I am now trying to make two files: main.cpp and Quit.h. Main.cpp will have the core of the application, and Quit.h will have the class for the quit dialog: Do you really want to quit this application (Yes / No).

现在这是我的Quit.h文件(没有include部分):

Now this is my Quit.h file (without include part):

class Quit : public wxFrame
{
public:
    Quit(const wxString& tekst);
};
Quit::Quit(const wxString& tekst)
{
    wxMessageDialog* dial = new wxMessageDialog(NULL, _("Do you really want to quit?"), _("Quit"), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
    dial->ShowModal();
}

在这里我被困住了.我尝试使用wxDECLARE_EVENT_TABLE(),但我不知道哪个事件代表此情况:按按钮(在wxYES_NO按钮系统中)".我不能说:按下wxYES_NO,因为这是两个按钮(是和否).

And here I am stuck. I tried with wxDECLARE_EVENT_TABLE(), but I don't know which event stands for this: "on the press of yes button (in wxYES_NO system of buttons)". I can't say: on the press of wxYES_NO because these are two buttons (both YES and NO).

那么当按下是"按钮时如何执行该功能?

So how can I execute the function when the button YES is pressed?

谢谢!

P.S. 对于这个不清楚的问题,我深表歉意,但希望您能理解.请注意,我只是一个初学者,所以请不要在答案中使用太多技术性"字眼和表达方式.我阅读了文档,但是它使用了许多技术表达和解释.另外,我阅读了这本书.

P.S. I really apologize for this unclear question, but I hope, that you'll understand. Note that I am just a beginner, so please don't use so many "technical" words and expressions in the answer. I read the documentation, but it uses so many technical expressions and explanation. Also, I read this book.

P.P.S. 您是否注意到SE上现在有很多问题,而即将推出COVID-19?

P.P.S. Have you noticed that there are a lot of questions on SE now, while there is COVID-19 on its way?

:在打开程序时,出现了另一个错误.最少的代码:

When I was making the program on, I came to the other error. Minimal code:

Quit.h

class Quit : public wxFrame
{
public:
    Quit(const wxWindow* parent, const wxString& text);
};

Quit::Quit(const wxWindow* parent, const wxString& text)
{
    int dialog_return_value = wxNO;
    wxMessageDialog* dial = new wxMessageDialog(NULL, text, _("Exit"), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
    dialog_return_value = dial->ShowModal();

    switch (dialog_return_value)
    {
    case wxYES:
        Close(true);
        break;
    case wxNO:
        Close(false);
        break;
    default:
        Close(false);
    };
}

然后在main.cpp中有这行:

and then I have this line in main.cpp:

void MyFrame::CloseWindow(wxCommandEvent& event)
{
    Quit* quit = new Quit(this, _("Do you really want to close the App?"));
}

然后它不起作用.我找不到解决方案,因此,如果您有空,请帮忙.

And then it doesn't work. I can't find the solution, so, if you have some time, please help.

再次感谢您!

推荐答案

我建议使用wxEvtHandler::Bind<>()函数,如

I would suggest using the wxEvtHandler::Bind<>() function as detailed in the wxWidgets documentaton at https://docs.wxwidgets.org/3.0/overview_events.html. The Bind() function allows dynamic binding of events and the syntax is one line of code as compared to setting up tables to link events to objects.

另外请参阅此wxWidgets用户论坛线程,其中包含有关调用成员和非成员方法的详细说明 https://forums.wxwidgets.org/viewtopic.php?t=39817

Additionally see this wxWidgets user forum thread which has detailed explanation for calling member and nonmember methods https://forums.wxwidgets.org/viewtopic.php?t=39817

wxYES_NO是一个样式标志,它告诉wxWidgets框架您希望对话框中的是和否按钮.检查ShowModal()的返回值是否等于定义为wxYESwxNO的内置宏之一.

wxYES_NO is a style flag that tells wxWidgets framework that you want both yes and no buttons in your dialog. Check if the return value of ShowModal() is equal to one of the builtin macros defined as wxYES and wxNO.

有关宏的定义,请参见此处 https://docs.wxwidgets.org/trunk/defs_8h .html

See here for the macro definitions https://docs.wxwidgets.org/trunk/defs_8h.html

您应该阅读wxDiaglog.从此处开始 https://docs.wxwidgets.org/trunk/classwx_dialog.html

And you should read up on wxDiaglog. Start here https://docs.wxwidgets.org/trunk/classwx_dialog.html

是否要将值返回给Quit::Quit()的调用者?构造函数不返回值,您可以将成员变量设置为该值,但是请记住,如果对象被破坏,那么您的成员变量也将消失.您没有提供足够的信息来知道Quit()时需要执行的清理工作,因此,我将为您提供检查返回值的代码,只需在案例正文中填写所需的内容即可.

Do you want to return the value to the caller of Quit::Quit()? Constructors do not return values, you can set a member variable to the value but remember that if the object is destroyed then your member variable is gone too. You have not provided enough information to know what needs to be done for cleanup when you Quit() so I will provide you with the code to check the return value, just fill in what you need in the case body.

以下是检查返回值的方法:

Here is how you would check the return value:

class Quit : public wxFrame
{
public:
    Quit(const wxString& tekst);
};
Quit::Quit(const wxString& tekst)
{
    int dialog_return_value = wxNO; // initialize to a sane default value
    wxMessageDialog* dial = new wxMessageDialog(NULL, _("Do you really want to quit?"), _("Quit"), wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
    dialog_return_value = dial->ShowModal();
    // You do not have cancel button so only check wxYES and wxNO
    switch( dialog_return_value) // Use switch, scales to more buttons later
    {
        case wxYES :
        /* do something */
        break;
        case wxNO :
        /* do something */
        break;
        default : /* ignore or handle error */ ;
    };
}

您正在执行一项技术任务,因此可以合理预期将学习技术"字词.

You are doing a technical task, it is reasonable to expect that learning "technical" words will be involved.

这篇关于按下wxYES_NO中的按钮时,如何执行功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆