首页 > 解决方案 > 如何在我自己的 C++ 应用程序中显示简单(但自定义)对话框

问题描述

我正在寻找简单的解决方案,通过使用对话框从用户那里获取一些数据。

我不想使用 MFC,因为它应该是跨平台的。我已经阅读并运行了一些 QT 和 wxWidgets 的示例,但问题是它们使用自己的“自己的”主入口点,所以我不知道如何在我的应用程序中组合它们。

这是我正在尝试做的一个例子:

int main()
{
    std::cout << "Please choose the dialog! Press 1 or 2" << std::endl;
    int key = 0;
    std::cin >> key;

    if (key == 1)
    {
        Dialog1 dlg; //This is my question! how to do that?
        dlg.show();

        //Get data drom dialog:
        std::string str1 = dlg.GetData(ID_EDIT_BOX_1);
        std::string str2 = dlg.GetData(ID_EDIT_BOX_2);
    }

    if (key == 2)
    {
        Dialog2 dlg;
        dlg.show(); //Show simple dialog with some edit boxes

        //Get data drom dialog:
        std::string str1 = dlg.GetData(ID_EDIT_BOX_1);
        std::string str2 = dlg.GetData(ID_EDIT_BOX_2);
    }
}

我的意思是我知道如何创建和调用对话框,但不知道如何在我的应用程序中组合它们并在特定行中调用它们......

谢谢。

标签: c++qtuser-interfacecross-platformwxwidgets

解决方案


使用 wxWidgets,您可以使用 初始化库wxInitialize(),使用wxDialog::ShowModal()来显示您的对话框,然后使用关闭它wxUninitialize()并根据需要重复。如果您需要显示许多对话框,每次都这样做显然不是很有效,但是如果您真的想制作一个完全独立的函数来询问用户的值,您可以这样做。


推荐阅读