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

    I've been struggling with this issue for days, searching through countless posts online, but I still can't find the cause. I'm trying to capture the full screen using the grabWindow() method. However, it consistently fails and returns an invalid or null QPixmap.

    Here is my header file:

    #ifndef SCREENSHOT_H
    #define SCREENSHOT_H
    
    #include <QObject>
    #include <QTimer>
    #include <QScreen>
    #include <QPixmap>
    #include <QGuiApplication>
    
    class Screenshot : public QObject
    {
        Q_OBJECT
    
    public:
        explicit Screenshot(QObject *parent = nullptr);
        ~Screenshot();
    
    private:
        QPixmap m_Screenshot;
    
    public:
        void takeScreenshot(const QString &screenName);
    
    private:
        void setScreenshot(const QPixmap &newScreenshot);
    };
    
    #endif // SCREENSHOT_H
    

    Here is my cpp file:

    #include "screenshot.hpp"
    
    // Constructors, Destructor
    // [[------------------------------------------------------------------------]]
    // [[------------------------------------------------------------------------]]
    
    Screenshot::Screenshot(QObject *parent, const QString& name)
    : QObject{parent}
    , m_Screenshot(QPixmap{})
    {
    
    }
    
    Screenshot::~Screenshot()
    {
    
    }
    
    // [[------------------------------------------------------------------------]]
    // [[------------------------------------------------------------------------]]
    
    
    
    
    
    // PUBLIC Methods
    // [[------------------------------------------------------------------------]]
    // [[------------------------------------------------------------------------]]
    
    void Screenshot::takeScreenshot(const QString &screenName)
    {
            QScreen* selectedScreen = nullptr;
    
            // Find the screen with the specified name;
            for (QScreen *screen : QGuiApplication::screens())
            {
                    if (screen->name() == screenName)
                    {
                            selectedScreen = screen;
                            break;
                    }
            }
    
            // In testing the screen is not null.
            if(selectedScreen == nullptr)
            {
                    qDebug() << "Screen is null";
            }
    
            // The screen is always found and this never prints.
            if (!selectedScreen) {
                    qDebug() << "Error: No screen found with name" << screenName;
            }
    
    
            // The geomotry is correct and gives 1920 X 1080
            qDebug() << "Screen geometry:" << selectedScreen->geometry();
    
            // The pixel ratio is also correct and gives 1
            qDebug() << "Device pixel ratio:" << selectedScreen->devicePixelRatio();
    
    
            // Wait for 850 miliseconds before calling the functionality. This makes sure that the window has enough time to hide.
            QTimer::singleShot(
    
                    850,
                    this,
                    [this, selectedScreen]()
                    {
                            setScreenshot(
                                    selectedScreen->grabWindow(0, selectedScreen->geometry().x(), selectedScreen->geometry().y(), selectedScreen->geometry().width(), selectedScreen->geometry().height())
                            );
    
                            // This always prints null. This means that the operation is not successful.
                            if (m_Screenshot.isNull())
                            {
                                    qDebug() << "Pixmap is null.";
                            }
                    }
            );
    }
    
    // [[------------------------------------------------------------------------]]
    // [[------------------------------------------------------------------------]]
    
    
    
    
    // Setters
    // [[------------------------------------------------------------------------]]
    // [[------------------------------------------------------------------------]]
    
    void Screenshot::setScreenshot(const QPixmap &newScreenshot)
    {
            // Performing deep copy.
            m_Screenshot = newScreenshot.copy();
    }
    
    // [[------------------------------------------------------------------------]]
    // [[------------------------------------------------------------------------]]
    

    I've tested this on all platforms (Linux, Windows, macOS) using Qt version 6.7.2 and the result is the same. At this point, I doubt the problem is the underlying platform. Any help would be greatly appreciated.

    C Offline
    C Offline
    ChrisW67
    wrote on last edited by
    #2

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

    SavizS 1 Reply Last reply
    1
    • C ChrisW67

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

      SavizS Offline
      SavizS Offline
      Saviz
      wrote on 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 last edited by
        #4

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

        SavizS 1 Reply Last reply
        0
        • Z zeljko

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

          SavizS Offline
          SavizS Offline
          Saviz
          wrote on 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
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

            SavizS 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher
              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).

              SavizS Offline
              SavizS Offline
              Saviz
              wrote on 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
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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

                SavizS 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

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

                  SavizS Offline
                  SavizS Offline
                  Saviz
                  wrote on 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
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 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

                    SavizS 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

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

                      SavizS Offline
                      SavizS Offline
                      Saviz
                      wrote on 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
                      • SavizS Offline
                        SavizS Offline
                        Saviz
                        wrote on last edited by
                        #12

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

                        Christian EhrlicherC 1 Reply Last reply
                        0
                        • SavizS Saviz

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

                          Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 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 last edited by
                            #14

                            Is it under x11 or wayland ?

                            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