GrabWindow not working with Qt::FramelessHint

  • I recognized a strange behaviour taking screenshots using Qt::FramlessHint. On Ubuntu 12.04 LTS everything is working fine, however, on ubuntu 14.04 (without standard desktop environment) and Openbox it doesn't. On QMainWindow which is framless, grabWindow() only grabs desktop background and other application's window (My QMainWindow seems to be invisible or transparent).

    //in constructor I set

    //on key event I execute
    QScreen *screen = QApplication::primaryScreen();
    QPixmap p = screen->grabWindow(0);

    QDateTime time = QDateTime::currentDateTime();

    QString name = QString("screenshot_%1").arg(time.toString("yyyyMMdd_hhmmsszzz"));
    QString file = QString("%1/%2.%3").arg(directory).arg(name).arg("png");
    if ( QFile::exists(file) ) {
    } QString(file), "png" );

    If i set Qt::WindowStaysOnTopHint (with border), screenshot is taken as expected. If i use (Qt::WindowStaysOnTopHint | Qt::FramlessWindowHint), screenshot does not display my Application. I also tried Qt::CustomizedWindowHint, however, my application is still not visible on screenshot. I also tried setWindowState(Qt::WindowFullScreen), but this did not work either.

    Using Qt 4.8 everything worked well, now on 5.3.0 or 5.3.1 it is not working anymore.

    Any suggestions? :) Thanks in advance!

    [edit: Added missing coding tags @ SGaist]

  • Hi
    I have exactly the same problem. Did you find a solution for it?


  • I have the same problem on Windows 7 Embedded. And the interesting thing is that it works fine when there is another monitor connected and I extend the desktop onto it.

    On my top level widget (which is thus a window), when I setGeometry to be the QApplication::primaryScreen()-geometry() it will fill the entire second monitor but on the primary it leaves like 15 pixels uncovered if I don't do FramelessWindowHint.

    And if I do FramelessWindowHint, grabWindow still works (with two monitors connected, extended.)

    But if I unplug the second monitor and keep FramelessWIndowHint, grabWIndow from the primaryScreen only gets black. I see something for a quick instant, then black.

    By the way, I use the winId() of my top-level widget in QApplication::primaryScreen()->grabWIndow(). Could it be that the winId is wrong or non-existent for a frameless window? I had read a post somewhere that said to setWindowTitle so that it would be listed in app list in Task Mgr (see here ) but that's a little different (and didnt work...)

  • Got a hack fix working:
    Make the window 1 pixel less in width and height than the primary screen:

    w.setWindowFlags(w.windowFlags() | Qt::FramelessWindowHint);
    QRect mPrimaryScreenRect = QApplication::primaryScreen()->availableVirtualGeometry();
    mPrimaryScreenRect.setWidth(mPrimaryScreenRect.width() - 1);
    mPrimaryScreenRect.setHeight(mPrimaryScreenRect.height() - 1);

    Also had some luck, but eventually abandoned this:
    DIFFERENT, LESS COMPLETE AND MORE RISKY HACK: Add a non-zero-sized spacerItem to the layout for the frameless window and then such that the whole layout is larger than the primary screen.

  • UPDATE: Change the second line if you have multiple monitors connected as an extended desktop and only want the main window on one of them:

    QRect mPrimaryScreenRect = QApplication::primaryScreen()->geometry();

  • Also note one pitfall: When specifying where to copy from, you must operate in global space. If you are trying to use grabWindow to copy the pixels of a given widget, use mapToGlobal:

      QPoint guiToCopyTopLeftGlobal = ui.guiToCopy->mapToGlobal(ui.guiToCopy->geometry().topLeft());

    I had that wrong a couple times and was copying from like x = -960 which is offscreen and gave a black copy.

Log in to reply