c++ - 单击按钮后,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;
}
}
解决方案
在您按下按钮的那一刻,相应if
语句中的所有内容都会被执行。i
这意味着在成功打印所有 s 之前不会更新您的用户界面。完成此操作后,您的程序将返回执行 GUI 例程,并且可以再次按下按钮。
如果您想i
在后台运行 s 的打印,您可以考虑使用线程。通过这个,您可以继续将您的 GUI 用于不依赖于 for 循环执行的其他事情。启动线程后,您很可能还想禁用该按钮。在帧的每次更新中,您可以检查打印i
s 的线程是否已完成执行。如果这样做了,请加入线程并再次启用该按钮。
推荐阅读
- amazon-s3 - 有没有一种方法可以定义一个函数来根据 kafka connect 中的主题消息来决定 S3 路径
- windows - 启动时运行的批处理文件不会使用“del”命令自行删除
- azure - 如何授权多个客户端应用程序访问我的 Azure 存储资源?
- python - 运行 python websocket 库示例代码时出错
- php - 使用 PHP 清除 SQLite3 表时遇到问题
- swift - 使用 Firebase `observe` 会破坏我在 Swift 中的 dispatchGroup.leave 代码,为什么?
- react-native-android - 分支 io 退订
- algorithm - 算法的复杂性
- java - http响应中的Android json解析错误
- ios - 使用 JSONDecoder 时 UICollectionViewCells 显示为空