当我们关闭 QT 应用程序时如何关闭 GLFW window

How to close GLFW window when we close QT application

当我关闭 QT GUI 时,我希望 GLFW window 相应地关闭。

对于glfw,我们可以通过glfwWindowShouldClose函数查询window是否关闭。

如果应用程序 GUI 关闭,我们在 QT 中是否有类似的东西可以继续查询。

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TreeModel model;
    QTApplication w(model);
    int return_code = 0;
    QTApplication.show();
    glfwInit();
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
    glfwWindowHint(GLFW_SAMPLES, 4);
    window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Renderer", nullptr, nullptr);   // Create the render window
    if (window == NULL)
    {
        QMessageBox msgBox;
        msgBox.setText("Not able to create GL Window");
        msgBox.exec();
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    GLenum GlewInitResult;
    glewExperimental = GL_TRUE;
    GlewInitResult = glewInit();
    if (GLEW_OK != GlewInitResult)   // Check if glew is initialized properly
    {
        glfwTerminate();
    }       
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);      
    while (!glfwWindowShouldClose(window))
    {
        // Do Rendering here

    }       
    return_code =  a.exec();
    glfwTerminate();
    return return_code;
}

}

有一个信号,由应用程序发出:

QGuiApplication::lastWindowClosed()

看来您想要做的是将 Qt 事件循环与您自己的循环混合在一起。以下是有关如何从 Qt 文档执行此操作的提示:

To make your application perform idle processing (i.e. executing a special function whenever there are no pending events), use a QTimer with 0 timeout. More sophisticated idle processing schemes can be achieved using processEvents().

这就是我的处理方式:

  1. 设置一个连接到进行渲染的方法的 QTimer(如果 window 不应关闭)并重新启动计时器
  2. 调用a.exec()
  3. 定时器调用的方法中,如果window要关闭,调用a.quit()