Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved grabWindow only grabs desktop background in macOS Catalina

    General and Desktop
    2
    3
    322
    Loading More Posts
    • 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.
    • freddy311082
      freddy311082 last edited by

      Hi Guys

      I'm working in an eyedropper component and I'm not able to take a good screenshot. The problem I have is with grabWindow which always takes the pixel value from the desktop wallpaper. It doesn't include any app in the screenshot.

      The code I'm testing looks as this:

      QMouseEvent* evMouse = dynamic_cast<QMouseEvent*>(ev);
      cout << "Posicion: " << evMouse->x() << ", " << evMouse->y() << endl;
      QPoint mousePos = QCursor::pos();
              
      int screenNumber = qApp->desktop()->screenNumber(mousePos);
      QScreen* screen = qApp->screenAt(QCursor::pos());
      cout << "Mouse Pos: " << mousePos.x() << ", " << mousePos.y() << ". Screen " << screenNumber << endl;
              
      QPixmap pixmap = screen->grabWindow(qApp->desktop()->winId(), mousePos.x(), mousePos.y(), 1, 1);
      Q_EMIT selectedPixelColor(pixmap.toImage().pixel(0, 0));
      

      I'm running the project in macOS Catalina 10.15.5 against Qt 5.12.8.

      Is there anything that I'm doing wrong here? Please, let me know if you need the entire code because it's test project and it's very small.

      Regards

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @freddy311082 last edited by

        @freddy311082 said in grabWindow only grabs desktop background in macOS Catalina:

        QPixmap pixmap = screen->grabWindow(qApp->desktop()->winId(), mousePos.x(), mousePos.y(), 1, 1);

        I think you need to map the mouse coordinates to global: https://doc.qt.io/qt-5/qwidget.html#mapToGlobal

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 2
        • freddy311082
          freddy311082 last edited by

          Awesome @jsulm... it works !!! Thank you very much !!!

          1 Reply Last reply Reply Quote 0
          • First post
            Last post