huilui
22nd August 2012, 22:48
Hi guys,
I've got a little problem: A little app of mine has a screenshot of the current desktop set as background. The application always runs in fullscreen. It all works just perfectly on a single monitor. However, on a dual-head system I run into some problems getting the right part of the screenshot to be set as background.
More details: I've got my primary monitor on the right and a secondary monitor on the left. I simply cannot detect reliably what screen I'm running the app on or what global position the app has. Some code:
// Getting the screenshot
QPixmap screen = QPixmap::grabWindow(QApplication::desktop()->winId());
// Here's one problem: Getting the current screen size (see note after code snippet).
globScreenRect = QApplication::desktop()->screenGeometry();
// The QPixmap bg is the new background pixmap, containing screen and an overlay
QPainter painter(&bg);
painter.setCompositionMode(QPainter::CompositionMo de_SourceOver);
int x = 0;
int y = 0;
int w = screen.width();
int h = screen.height();
if(QApplication::desktop()->numScreens() == 2) {
// Detecting if app is run on primary or secondary screen and adjust screenshot accordingly
if(globScreenRect.x() != 0) {
x = w-globScreenRect.width();
w = globScreenRect.width();
} else {
w = globScreenRect.width();
}
}
painter.drawPixmap(0, 0, overlay.width(), overlay.height(), screen,x,y,w,h);
painter.drawPixmap(overlay.rect(), overlay);
painter.end();
The problem seems to be detecting the current active screen size. "screenGeometry()" always returns the geometry of the primary screen (i.e. QRect(1024,0 1366x768) ). I could get the geometry of the secondary screen simply by using "screenGeometry(1)", but how do I know if the app is running on the primary or secondary screen?
If I try to find out the current global position of the app by "this->pos().x()" or even "mapToGlobal(this->pos()).x()", it always gives me "0", i.e. the absolute top left position... According to the Qt Doc: "mapToGlobal - Translates the widget coordinate pos to global screen coordinates.", but it somehow doesn't do that...?
Am thankful for any help :)
huilui
I've got a little problem: A little app of mine has a screenshot of the current desktop set as background. The application always runs in fullscreen. It all works just perfectly on a single monitor. However, on a dual-head system I run into some problems getting the right part of the screenshot to be set as background.
More details: I've got my primary monitor on the right and a secondary monitor on the left. I simply cannot detect reliably what screen I'm running the app on or what global position the app has. Some code:
// Getting the screenshot
QPixmap screen = QPixmap::grabWindow(QApplication::desktop()->winId());
// Here's one problem: Getting the current screen size (see note after code snippet).
globScreenRect = QApplication::desktop()->screenGeometry();
// The QPixmap bg is the new background pixmap, containing screen and an overlay
QPainter painter(&bg);
painter.setCompositionMode(QPainter::CompositionMo de_SourceOver);
int x = 0;
int y = 0;
int w = screen.width();
int h = screen.height();
if(QApplication::desktop()->numScreens() == 2) {
// Detecting if app is run on primary or secondary screen and adjust screenshot accordingly
if(globScreenRect.x() != 0) {
x = w-globScreenRect.width();
w = globScreenRect.width();
} else {
w = globScreenRect.width();
}
}
painter.drawPixmap(0, 0, overlay.width(), overlay.height(), screen,x,y,w,h);
painter.drawPixmap(overlay.rect(), overlay);
painter.end();
The problem seems to be detecting the current active screen size. "screenGeometry()" always returns the geometry of the primary screen (i.e. QRect(1024,0 1366x768) ). I could get the geometry of the secondary screen simply by using "screenGeometry(1)", but how do I know if the app is running on the primary or secondary screen?
If I try to find out the current global position of the app by "this->pos().x()" or even "mapToGlobal(this->pos()).x()", it always gives me "0", i.e. the absolute top left position... According to the Qt Doc: "mapToGlobal - Translates the widget coordinate pos to global screen coordinates.", but it somehow doesn't do that...?
Am thankful for any help :)
huilui