简体   繁体   English

异步渲染到Qt OpenGL窗口

[英]Asynchronous Rendering to Qt OpenGL Window

I am writing a Windows Visual Studio 2017 C++ program that will render a series of frames from a capture card onto a Qt 5.9 OpenGL Window. 我正在编写Windows Visual Studio 2017 C ++程序,该程序会将捕获卡上的一系列帧渲染到Qt 5.9 OpenGL窗口上。 Loading and processing the images is done asynchronously in a thread until it is stopped: 图像的加载和处理在线程中异步完成,直到停止:

void readFile(std::string filename)
{
  auto reader = openreader(file);

   while(!done())
   {
      QImage frame = reader.read();
      processFrame(frame);
      displayFrame(frame); /// <--- here
}

and the thread might be kicked off by 并且线程可能会被启动

std::thread workit(readfile,"c:\funtimes.dat");

What is the proper pattern to follow to get the QImage frame containing pixels onto the screen? 要使包含像素的QImage frame显示在屏幕上,应该遵循什么适当的模式? In .Net some care must be taken to only modify the gui from the main thread. 在.Net中,必须注意只能从主线程修改gui。 How does that work with Qt? Qt如何运作?

The current way I have is to set a local variable to equal a copy of the frame, then call Update() on the OpenGL QWidget. 我目前的方法是将局部变量设置为等于框架的副本,然后在OpenGL QWidget上调用Update() That is, the code code displayFrame looks about like: 也就是说,代码代码displayFrame看起来像:

void displayFrame(QImage frame)
{
   // Mutex here
   oglWindow.copyOfFrame= frame.copy();
   oglWindow.update(); 
}

This triggers a paintEvent that draws to the window with 这将触发一个paintEvent ,该事件通过

painter.drawImage(QPoint(width()/2 - bi.width()/2, height()/2 - bi.height()/2), copyOfFrame);

Is this safe and appropriate when a thread outside the main window message pump is triggering updates or is another mechanism required? 当主窗口消息泵外部的线程正在触发更新或需要其他机制时,这是否安全且合适?

If your asynchronous image loader is a subclass of QThread, you can signal the main thread from the image loader and QT will ensure that the handling code slot is executed in the main thread. 如果异步图像加载器是QThread的子类,则可以从图像加载器发出信号通知主线程,并且QT将确保在主线程中执行处理代码槽。 See the Qt Threads and QObjects doc 请参阅Qt 线程和QObjects文档

If the image loader isn't a QThread, just a regular Win32 thread, you should use QCoreApplication::PostEvent for thread safe delivery 如果图像加载器不是QThread,而只是常规Win32线程,则应使用QCoreApplication :: PostEvent进行线程安全传递

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM