Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to take desktop screenshot on Mac OS X?
I need to take screenshot inside my app, but when I trying to do it, like in example, application crashed with EXC_BAD_ACCESS error. It's bug? What else I can use? Now I use Qt 5.1.1
Code to take screenshot:
@QPixmap screenshot = QPixmap::grabWindow(QApplication::desktop()->winId());@
Hi and welcome to devnet,
Can you check what does QApplication::desktop() returns ?
I don't know it's correct or not, but not NULL. There is "screenshot":https://www.dropbox.com/s/qqpm45xsn0idyaa/debug.png (image are to large for attach to post) of debug window with all values.
The image is not accessible
What does winId() return ?
winId() returns 4317431408.
Hmm, I check this code on Mac OS X 10.8, and it works OK. But on 10.6 it crashes :(
If you can, could you build your application directly on 10.6 and retry ?
I made all builds on same systems, where made tests. So, it crashes on 10.6 if compiled on 10.6 too.
I can't find code for taking screenshot in Qt sources, but looks like it uses CGDisplayCreateImage() function. It's buggy on Mac OS 10.6, and crashes app if used. For now I changed code to this, and it works on both Mac OS versions:
// This line crashes app on 10.6, use second instead
// CGImageRef windowImage = CGDisplayCreateImage(CGMainDisplayID());
CGImageRef windowImage = CGWindowListCreateImage(CGRectNull, kCGWindowListOptionOnScreenOnly,
QPixmap screenshot = fromMacCGImageRef(windowImage);
QPixmap screenshot = mainScreen->grabWindow(QApplication::desktop()->winId());
I got fromMacCGImageRef() from old revision of qtmacports.
But now I got other issue - MyQMainWindow->showFullScreen() does't open window fullscreen. O_o
I needed to take it from my app. I know how to take it with system hotkeys.
Now I see some critical bugs in Qt on Mac OS X, so now I trying to use Java.
Anyway, thanks for the help!
Can you provide a minimal compilable example that shows the crash ?
It's in first post. Just call this code on Mac OS 10.6, and app will crash.
I can take screenshot with Mac-specific code from my "post":http://qt-project.org/forums/viewreply/150944/ but I got two issues with fullscreen window now:
- On 10.6 QMainWindow::showFullScreen() does nothing. Just shows window not fullscreen. Strange, but Java has same issue. Looks like it fixed now with "this":https://codereview.qt-project.org/#change,70543 path, but I didn't try it, because I can't install Qt 5.2 beta to Mac OS 10.6 previosly;
- On 10.8 calling QMainWindow::showFullScreen() opens fullscreen window normally, but when I call QMainWindow::close() - blank frame still appears on screen. Don't know how to fix that.
vezprog last edited by
You can render a QWidget using QPixmap. This is how you would render the QWidget.
// render the widget to QPixmap
// save the screen shot
Edit: I had to change my post, I misunderstood your question...I thought you wanted to grab a screen shot of the whole screen.
[quote author="dvez43" date="1387217447"]You might be able to create a transparent widget to the size of the screen (using QDesktopWidget to get the dimensions), and get the screen shot.[/quote]
And it really will work on both systems? Looks so hacky.
For example, I can't open non-fullscreen window with all screen dimensions. Upper menu bar are unreachable for this windows.
vezprog last edited by
The edited code example above should work.
belab last edited by
Maybe the problem in your original code is in line 16:
@this->screenshot = &screenshot;@
You are assigning the pointer of a local object to a member. Going out of scope destroys the pixmap.
Yes, it was wrong, and I already changed this code. But problem are not here.
Did you retry with a more recent version of Qt ?
I no longer have machine with Mac OS 10.6, so I can't test it anymore.
Now I trying to support 10.8+ only.