Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Failure Creating QOpenGLContext in Console Based Application



  • Hello,
    My team in currently working on an application which renders an OpenGL geometry off-screen to create a bitmap. When executing this code when our application is running with a QML gui everything works as expected.

    However, we are currently developing Google Test, and need to unit test this functionality. Obviously with Google Test there is no GUI, and instead we are just exercising this code through a console -- this is where the issues occurring. When running the following code through google test we get an exception.

    QSurfaceFormat format;
                format.setDepthBufferSize(MinDepthBufferSize);
                format.setStencilBufferSize(StencilBufferSize);
    
                context = make_unique<QOpenGLContext>();
                context->setFormat(format);
                context->create();
    

    The read access exception occurs on 'context->create();'. Documentation on the topic of running a headless OpenGL Qt application is scarce, but everything I found suggests this isn't an issue. I've seen several postings stating that settings in the .pro to run in terminal (i.e. QT -= gui) may be causing OPENGL to not be loaded; but with or without that line I still see the same issue.

    Any help would be appreciated. Thanks!



  • Ah ha! Simple creating an instance of QGuiApplication at the beginning of my test has appeared to resolve this problem, at least. Thank you for the help!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    How are you writing your tests ?

    Are you creating at least a QGuiApplication in your tests ?



  • No, I am not creating a QGuiApplication , which sounds like that might be the issue...

    My team already had gtest framework in place, so I was hoping to piggy back off of that.

    Simply I've just been running:

    int main(int argc, char *argv[])
    {
        ::testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
    }
    
    TEST(PEGTestCase, PEGTestSet)
    {
        QObjectToTest object = new QObjectToTest();
        int qty = object->GetZero();
        EXPECT_EQ(qty, 0);
        ASSERT_THAT(0, Eq(0));
    }
    

    The objects I've been initializing have been QObjects, and things have been working well thus far. Of course they have not had any GUI components, which would explain my need for not needing a QGuiApplication.

    Is it possible to integrate a QGuiApplication into this setup you could point me towards, or am I looking at using QTest to solve this issue? Thank you!



  • Ah ha! Simple creating an instance of QGuiApplication at the beginning of my test has appeared to resolve this problem, at least. Thank you for the help!


Log in to reply