Important: Please read the Qt Code of Conduct -

How to take desktop screenshot on Mac OS X?

  • Hello!
    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());@

  • Lifetime Qt Champion

    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": (image are to large for attach to post) of debug window with all values.

  • Lifetime Qt Champion

    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 :(

  • Lifetime Qt Champion

    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:
    @#ifdef Q_OS_MAC
    // This line crashes app on 10.6, use second instead
    // CGImageRef windowImage = CGDisplayCreateImage(CGMainDisplayID());
    CGImageRef windowImage = CGWindowListCreateImage(CGRectNull, kCGWindowListOptionOnScreenOnly,
    0, kCGWindowImageBoundsIgnoreFraming);
    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!

  • Lifetime Qt Champion

    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": but I got two issues with fullscreen window now:

    1. 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":,70543 path, but I didn't try it, because I can't install Qt 5.2 beta to Mac OS 10.6 previosly;
    2. 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.

  • You can render a QWidget using QPixmap. This is how you would render the QWidget.
    // render the widget to QPixmap
    QPixmap screenshot(widget->size());

    // save the screen shot"fileName.jpg");

    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.

  • The edited code example above should work.

  • 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.

  • Lifetime Qt Champion

    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.

Log in to reply