Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. GrabWindow not working with Qt::FramelessHint
QtWS25 Last Chance

GrabWindow not working with Qt::FramelessHint

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Axorn
    wrote on last edited by
    #1

    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
    setWindowFlags(Qt::FramelessWindowHint);
    show();

    //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) ) {
    QFile::remove(file);
    }
    p.save( 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]

    A 1 Reply Last reply
    0
    • A Axorn

      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
      setWindowFlags(Qt::FramelessWindowHint);
      show();

      //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) ) {
      QFile::remove(file);
      }
      p.save( 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]

      A Offline
      A Offline
      Aadi
      wrote on last edited by
      #2

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

      Thanks
      Adrian

      1 Reply Last reply
      0
      • G Offline
        G Offline
        GeorgePTVS
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          GeorgePTVS
          wrote on last edited by
          #4

          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);
          w.move(mPrimaryScreenRect.topLeft());
          w.setGeometry(mPrimaryScreenRect);
          w.show();
          

          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.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            GeorgePTVS
            wrote on last edited by
            #5

            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();
            
            1 Reply Last reply
            0
            • G Offline
              G Offline
              GeorgePTVS
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved