关闭QT应用程序时如何关闭GLFW窗口 [英] How to close GLFW window when we close QT application

查看:297
本文介绍了关闭QT应用程序时如何关闭GLFW窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我关闭QT GUI时,我希望相应地关闭GLFW窗口.

When i close the QT GUI i want the GLFW window to be closed accordingly.

对于glfw,我们可以通过glfwWindowShouldClose函数查询窗口是否关闭.

For glfw we can query if window is closed or not by glfwWindowShouldClose function.

我们在QT中有类似的东西吗?我们可以继续查询是否关闭了应用程序GUI.

Do we have anything like that in QT where we can keep Querying if the application GUI is closed.

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文档中做到这一点的提示:

It looks like what you want to do is intermangle the Qt event loop with your own loop. Here is a hint about how to do that from the Qt docs:

要使您的应用执行空闲处理(即,在没有待处理事件的情况下执行特殊功能),请使用超时为0的QTimer.使用processEvents()可以实现更复杂的空闲处理方案.

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,该QTimer连接到执行渲染(如果窗口不应关闭)并重启计时器的方法
  2. 调用a.exec()
  3. 在计时器调用的方法中,如果窗口应该关闭,请调用a.quit()

这篇关于关闭QT应用程序时如何关闭GLFW窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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