[英]How to render opengl in a separate thread in Qt, using QOpenGLWidget?
我用Google搜索並發現,可以使用qml在單獨的線程上進行繪制。 http://doc.qt.io/qt-5/qtquick-scenegraph-openglunderqml-example.html
但這不是我所需要的。 如何在沒有qml的情況下使用常見的qt小部件在單獨的線程中渲染?
如果您的QWidget
繼承自QOpenglWidget ,則只需調用
this->makeCurrent();
但是我個人更喜歡通過使用QWindow並在那里配置所有與OpenGL相關的設置來封裝OpenGL上下文的更有效 , 更可靠的方法:
這是一個例子:
bool MyOpenGLWindow::Create()
{
this->requestActivate();
if(!glContext)
{
glContext= new QOpenGLContext(this);
QSurfaceFormat fmt = requestedFormat();
int maj = fmt.majorVersion();
int min = fmt.minorVersion();
glContext->setFormat( requestedFormat());
glContext->create();
if(glContext->isValid() == false)
{
QString str;
str.sprintf("Failed to create GL:%i.%i context",maj,min);
return false;
}
}
glContext->makeCurrent(this);
//after this line you can work with OpenGL
initializeOpenGLFunctions();
glDisable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
//etc...
現在,請記住,每次在線程中生成(或移動現有的)此類的實例時,都必須將上下文設置為當前。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.