首页 > 解决方案 > 单击按钮后,ImGui 冻结

问题描述

我在我的 UI 应用程序中使用 ImGui。我的问题是当我按下按钮时,'if condition' 中的代码将执行。但是一旦我按下按钮,我就无法按下其他按钮,包括按下的按钮。任何人都可以让我知道是什么问题吗?

示例代码:

while(window)

{
    // Poll and handle events (inputs, window resize, etc.)
    // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
    // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
    // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
    // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
    glfwPollEvents();

    // Start the Dear ImGui frame
    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
    if (show_demo_window)
        ImGui::ShowDemoWindow(&show_demo_window);

    // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
    {
        static float f = 0.0f;
        static int counter = 0;

        ImGui::Begin("Hello, world!");                          // Create a window called "Hello, world!" and append into it.

        ImGui::Text("This is some useful text.");               // Display some text (you can use a format strings too)
        ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our window open/close state
        ImGui::Checkbox("Another Window", &show_another_window);

        ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f
        ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color

        if (ImGui::Button("Button"))                            // Buttons return true when clicked (most widgets return true when edited/activated)

        {

            for (int i = 0; i < 1000000; i++)

            {
                cout << i;
            }
           
        }

标签: c++user-interfaceopenglglfwimgui

解决方案


在您按下按钮的那一刻,相应if语句中的所有内容都会被执行。i这意味着在成功打印所有 s 之前不会更新您的用户界面。完成此操作后,您的程序将返回执行 GUI 例程,并且可以再次按下按钮。

如果您想i在后台运行 s 的打印,您可以考虑使用线程。通过这个,您可以继续将您的 GUI 用于不依赖于 for 循环执行的其他事情。启动线程后,您很可能还想禁用该按钮。在帧的每次更新中,您可以检查打印is 的线程是否已完成执行。如果这样做了,请加入线程并再次启用该按钮。


推荐阅读