grabWindow always returns null QPixmap
-
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.
-
@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. -
@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.
-
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).
-
@Christian-Ehrlicher I noticed you're using
QApplication
. Could that be the cause of the issue? I'm usingQGuiApplication
instead because of QML. -
No, tried with a QGuiApplication - all fine.
Please also test with this minimal example. -
@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?
-
This is not my code - please test it as I wrote it to make sure the simplest example is working.
-
@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.
-
@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.