Clean up QScreen::grabWindow()

Handle 0 WId parameter as meaning "desktop window"/whole screen.
Also, re-add the default values for the grab area, both for
convenience and compatibility with QPixmap::grabWindow() in Qt4.
Update the screenshot example so it doesn't comlain about usage of
deprecated QPixmap::grabWindow().

Change-Id: I2ad229113ddb8ded0388f2ebc0e8c703c6657f1f
Reviewed-by: Gunnar Sletta <gunnar.sletta@nokia.com>
This commit is contained in:
aavit 2012-08-17 14:15:36 +02:00 committed by Qt by Nokia
parent f6c58d242a
commit 94ac17c2cc
5 changed files with 20 additions and 13 deletions

View File

@ -172,16 +172,15 @@
\snippet examples/desktop/screenshot/screenshot.cpp 5 \snippet examples/desktop/screenshot/screenshot.cpp 5
We take the screenshot using the static QPixmap::grabWindow() Using the static function QApplication::primaryScreen(), we
obtain the QScreen object for the application's main screen.
We take the screenshot using the QScreen::grabWindow()
function. The function grabs the contents of the window passed as function. The function grabs the contents of the window passed as
an argument, makes a pixmap out of it and returns that pixmap. an argument, makes a pixmap out of it and returns that pixmap.
The window id can be obtained with QWidget::winId() or QWindow::winId().
We identify the argument window using the QWidget::winID() Here, however, we just pass 0 as the window id, indicating that we
function which returns the window system identifier. Here it want to grab the entire screen.
returns the identifier of the current QDesktopWidget retrieved by
the QApplication::desktop() function. The QDesktopWidget class
provides access to screen information, and inherits
QWidget::winID().
We update the screenshot preview label using the private \c We update the screenshot preview label using the private \c
updateScreenshotLabel() function. Then we enable the \uicontrol {New updateScreenshotLabel() function. Then we enable the \uicontrol {New

View File

@ -115,7 +115,9 @@ void Screenshot::shootScreen()
originalPixmap = QPixmap(); // clear image for low memory situations originalPixmap = QPixmap(); // clear image for low memory situations
// on embedded devices. // on embedded devices.
//! [5] //! [5]
originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId()); QScreen *screen = QGuiApplication::primaryScreen();
if (screen)
originalPixmap = screen->grabWindow(0);
updateScreenshotLabel(); updateScreenshotLabel();
newScreenshotButton->setDisabled(false); newScreenshotButton->setDisabled(false);

View File

@ -129,7 +129,7 @@ public:
bool isPortrait(Qt::ScreenOrientation orientation) const; bool isPortrait(Qt::ScreenOrientation orientation) const;
bool isLandscape(Qt::ScreenOrientation orientation) const; bool isLandscape(Qt::ScreenOrientation orientation) const;
QPixmap grabWindow(WId window, int x, int y, int w, int h); QPixmap grabWindow(WId window, int x = 0, int y = 0, int w = -1, int h = -1);
qreal refreshRate() const; qreal refreshRate() const;

View File

@ -183,6 +183,8 @@ Q_GUI_EXPORT QPixmap qt_pixmapFromWinHBITMAP(HBITMAP bitmap, int hbitmapFormat =
QPixmap QWindowsScreen::grabWindow(WId window, int x, int y, int width, int height) const QPixmap QWindowsScreen::grabWindow(WId window, int x, int y, int width, int height) const
{ {
// TODO: handle window==0, i.e. grab whole screen
if (QWindowsContext::verboseIntegration) if (QWindowsContext::verboseIntegration)
qDebug() << __FUNCTION__ << window << x << y << width << height; qDebug() << __FUNCTION__ << window << x << y << width << height;
RECT r; RECT r;

View File

@ -277,6 +277,13 @@ QPixmap QXcbScreen::grabWindow(WId window, int x, int y, int width, int height)
if (width == 0 || height == 0) if (width == 0 || height == 0)
return QPixmap(); return QPixmap();
// TODO: handle multiple screens
QXcbScreen *screen = const_cast<QXcbScreen *>(this);
xcb_window_t root = screen->root();
if (window == 0)
window = root;
xcb_get_geometry_cookie_t geometry_cookie = xcb_get_geometry_unchecked(xcb_connection(), window); xcb_get_geometry_cookie_t geometry_cookie = xcb_get_geometry_unchecked(xcb_connection(), window);
xcb_get_geometry_reply_t *reply = xcb_get_geometry_reply_t *reply =
@ -291,9 +298,6 @@ QPixmap QXcbScreen::grabWindow(WId window, int x, int y, int width, int height)
if (height < 0) if (height < 0)
height = reply->height - y; height = reply->height - y;
// TODO: handle multiple screens
QXcbScreen *screen = const_cast<QXcbScreen *>(this);
xcb_window_t root = screen->root();
geometry_cookie = xcb_get_geometry_unchecked(xcb_connection(), root); geometry_cookie = xcb_get_geometry_unchecked(xcb_connection(), root);
xcb_get_geometry_reply_t *root_reply = xcb_get_geometry_reply_t *root_reply =
xcb_get_geometry_reply(xcb_connection(), geometry_cookie, NULL); xcb_get_geometry_reply(xcb_connection(), geometry_cookie, NULL);