Qt MDI app with OpenGL: how to grab a valid screen

2019-06-22 07:30发布

问题:

I have an MDI app written in Qt. Some of the subwindows include QGLWidgets, i.e., OpenGL contexts. The most salient of these use OpenGL vertex and fragment shaders to visualize their data.

I need to programmatically capture a screenshot of the application's main window, including of course all the subwindows that happen to be visible. Sounds easy at first ("definitely been asked many times, so let's just google it!"), but after a closer look it seems to be getting a bit tricky...

Now here's what I've tried so far:

  1. QPixmap::grabWidget doesn't work for the subwindows that use shader programs, apparently because the redirection context does not seem to support the necessary OpenGL extensions. (The rendering code checks that the associated context supports the required extensions, and refuses to continue if not.)
  2. QPixmap::grabWindow silently leaves all OpenGL contexts empty -- even those that use basic primitives only.
  3. QGLWidget::grabFrameBuffer works, but captures only the OpenGL context of the specific subwindow, whereas I'd like to grab the whole application (essentially what Alt+PrtScr does in Windows).

I also tried doing first #2 and then iterating #3 for all subwindows, just copying the result of #3 into the right location of the image from #2. This worked very well, until I made some subwindows overlap -- in which case the images from #3 overwrite the subwindow frames, etc. So this approach would probably require much more code in order to handle all the nasty corner cases...

Some additional background: Once I get the screenshot, I'm going to put several of them in a seqence in order to create a video -- probably using ffmpeg. That makes this question a kind of continuation to (the first part of) my previous question; it's just that the app has evolved from a single-context standalone OpenGL program to something that uses Qt for the overall windowing and UI widget stuff, and embeds the graphics in subwindows.

While I would of course prefer a nice, Qt-ish cross-platform solution, I'm willing to consider resorting to a Windows-specific hack, too. (Which ought to be possible, as I can see Alt+PrtScr doing very much the right thing.)

So, any suggestions?

回答1:

Very hackish, but does capturing the desktop (without hiding your window) like in this tutorial and cropping everything but your window work?

More generally, I'm not even sure that the OpenGL spec demands that output has to be placed in the front buffer: an implementation could just use video overlays (or Voodoo2-esque VGA pass-through shenanigans) for output.



回答2:

The only thing I can think of off-hand is to do the following psuedocode:

 for each window
     grab the window into its own pixmap
     grab the GL portions and paste into the windows
 order the window pixmaps by depth of the window (deepest first)
 initialize a common pixmap
 for each window pixmap
     paint the window pixmap into the common pixmap at the appropriate location.


回答3:

Is this any use??
The Grabber examples shows how to retrieve the contents of an OpenGL framebuffer