How to close GLFW window when we close QT application

孤人 提交于 2020-03-04 18:55:52

问题


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

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

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;
}

}


回答1:


There is a signal for that, emitted by the application:

QGuiApplication::lastWindowClosed()

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:

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().

So this is how I would approach this:

  1. Setup a QTimer that connects to a method that does rendering (if window should not close) and restarts the timer
  2. Call a.exec()
  3. In the method called by the timer, if window should close, call a.quit()


来源:https://stackoverflow.com/questions/60222730/how-to-close-glfw-window-when-we-close-qt-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!