从另一个线程渲染到QWindow
Rendering to QWindow from another thread
我一直在试图解决我的问题一段时间了,现在,到目前为止,我没有找到一个解决方案
我有一个QT应用程序,现在我需要在其中添加一个QWindow来使用opengl进行绘制。
我知道我需要调用QWidget::createWindowContainer()来创建我可以插入到另一个小部件或主窗口的QWidget。现在,我已经在我的子类QWindow类中创建了一个上下文。窗户暴露的时候我就这么做。然后我创建了另一个线程,我的渲染类生活,传递QOpenGLContext给它,使它当前在那里,但无论我尝试它只是不工作。
QWindow的子类。我只是初始化上下文,没有别的(我已经在构造函数中设置了表面类型):
void OpenGLWindow::initialize()
{
if (!m_context) {
m_context = new QOpenGLContext();
m_context->setFormat(requestedFormat());
m_context->create();
}
if(!isRunning) {
thread = new ThreadHelper(m_context, this);
thread->start();
}
}
然后ThreadHelper:
ThreadHelper::ThreadHelper(QOpenGLContext *context, OpenGLWindow *window) :
m_window(window),
m_context(context)
{
}
void ThreadHelper::run()
{
m_context->makeCurrent(m_window);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClearColor(1.0, 0.0, 0.0, 0.5);
// the rendering is done here, it's a loop, where I loop
// over classes and call the render method
}
void ThreadHelper::swapOpenGLBuffers(){
m_context->swapBuffers(m_window);
}
它在m_context-> makeccurrent (m_window)行崩溃。
还尝试将上下文移动到另一个线程,但没有成功,我得到一个运行时错误,说它不能移动。
来自文档:
QOpenGLContext可以通过moveToThread()移动到不同的线程。不要从不同的线程中调用makeccurrent ()QOpenGLContext对象所属的。上下文只能是当前的在一根线上,一次在一个表面上,一根线只有一次只能运行一个上下文
所以你需要在启动线程之前调用m_context->moveToThread(thread)
之类的东西。这可能不适合您,因为您试图在另一个线程中调用它。moveToThread
必须在当前拥有该对象的线程(即创建该对象的线程)中调用。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 运行同一解决方案的另一个项目的项目
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- C++从另一个类访问公共静态向量的正确方法是什么
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 使用std::transform将一个范围的元素添加到另一个范围中
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 修改函数中的指针(将另一个指针作为参数传递)
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- C++试图读取一个文件并输出到另一个文本文件
- 如何将指针从一个void函数传递到另一个C++
- 如何从另一个文件继承私有成员变量和公共函数
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 在他自己的方法中,有可能将一个对象取消引用到另一个对象吗
- C++-我可以创建另一个类的成员并在构造函数中使用它吗
- 调整QT中从QWindow继承的另一个类中的事件大小
- 从另一个线程渲染到QWindow