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 always returns null QPixmap
QtWS25 Last Chance

grabWindow always returns null QPixmap

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpixmapgrab
14 Posts 4 Posters 1.0k 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.
  • C ChrisW67
    15 Aug 2024, 07:07

    @Saviz said in grabWindow always returns null QPixmap:

    // The geomotry is correct and gives 1920 X 1080

    What does it give for the position and are you in a single or multi-monitor setup? In multi-monitor, some of the screens will return a non-zero position (e.g. 1920x1080+1920+0) and, because the x/y parameters to grabWindow() are relative to the screen, using these values will place the capture area outside the screen.

    Have you tried without the geometry? This, selectedScreen->grabWindow(), should default to grabbing all of the screen.

    S Offline
    S Offline
    Saviz
    wrote on 15 Aug 2024, 14:29 last edited by
    #3

    @ChrisW67 I only have one monitor and the position that it returns is (0, 0). I also tried your recommendation for just using selectedScreen->grabWindow(), but it still results in a null QPixmap.

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      zeljko
      wrote on 15 Aug 2024, 15:57 last edited by
      #4

      Try to make screenshoot without timer and check if result is null pixmap.

      S 1 Reply Last reply 15 Aug 2024, 16:59
      0
      • Z zeljko
        15 Aug 2024, 15:57

        Try to make screenshoot without timer and check if result is null pixmap.

        S Offline
        S Offline
        Saviz
        wrote on 15 Aug 2024, 16:59 last edited by
        #5

        @zeljko I tried your suggestion and called it without the timer part for both versions like this:

        Version 1:

        QPixmap output = selectedScreen->grabWindow(0, selectedScreen->geometry().x(), selectedScreen->geometry().y(), selectedScreen->geometry().width(), selectedScreen->geometry().height());
        
        if (output.isNull())
        {
                qDebug() << "Pixmap is null.";
        }
        

        Version 2:

        QPixmap output = selectedScreen->grabWindow();
        
        if (output.isNull())
        {
                qDebug() << "Pixmap is null.";
        }
        

        Both result in a null QPixmap.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 15 Aug 2024, 17:03 last edited by
          #6
          int main(int argc, char* argv[])
          {
              QApplication a(argc, argv);
              auto pm = a.primaryScreen()->grabWindow();
              qDebug() << pm;
              return 0;
          }
          

          This works fine for me with Qt6.8 on windows 10 and linux (x11).

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          S 1 Reply Last reply 15 Aug 2024, 17:29
          1
          • C Christian Ehrlicher
            15 Aug 2024, 17:03
            int main(int argc, char* argv[])
            {
                QApplication a(argc, argv);
                auto pm = a.primaryScreen()->grabWindow();
                qDebug() << pm;
                return 0;
            }
            

            This works fine for me with Qt6.8 on windows 10 and linux (x11).

            S Offline
            S Offline
            Saviz
            wrote on 15 Aug 2024, 17:29 last edited by
            #7

            @Christian-Ehrlicher I noticed you're using QApplication. Could that be the cause of the issue? I'm using QGuiApplication instead because of QML.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 15 Aug 2024, 17:52 last edited by
              #8

              No, tried with a QGuiApplication - all fine.
              Please also test with this minimal example.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              S 1 Reply Last reply 16 Aug 2024, 21:22
              0
              • C Christian Ehrlicher
                15 Aug 2024, 17:52

                No, tried with a QGuiApplication - all fine.
                Please also test with this minimal example.

                S Offline
                S Offline
                Saviz
                wrote on 16 Aug 2024, 21:22 last edited by Saviz
                #9

                @Christian-Ehrlicher I created a new project using the simple example you provided and tested it on each platform. Here is the complete code:

                int main(int argc, char *argv[])
                {
                    QGuiApplication app(argc, argv);
                    QQmlApplicationEngine engine;
                
                    // Loading engine here...
                
                    auto pm = app.primaryScreen()->grabWindow();
                    qDebug() << pm;
                
                    return app.exec()
                }
                

                I did a bunch of tests. It was only successful in one run, and every other time the output is still null. At this point, I'm not sure what might be causing the problem as the results flicker. Could it be related to the Qt version?

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 16 Aug 2024, 21:23 last edited by
                  #10

                  This is not my code - please test it as I wrote it to make sure the simplest example is working.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  S 1 Reply Last reply 16 Aug 2024, 21:26
                  0
                  • C Christian Ehrlicher
                    16 Aug 2024, 21:23

                    This is not my code - please test it as I wrote it to make sure the simplest example is working.

                    S Offline
                    S Offline
                    Saviz
                    wrote on 16 Aug 2024, 21:26 last edited by
                    #11

                    @Christian-Ehrlicher I even tested your code using QApplication

                    int main(int argc, char* argv[])
                    {
                        QApplication a(argc, argv);
                        auto pm = a.primaryScreen()->grabWindow();
                        qDebug() << pm;
                        return 0;
                    }
                    

                    This results in the same issues.

                    @Christian-Ehrlicher said in grabWindow always returns null QPixmap:

                    No, tried with a QGuiApplication - all fine.
                    Please also test with this minimal example.

                    Besides, what difference does it make? You were the one who claimed there was no difference.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Saviz
                      wrote on 16 Aug 2024, 22:41 last edited by
                      #12

                      Could this be a problem with the version of my Qt kit?

                      C 1 Reply Last reply 17 Aug 2024, 06:38
                      0
                      • S Saviz
                        16 Aug 2024, 22:41

                        Could this be a problem with the version of my Qt kit?

                        C Offline
                        C Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 17 Aug 2024, 06:38 last edited by
                        #13

                        @Saviz said in grabWindow always returns null QPixmap:

                        Could this be a problem with the version of my Qt kit?

                        I don't think so. It works for sure on windows - the rest depends on the window manager.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • Z Offline
                          Z Offline
                          zeljko
                          wrote on 18 Aug 2024, 14:02 last edited by
                          #14

                          Is it under x11 or wayland ?

                          1 Reply Last reply
                          0

                          12/14

                          16 Aug 2024, 22:41

                          • Login

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